imwithye commited on
Commit
f68ade5
·
1 Parent(s): e98fc97

add rotation

Browse files
Files changed (1) hide show
  1. src/components/rotator.tsx +78 -2
src/components/rotator.tsx CHANGED
@@ -1,20 +1,96 @@
1
  import { useCubesContext } from "@/contexts/cubes-context";
2
  import { FacingDirection } from "./consts";
3
  import { RotationPanel } from "./rotation-panel";
 
 
 
4
 
5
  type RotatorProps = {
6
  facingDirection: FacingDirection;
7
  };
8
 
9
  export const Rotator = ({ facingDirection }: RotatorProps) => {
 
10
  const { getCubes } = useCubesContext();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
  const handleClick = (direction: "clockwise" | "counter-clockwise") => {
 
13
  const cubes = getCubes(facingDirection);
 
14
  cubes.forEach((cube) => {
15
- console.log(cube);
16
  });
17
- console.log(cubes.length, direction);
 
18
  };
19
 
20
  return (
 
1
  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
+ scene.add(rotatingGroup.current);
21
+
22
+ useFrame((state, delta) => {
23
+ if (!isRotating.current) return;
24
+
25
+ const speed = 2;
26
+ let sign = 0;
27
+ switch (facingDirection) {
28
+ case "front":
29
+ sign = rotatingDirection.current === "clockwise" ? -1 : 1;
30
+ rotatingGroup.current.rotation.z += sign * delta * speed;
31
+ if (Math.abs(rotatingGroup.current.rotation.z) > Math.PI / 2) {
32
+ isRotating.current = false;
33
+ rotatingGroup.current.rotation.z = (Math.PI / 2) * sign;
34
+ }
35
+ break;
36
+ case "back":
37
+ sign = rotatingDirection.current === "clockwise" ? 1 : 1;
38
+ rotatingGroup.current.rotation.z += sign * delta * speed;
39
+ if (Math.abs(rotatingGroup.current.rotation.z) > Math.PI / 2) {
40
+ isRotating.current = false;
41
+ rotatingGroup.current.rotation.z = (Math.PI / 2) * sign;
42
+ }
43
+ break;
44
+ case "left":
45
+ sign = rotatingDirection.current === "clockwise" ? 1 : -1;
46
+ rotatingGroup.current.rotation.x += sign * delta * speed;
47
+ if (Math.abs(rotatingGroup.current.rotation.x) > Math.PI / 2) {
48
+ isRotating.current = false;
49
+ rotatingGroup.current.rotation.x = (Math.PI / 2) * sign;
50
+ }
51
+ break;
52
+ case "right":
53
+ sign = rotatingDirection.current === "clockwise" ? -1 : 1;
54
+ rotatingGroup.current.rotation.x += sign * delta * speed;
55
+ if (Math.abs(rotatingGroup.current.rotation.x) > Math.PI / 2) {
56
+ isRotating.current = false;
57
+ rotatingGroup.current.rotation.x = (Math.PI / 2) * sign;
58
+ }
59
+ break;
60
+ case "top":
61
+ sign = rotatingDirection.current === "clockwise" ? -1 : 1;
62
+ rotatingGroup.current.rotation.y += sign * delta * speed;
63
+ if (Math.abs(rotatingGroup.current.rotation.y) > Math.PI / 2) {
64
+ isRotating.current = false;
65
+ rotatingGroup.current.rotation.y = (Math.PI / 2) * sign;
66
+ }
67
+ break;
68
+ case "bottom":
69
+ sign = rotatingDirection.current === "clockwise" ? 1 : -1;
70
+ rotatingGroup.current.rotation.y += sign * delta * speed;
71
+ if (Math.abs(rotatingGroup.current.rotation.y) > Math.PI / 2) {
72
+ isRotating.current = false;
73
+ rotatingGroup.current.rotation.y = (Math.PI / 2) * sign;
74
+ }
75
+ break;
76
+ }
77
+
78
+ if (!isRotating.current) {
79
+ rotatingGroup.current.children.forEach((child) => {
80
+ scene.attach(child);
81
+ });
82
+ }
83
+ });
84
 
85
  const handleClick = (direction: "clockwise" | "counter-clockwise") => {
86
+ if (isRotating.current) return;
87
  const cubes = getCubes(facingDirection);
88
+
89
  cubes.forEach((cube) => {
90
+ rotatingGroup.current.attach(cube);
91
  });
92
+ rotatingDirection.current = direction;
93
+ isRotating.current = true;
94
  };
95
 
96
  return (