File size: 1,497 Bytes
1afe868
 
 
 
 
 
 
a6e23da
 
ec43676
 
 
a6e23da
 
 
 
 
7cefe36
9d0c4b5
7cefe36
 
a6e23da
aa74807
 
a6e23da
 
1afe868
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { forwardRef, useEffect, useImperativeHandle, useRef } from 'react';
import { Group } from 'three';

import { RotationStep } from './consts';
import { CubePiece } from './cube-piece';
import { rotationController } from './rotation-controller';
import { Rotator, RotatorRef } from './rotator';

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]);
    }
  }
}

export type RubiksCubeRef = {
  rotate: (steps: Array<RotationStep>) => void;
};

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

export const RubiksCube = forwardRef<RubiksCubeRef, RubiksCubeProps>(({ cubeRoughness, cubeSpeed }, ref) => {
  const cubeGroupRef = useRef<Group | null>(null);
  const rotatorRef = useRef<RotatorRef | null>(null);

  useImperativeHandle(ref, () => ({
    rotate: (steps: Array<RotationStep>) => rotatorRef.current?.rotate(steps),
  }));

  useEffect(() => {
    if (cubeGroupRef.current) rotationController.setCubeGroup(cubeGroupRef.current);
  }, [cubeGroupRef]);

  return (
    <>
      <group ref={cubeGroupRef}>
        {CUBE_POSITIONS.map((position) => (
          <CubePiece key={position.join(',')} initialPosition={position} roughness={cubeRoughness} />
        ))}
      </group>
      <Rotator ref={rotatorRef} cubeSpeed={cubeSpeed} />
    </>
  );
});

RubiksCube.displayName = 'RubiksCube';