我有这个 Card 组件,单击它会激活并展开块。想点击一个,然后另一个他们切换位置。就像是:
// Initial Board
1,1,1,1,
2,2,2,2,
3,3,3,3,
4,4,4,4,
并且不考虑在线相同的数字。行、列或主对角线。完成对块的排序,例如:
// win condition for Board
1, 2, 3, 4,
4, 3, 2, 1,
2, 1, 4, 3,
3, 4, 1, 2,
// the Card component
const Card = ({locale, args, color, speed}) => {
const mesh = useRef(null)
useFrame(() => (mesh.current.rotation.x = mesh.current.rotation.y += 0.01));
// expand state of the mesh. change size on click event
const [expand, setExpand] = useState(false);
const props = useSpring({
scale: expand ? [1.4, 1.4, 1.4]: [1,1,1],
})
return (
<a.mesh
onClick={() => setExpand(!expand)}
scale={props.scale}
castShadow
position={locale}
ref={mesh}>
<boxBufferGeometry attach='geometry' args={args} />
<MeshWobbleMaterial attach='material' color={color} speed={speed} factor={0.6} />
</a.mesh>
)
}
// its rendering inside Canvas from react-three-fiber
<Card locale={[-2, 1, -2]} color={'navy'} speed={6} />
<Card locale={[0, 1, -2]} color={'navy'} speed={6} />
<Card locale={[2, 1, -2]} color={'navy'} speed={6} />
<Card locale={[4, 1, -2]} color={'navy'} speed={6} />
<Card locale={[-2, 3, -2]} color={'teal'} speed={3} />
<Card locale={[0, 3, -2]} color={'teal'} speed={3} />
<Card locale={[2, 3, -2]} color={'teal'} speed={3} />
<Card locale={[4, 3, -2]} color={'teal'} speed={3} />
<Card locale={[-2, 5, -2]} color={'blue'} speed={9} />
<Card locale={[0, 5, -2]} color={'blue'} speed={9} />
<Card locale={[2, 5, -2]} color={'blue'} speed={9} />
<Card locale={[4, 5, -2]} color={'blue'} speed={9} />
<Card locale={[-2, -1, -2]} color={'aqua'} speed={2} />
<Card locale={[0, -1, -2]} color={'aqua'} speed={2} />
<Card locale={[2, -1, -2]} color={'aqua'} speed={2} />
<Card locale={[4, -1, -2]} color={'aqua'} speed={2} />
github 链接在这里:https ://github.com/iagokrt/board-game-threejs 我想出了一个愚蠢的董事会逻辑。