Commit 8e4708ae authored by Chunchi Che's avatar Chunchi Che

rerange hands.tsx

parent 53b3a0bb
...@@ -14,22 +14,27 @@ import { useHover } from "react-babylonjs"; ...@@ -14,22 +14,27 @@ import { useHover } from "react-babylonjs";
import { useClick } from "./hook"; import { useClick } from "./hook";
import { useState, useRef, useEffect } from "react"; import { useState, useRef, useEffect } from "react";
import { useSpring, animated } from "./spring"; import { useSpring, animated } from "./spring";
import { zip } from "./util";
const groundShape = CONFIG.GroundShape(); const groundShape = CONFIG.GroundShape();
const left = -(groundShape.width / 2); const left = -(groundShape.width / 2);
const handShape = CONFIG.HandShape();
const handRotation = CONFIG.HandRotation();
const Hands = () => { const Hands = () => {
const hands = useAppSelector(selectMeHands).cards; const meHands = useAppSelector(selectMeHands).cards;
const meHandPositions = handPositons(0, meHands);
return ( return (
<> <>
{hands.map((hand, idx) => { {zip(meHands, meHandPositions).map(([hand, position], idx) => {
return ( return (
<CHand <CHand
state={hand}
idx={idx}
key={idx} key={idx}
gap={groundShape.width / (hands.length - 1)} state={hand}
sequence={idx}
position={position}
rotation={handRotation}
/> />
); );
})} })}
...@@ -37,25 +42,22 @@ const Hands = () => { ...@@ -37,25 +42,22 @@ const Hands = () => {
); );
}; };
const CHand = (props: { state: Hand; idx: number; gap: number }) => { const CHand = (props: {
const handShape = CONFIG.HandShape(); state: Hand;
const rotation = CONFIG.HandRotation(); sequence: number;
position: BABYLON.Vector3;
rotation: BABYLON.Vector3;
}) => {
const hoverScale = CONFIG.HandHoverScaling(); const hoverScale = CONFIG.HandHoverScaling();
const defaultScale = new BABYLON.Vector3(1, 1, 1); const defaultScale = new BABYLON.Vector3(1, 1, 1);
const edgesWidth = 2.0; const edgesWidth = 2.0;
const edgesColor = BABYLON.Color4.FromColor3(BABYLON.Color3.Yellow()); const edgesColor = BABYLON.Color4.FromColor3(BABYLON.Color3.Yellow());
const planeRef = useRef(null); const planeRef = useRef(null);
const [state, idx] = [props.state, props.idx]; const state = props.state;
const [hovered, setHovered] = useState(false); const [hovered, setHovered] = useState(false);
const dispatch = store.dispatch; const dispatch = store.dispatch;
const [position, setPosition] = useState( const [position, setPosition] = useState(props.position);
new BABYLON.Vector3(
left + props.gap * props.idx,
handShape.height / 2,
-(groundShape.height / 2) - 1
)
);
const [spring, api] = useSpring( const [spring, api] = useSpring(
() => ({ () => ({
...@@ -76,18 +78,12 @@ const CHand = (props: { state: Hand; idx: number; gap: number }) => { ...@@ -76,18 +78,12 @@ const CHand = (props: { state: Hand; idx: number; gap: number }) => {
); );
useEffect(() => { useEffect(() => {
const newPosition = new BABYLON.Vector3(
left + props.gap * props.idx,
handShape.height / 2,
-(groundShape.height / 2) - 1
);
api.start({ api.start({
position: newPosition, position,
}); });
setPosition(newPosition); setPosition(position);
}, [props.idx, props.gap]); }, [position]);
useHover( useHover(
() => setHovered(true), () => setHovered(true),
...@@ -122,19 +118,19 @@ const CHand = (props: { state: Hand; idx: number; gap: number }) => { ...@@ -122,19 +118,19 @@ const CHand = (props: { state: Hand; idx: number; gap: number }) => {
// @ts-ignore // @ts-ignore
<animated.transformNode name=""> <animated.transformNode name="">
<animated.plane <animated.plane
name={`hand-${idx}`} name={`hand-${props.sequence}`}
ref={planeRef} ref={planeRef}
width={handShape.width} width={handShape.width}
height={handShape.height} height={handShape.height}
scaling={hovered ? hoverScale : defaultScale} scaling={hovered ? hoverScale : defaultScale}
position={spring.position} position={spring.position}
rotation={rotation} rotation={props.rotation}
enableEdgesRendering enableEdgesRendering
edgesWidth={state.interactivities.length == 0 ? 0 : edgesWidth} edgesWidth={state.interactivities.length == 0 ? 0 : edgesWidth}
edgesColor={edgesColor} edgesColor={edgesColor}
> >
<animated.standardMaterial <animated.standardMaterial
name={`hand-mat-${idx}`} name={`hand-mat-${props.sequence}`}
diffuseTexture={ diffuseTexture={
new BABYLON.Texture( new BABYLON.Texture(
`https://cdn02.moecube.com:444/images/ygopro-images-zh-CN/${state.meta.id}.jpg` `https://cdn02.moecube.com:444/images/ygopro-images-zh-CN/${state.meta.id}.jpg`
...@@ -172,4 +168,13 @@ function interactTypeToString(t: InteractType): string { ...@@ -172,4 +168,13 @@ function interactTypeToString(t: InteractType): string {
} }
} }
const handPositons = (player: number, hands: Hand[]) => {
const gap = groundShape.width / (hands.length - 1);
const y = handShape.height / 2;
const z =
player == 0 ? -(groundShape.height / 2) - 1 : groundShape.height / 2 + 1;
return hands.map((_, idx) => new BABYLON.Vector3(left + gap * idx, y, z));
};
export default Hands; export default Hands;
export function zip<S1, S2>(
firstCollection: Array<S1>,
lastCollection: Array<S2>
): Array<[S1, S2]> {
const length = Math.min(firstCollection.length, lastCollection.length);
const zipped: Array<[S1, S2]> = [];
for (let index = 0; index < length; index++) {
zipped.push([firstCollection[index], lastCollection[index]]);
}
return zipped;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment