imwithye commited on
Commit
42ffd2e
·
1 Parent(s): fb18574

lock rotation

Browse files
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 RotatorProps = {
9
- facingDirection: FacingDirection;
 
 
10
  };
11
 
12
- export const Rotator = ({ facingDirection }: RotatorProps) => {
13
  const { scene } = useThree();
14
  const { getCubes } = useCubesContext();
15
  const isRotating = useRef(false);
16
- const rotatingDirection = useRef<"clockwise" | "counter-clockwise">(
17
- "clockwise",
18
- );
19
- const rotatingGroup = useRef<Group>(new Group());
 
20
 
21
  useFrame((state, delta) => {
 
 
22
  if (!isRotating.current) return;
23
 
24
  const speed = 2;
25
  let sign = 0;
26
- switch (facingDirection) {
27
  case "front":
28
- sign = rotatingDirection.current === "clockwise" ? -1 : 1;
29
- rotatingGroup.current.rotation.z += sign * delta * speed;
30
- if (Math.abs(rotatingGroup.current.rotation.z) > Math.PI / 2) {
 
31
  isRotating.current = false;
32
- rotatingGroup.current.rotation.z = (Math.PI / 2) * sign;
33
  }
34
  break;
35
  case "back":
36
- sign = rotatingDirection.current === "clockwise" ? 1 : -1;
37
- rotatingGroup.current.rotation.z += sign * delta * speed;
38
- if (Math.abs(rotatingGroup.current.rotation.z) > Math.PI / 2) {
 
39
  isRotating.current = false;
40
- rotatingGroup.current.rotation.z = (Math.PI / 2) * sign;
41
  }
42
  break;
43
  case "left":
44
- sign = rotatingDirection.current === "clockwise" ? 1 : -1;
45
- rotatingGroup.current.rotation.x += sign * delta * speed;
46
- if (Math.abs(rotatingGroup.current.rotation.x) > Math.PI / 2) {
 
47
  isRotating.current = false;
48
- rotatingGroup.current.rotation.x = (Math.PI / 2) * sign;
49
  }
50
  break;
51
  case "right":
52
- sign = rotatingDirection.current === "clockwise" ? -1 : 1;
53
- rotatingGroup.current.rotation.x += sign * delta * speed;
54
- if (Math.abs(rotatingGroup.current.rotation.x) > Math.PI / 2) {
 
55
  isRotating.current = false;
56
- rotatingGroup.current.rotation.x = (Math.PI / 2) * sign;
57
  }
58
  break;
59
  case "top":
60
- sign = rotatingDirection.current === "clockwise" ? -1 : 1;
61
- rotatingGroup.current.rotation.y += sign * delta * speed;
62
- if (Math.abs(rotatingGroup.current.rotation.y) > Math.PI / 2) {
 
63
  isRotating.current = false;
64
- rotatingGroup.current.rotation.y = (Math.PI / 2) * sign;
65
  }
66
  break;
67
  case "bottom":
68
- sign = rotatingDirection.current === "clockwise" ? 1 : -1;
69
- rotatingGroup.current.rotation.y += sign * delta * speed;
70
- if (Math.abs(rotatingGroup.current.rotation.y) > Math.PI / 2) {
 
71
  isRotating.current = false;
72
- rotatingGroup.current.rotation.y = (Math.PI / 2) * sign;
73
  }
74
  break;
75
  }
76
 
77
- if (!isRotating.current) {
78
- const children = [...rotatingGroup.current.children];
79
- children.forEach((child) => scene.attach(child));
80
- scene.remove(rotatingGroup.current);
81
- }
82
  });
83
 
84
- const handleClick = (direction: "clockwise" | "counter-clockwise") => {
 
 
 
85
  if (isRotating.current) return;
86
  const cubes = getCubes(facingDirection);
87
 
88
- rotatingGroup.current = new Group();
89
- scene.add(rotatingGroup.current);
90
- cubes.forEach((cube) => rotatingGroup.current.attach(cube));
91
- rotatingDirection.current = direction;
 
 
92
  isRotating.current = true;
93
  };
94
 
95
  return (
96
  <>
97
- <RotationPanel
98
- direction="clockwise"
99
- facingDirection={facingDirection}
100
- onClick={(_, direction) => handleClick(direction)}
101
- />
102
- <RotationPanel
103
- direction="counter-clockwise"
104
- facingDirection={facingDirection}
105
- onClick={(_, direction) => handleClick(direction)}
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
- {["front", "back", "left", "right", "top", "bottom"].map((face) => (
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
  };