Spaces:
Sleeping
Sleeping
lock rotation
Browse files- src/components/rotator.tsx +68 -53
- src/components/rubiks-cube.tsx +1 -4
src/components/rotator.tsx
CHANGED
|
@@ -2,108 +2,123 @@ import { useCubesContext } from "@/contexts/cubes-context";
|
|
| 2 |
import { FacingDirection } from "./consts";
|
| 3 |
import { RotationPanel } from "./rotation-panel";
|
| 4 |
import { Group } from "three";
|
| 5 |
-
import { useRef } from "react";
|
| 6 |
import { useFrame, useThree } from "@react-three/fiber";
|
| 7 |
|
| 8 |
-
type
|
| 9 |
-
|
|
|
|
|
|
|
| 10 |
};
|
| 11 |
|
| 12 |
-
export const Rotator = (
|
| 13 |
const { scene } = useThree();
|
| 14 |
const { getCubes } = useCubesContext();
|
| 15 |
const isRotating = useRef(false);
|
| 16 |
-
const
|
| 17 |
-
"
|
| 18 |
-
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
useFrame((state, delta) => {
|
|
|
|
|
|
|
| 22 |
if (!isRotating.current) return;
|
| 23 |
|
| 24 |
const speed = 2;
|
| 25 |
let sign = 0;
|
| 26 |
-
switch (
|
| 27 |
case "front":
|
| 28 |
-
sign = rotatingDirection
|
| 29 |
-
rotatingGroup.
|
| 30 |
-
if (Math.abs(rotatingGroup.
|
|
|
|
| 31 |
isRotating.current = false;
|
| 32 |
-
rotatingGroup.current.rotation.z = (Math.PI / 2) * sign;
|
| 33 |
}
|
| 34 |
break;
|
| 35 |
case "back":
|
| 36 |
-
sign = rotatingDirection
|
| 37 |
-
rotatingGroup.
|
| 38 |
-
if (Math.abs(rotatingGroup.
|
|
|
|
| 39 |
isRotating.current = false;
|
| 40 |
-
rotatingGroup.current.rotation.z = (Math.PI / 2) * sign;
|
| 41 |
}
|
| 42 |
break;
|
| 43 |
case "left":
|
| 44 |
-
sign = rotatingDirection
|
| 45 |
-
rotatingGroup.
|
| 46 |
-
if (Math.abs(rotatingGroup.
|
|
|
|
| 47 |
isRotating.current = false;
|
| 48 |
-
rotatingGroup.current.rotation.x = (Math.PI / 2) * sign;
|
| 49 |
}
|
| 50 |
break;
|
| 51 |
case "right":
|
| 52 |
-
sign = rotatingDirection
|
| 53 |
-
rotatingGroup.
|
| 54 |
-
if (Math.abs(rotatingGroup.
|
|
|
|
| 55 |
isRotating.current = false;
|
| 56 |
-
rotatingGroup.current.rotation.x = (Math.PI / 2) * sign;
|
| 57 |
}
|
| 58 |
break;
|
| 59 |
case "top":
|
| 60 |
-
sign = rotatingDirection
|
| 61 |
-
rotatingGroup.
|
| 62 |
-
if (Math.abs(rotatingGroup.
|
|
|
|
| 63 |
isRotating.current = false;
|
| 64 |
-
rotatingGroup.current.rotation.y = (Math.PI / 2) * sign;
|
| 65 |
}
|
| 66 |
break;
|
| 67 |
case "bottom":
|
| 68 |
-
sign = rotatingDirection
|
| 69 |
-
rotatingGroup.
|
| 70 |
-
if (Math.abs(rotatingGroup.
|
|
|
|
| 71 |
isRotating.current = false;
|
| 72 |
-
rotatingGroup.current.rotation.y = (Math.PI / 2) * sign;
|
| 73 |
}
|
| 74 |
break;
|
| 75 |
}
|
| 76 |
|
| 77 |
-
if (
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
}
|
| 82 |
});
|
| 83 |
|
| 84 |
-
const handleClick = (
|
|
|
|
|
|
|
|
|
|
| 85 |
if (isRotating.current) return;
|
| 86 |
const cubes = getCubes(facingDirection);
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
| 92 |
isRotating.current = true;
|
| 93 |
};
|
| 94 |
|
| 95 |
return (
|
| 96 |
<>
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
</>
|
| 108 |
);
|
| 109 |
};
|
|
|
|
| 2 |
import { FacingDirection } from "./consts";
|
| 3 |
import { RotationPanel } from "./rotation-panel";
|
| 4 |
import { Group } from "three";
|
| 5 |
+
import { Fragment, useRef } from "react";
|
| 6 |
import { useFrame, useThree } from "@react-three/fiber";
|
| 7 |
|
| 8 |
+
type RotateArgs = {
|
| 9 |
+
rotatingFaceDirection: FacingDirection;
|
| 10 |
+
rotatingDirection: "clockwise" | "counter-clockwise";
|
| 11 |
+
rotatingGroup: Group;
|
| 12 |
};
|
| 13 |
|
| 14 |
+
export const Rotator = () => {
|
| 15 |
const { scene } = useThree();
|
| 16 |
const { getCubes } = useCubesContext();
|
| 17 |
const isRotating = useRef(false);
|
| 18 |
+
const rotateArgs = useRef<RotateArgs>({
|
| 19 |
+
rotatingFaceDirection: "front",
|
| 20 |
+
rotatingDirection: "clockwise",
|
| 21 |
+
rotatingGroup: new Group(),
|
| 22 |
+
});
|
| 23 |
|
| 24 |
useFrame((state, delta) => {
|
| 25 |
+
const { rotatingFaceDirection, rotatingDirection, rotatingGroup } =
|
| 26 |
+
rotateArgs.current;
|
| 27 |
if (!isRotating.current) return;
|
| 28 |
|
| 29 |
const speed = 2;
|
| 30 |
let sign = 0;
|
| 31 |
+
switch (rotatingFaceDirection) {
|
| 32 |
case "front":
|
| 33 |
+
sign = rotatingDirection === "clockwise" ? -1 : 1;
|
| 34 |
+
rotatingGroup.rotation.z += sign * delta * speed;
|
| 35 |
+
if (Math.abs(rotatingGroup.rotation.z) > Math.PI / 2) {
|
| 36 |
+
rotatingGroup.rotation.z = (Math.PI / 2) * sign;
|
| 37 |
isRotating.current = false;
|
|
|
|
| 38 |
}
|
| 39 |
break;
|
| 40 |
case "back":
|
| 41 |
+
sign = rotatingDirection === "clockwise" ? 1 : -1;
|
| 42 |
+
rotatingGroup.rotation.z += sign * delta * speed;
|
| 43 |
+
if (Math.abs(rotatingGroup.rotation.z) > Math.PI / 2) {
|
| 44 |
+
rotatingGroup.rotation.z = (Math.PI / 2) * sign;
|
| 45 |
isRotating.current = false;
|
|
|
|
| 46 |
}
|
| 47 |
break;
|
| 48 |
case "left":
|
| 49 |
+
sign = rotatingDirection === "clockwise" ? 1 : -1;
|
| 50 |
+
rotatingGroup.rotation.x += sign * delta * speed;
|
| 51 |
+
if (Math.abs(rotatingGroup.rotation.x) > Math.PI / 2) {
|
| 52 |
+
rotatingGroup.rotation.x = (Math.PI / 2) * sign;
|
| 53 |
isRotating.current = false;
|
|
|
|
| 54 |
}
|
| 55 |
break;
|
| 56 |
case "right":
|
| 57 |
+
sign = rotatingDirection === "clockwise" ? -1 : 1;
|
| 58 |
+
rotatingGroup.rotation.x += sign * delta * speed;
|
| 59 |
+
if (Math.abs(rotatingGroup.rotation.x) > Math.PI / 2) {
|
| 60 |
+
rotatingGroup.rotation.x = (Math.PI / 2) * sign;
|
| 61 |
isRotating.current = false;
|
|
|
|
| 62 |
}
|
| 63 |
break;
|
| 64 |
case "top":
|
| 65 |
+
sign = rotatingDirection === "clockwise" ? -1 : 1;
|
| 66 |
+
rotatingGroup.rotation.y += sign * delta * speed;
|
| 67 |
+
if (Math.abs(rotatingGroup.rotation.y) > Math.PI / 2) {
|
| 68 |
+
rotatingGroup.rotation.y = (Math.PI / 2) * sign;
|
| 69 |
isRotating.current = false;
|
|
|
|
| 70 |
}
|
| 71 |
break;
|
| 72 |
case "bottom":
|
| 73 |
+
sign = rotatingDirection === "clockwise" ? 1 : -1;
|
| 74 |
+
rotatingGroup.rotation.y += sign * delta * speed;
|
| 75 |
+
if (Math.abs(rotatingGroup.rotation.y) > Math.PI / 2) {
|
| 76 |
+
rotatingGroup.rotation.y = (Math.PI / 2) * sign;
|
| 77 |
isRotating.current = false;
|
|
|
|
| 78 |
}
|
| 79 |
break;
|
| 80 |
}
|
| 81 |
|
| 82 |
+
if (isRotating.current) return;
|
| 83 |
+
const children = [...rotatingGroup.children];
|
| 84 |
+
children.forEach((child) => scene.attach(child));
|
| 85 |
+
scene.remove(rotatingGroup);
|
|
|
|
| 86 |
});
|
| 87 |
|
| 88 |
+
const handleClick = (
|
| 89 |
+
facingDirection: FacingDirection,
|
| 90 |
+
direction: "clockwise" | "counter-clockwise",
|
| 91 |
+
) => {
|
| 92 |
if (isRotating.current) return;
|
| 93 |
const cubes = getCubes(facingDirection);
|
| 94 |
|
| 95 |
+
rotateArgs.current.rotatingFaceDirection = facingDirection;
|
| 96 |
+
rotateArgs.current.rotatingDirection = direction;
|
| 97 |
+
rotateArgs.current.rotatingGroup = new Group();
|
| 98 |
+
scene.add(rotateArgs.current.rotatingGroup);
|
| 99 |
+
cubes.forEach((cube) => rotateArgs.current.rotatingGroup.attach(cube));
|
| 100 |
+
|
| 101 |
isRotating.current = true;
|
| 102 |
};
|
| 103 |
|
| 104 |
return (
|
| 105 |
<>
|
| 106 |
+
{["front", "back", "left", "right", "top", "bottom"].map(
|
| 107 |
+
(facingDirection) => (
|
| 108 |
+
<Fragment key={facingDirection}>
|
| 109 |
+
<RotationPanel
|
| 110 |
+
direction="clockwise"
|
| 111 |
+
facingDirection={facingDirection as FacingDirection}
|
| 112 |
+
onClick={handleClick}
|
| 113 |
+
/>
|
| 114 |
+
<RotationPanel
|
| 115 |
+
direction="counter-clockwise"
|
| 116 |
+
facingDirection={facingDirection as FacingDirection}
|
| 117 |
+
onClick={handleClick}
|
| 118 |
+
/>
|
| 119 |
+
</Fragment>
|
| 120 |
+
),
|
| 121 |
+
)}
|
| 122 |
</>
|
| 123 |
);
|
| 124 |
};
|
src/components/rubiks-cube.tsx
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
import { CubePiece } from "./cube-piece";
|
| 2 |
-
import { FacingDirection } from "./consts";
|
| 3 |
import { Rotator } from "./rotator";
|
| 4 |
import { CubesProvider } from "@/contexts/cubes-context";
|
| 5 |
|
|
@@ -26,9 +25,7 @@ export const RubiksCube = ({ roughness }: RubiksCubeProps) => {
|
|
| 26 |
roughness={roughness}
|
| 27 |
/>
|
| 28 |
))}
|
| 29 |
-
|
| 30 |
-
<Rotator key={face} facingDirection={face as FacingDirection} />
|
| 31 |
-
))}
|
| 32 |
</CubesProvider>
|
| 33 |
);
|
| 34 |
};
|
|
|
|
| 1 |
import { CubePiece } from "./cube-piece";
|
|
|
|
| 2 |
import { Rotator } from "./rotator";
|
| 3 |
import { CubesProvider } from "@/contexts/cubes-context";
|
| 4 |
|
|
|
|
| 25 |
roughness={roughness}
|
| 26 |
/>
|
| 27 |
))}
|
| 28 |
+
<Rotator />
|
|
|
|
|
|
|
| 29 |
</CubesProvider>
|
| 30 |
);
|
| 31 |
};
|