File size: 1,020 Bytes
e9d3f6f
a6e23da
aa1b508
ec43676
e9d3f6f
a6e23da
 
ec43676
 
 
a6e23da
 
 
 
 
 
aa74807
 
a6e23da
 
aa74807
e9d3f6f
a6e23da
e9d3f6f
 
 
 
 
 
aa74807
e9d3f6f
 
 
aa74807
ec43676
a6e23da
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { useRef } from "react";
import { CubePiece } from "./cube-piece";
import { Rotator } from "./rotator";
import { CubesProvider } from "@/contexts/cubes-context";
import { Group } from "three";

const CUBE_POSITIONS: Array<[number, number, number]> = [];
for (let x = -0.5; x <= 0.5; x += 1) {
  for (let y = -0.5; y <= 0.5; y += 1) {
    for (let z = -0.5; z <= 0.5; z += 1) {
      CUBE_POSITIONS.push([x, y, z]);
    }
  }
}

type RubiksCubeProps = {
  cubeRoughness: number;
  cubeSpeed: number;
};

export const RubiksCube = ({ cubeRoughness, cubeSpeed }: RubiksCubeProps) => {
  const cubeGroupRef = useRef<Group | null>(null);
  return (
    <CubesProvider cubeGroupRef={cubeGroupRef}>
      <group ref={cubeGroupRef}>
        {CUBE_POSITIONS.map((position) => (
          <CubePiece
            key={position.join(",")}
            initialPosition={position}
            roughness={cubeRoughness}
          />
        ))}
      </group>
      <Rotator cubeSpeed={cubeSpeed} />
    </CubesProvider>
  );
};