Spaces:
Running
Running
refactor code
Browse files- src/components/rotator.tsx +8 -8
- src/components/rubiks-cube.tsx +13 -8
- src/contexts/cubes-context.tsx +11 -3
src/components/rotator.tsx
CHANGED
|
@@ -3,7 +3,7 @@ import { FacingDirection } from "./consts";
|
|
| 3 |
import { RotationPanel } from "./rotation-panel";
|
| 4 |
import { Group } from "three";
|
| 5 |
import { Fragment, useRef } from "react";
|
| 6 |
-
import { useFrame
|
| 7 |
|
| 8 |
type RotateArgs = {
|
| 9 |
rotatingFaceDirection: FacingDirection;
|
|
@@ -12,8 +12,7 @@ type RotateArgs = {
|
|
| 12 |
};
|
| 13 |
|
| 14 |
export const Rotator = () => {
|
| 15 |
-
const {
|
| 16 |
-
const { getCubes } = useCubesContext();
|
| 17 |
const isRotating = useRef(false);
|
| 18 |
const rotateArgs = useRef<RotateArgs>({
|
| 19 |
rotatingFaceDirection: "front",
|
|
@@ -24,7 +23,8 @@ export const Rotator = () => {
|
|
| 24 |
useFrame((state, delta) => {
|
| 25 |
const { rotatingFaceDirection, rotatingDirection, rotatingGroup } =
|
| 26 |
rotateArgs.current;
|
| 27 |
-
|
|
|
|
| 28 |
|
| 29 |
const speed = 2;
|
| 30 |
let sign = 0;
|
|
@@ -81,21 +81,21 @@ export const Rotator = () => {
|
|
| 81 |
|
| 82 |
if (isRotating.current) return;
|
| 83 |
const children = [...rotatingGroup.children];
|
| 84 |
-
children.forEach((child) =>
|
| 85 |
-
|
| 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 |
-
|
| 99 |
cubes.forEach((cube) => rotateArgs.current.rotatingGroup.attach(cube));
|
| 100 |
|
| 101 |
isRotating.current = true;
|
|
|
|
| 3 |
import { RotationPanel } from "./rotation-panel";
|
| 4 |
import { Group } from "three";
|
| 5 |
import { Fragment, useRef } from "react";
|
| 6 |
+
import { useFrame } from "@react-three/fiber";
|
| 7 |
|
| 8 |
type RotateArgs = {
|
| 9 |
rotatingFaceDirection: FacingDirection;
|
|
|
|
| 12 |
};
|
| 13 |
|
| 14 |
export const Rotator = () => {
|
| 15 |
+
const { getCubes, cubeGroupRef } = useCubesContext();
|
|
|
|
| 16 |
const isRotating = useRef(false);
|
| 17 |
const rotateArgs = useRef<RotateArgs>({
|
| 18 |
rotatingFaceDirection: "front",
|
|
|
|
| 23 |
useFrame((state, delta) => {
|
| 24 |
const { rotatingFaceDirection, rotatingDirection, rotatingGroup } =
|
| 25 |
rotateArgs.current;
|
| 26 |
+
const cubeGroup = cubeGroupRef.current;
|
| 27 |
+
if (!isRotating.current || !cubeGroup) return;
|
| 28 |
|
| 29 |
const speed = 2;
|
| 30 |
let sign = 0;
|
|
|
|
| 81 |
|
| 82 |
if (isRotating.current) return;
|
| 83 |
const children = [...rotatingGroup.children];
|
| 84 |
+
children.forEach((child) => cubeGroup.attach(child));
|
| 85 |
+
cubeGroup.remove(rotatingGroup);
|
| 86 |
});
|
| 87 |
|
| 88 |
const handleClick = (
|
| 89 |
facingDirection: FacingDirection,
|
| 90 |
direction: "clockwise" | "counter-clockwise",
|
| 91 |
) => {
|
| 92 |
+
if (isRotating.current || !cubeGroupRef.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 |
+
cubeGroupRef.current.add(rotateArgs.current.rotatingGroup);
|
| 99 |
cubes.forEach((cube) => rotateArgs.current.rotatingGroup.attach(cube));
|
| 100 |
|
| 101 |
isRotating.current = true;
|
src/components/rubiks-cube.tsx
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
|
|
| 1 |
import { CubePiece } from "./cube-piece";
|
| 2 |
import { Rotator } from "./rotator";
|
| 3 |
import { CubesProvider } from "@/contexts/cubes-context";
|
|
|
|
| 4 |
|
| 5 |
const CUBE_POSITIONS: Array<[number, number, number]> = [];
|
| 6 |
for (let x = -0.5; x <= 0.5; x += 1) {
|
|
@@ -16,15 +18,18 @@ type RubiksCubeProps = {
|
|
| 16 |
};
|
| 17 |
|
| 18 |
export const RubiksCube = ({ roughness }: RubiksCubeProps) => {
|
|
|
|
| 19 |
return (
|
| 20 |
-
<CubesProvider>
|
| 21 |
-
{
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
<Rotator />
|
| 29 |
</CubesProvider>
|
| 30 |
);
|
|
|
|
| 1 |
+
import { useRef } from "react";
|
| 2 |
import { CubePiece } from "./cube-piece";
|
| 3 |
import { Rotator } from "./rotator";
|
| 4 |
import { CubesProvider } from "@/contexts/cubes-context";
|
| 5 |
+
import { Group } from "three";
|
| 6 |
|
| 7 |
const CUBE_POSITIONS: Array<[number, number, number]> = [];
|
| 8 |
for (let x = -0.5; x <= 0.5; x += 1) {
|
|
|
|
| 18 |
};
|
| 19 |
|
| 20 |
export const RubiksCube = ({ roughness }: RubiksCubeProps) => {
|
| 21 |
+
const cubeGroupRef = useRef<Group | null>(null);
|
| 22 |
return (
|
| 23 |
+
<CubesProvider cubeGroupRef={cubeGroupRef}>
|
| 24 |
+
<group ref={cubeGroupRef}>
|
| 25 |
+
{CUBE_POSITIONS.map((position) => (
|
| 26 |
+
<CubePiece
|
| 27 |
+
key={position.join(",")}
|
| 28 |
+
initialPosition={position}
|
| 29 |
+
roughness={roughness}
|
| 30 |
+
/>
|
| 31 |
+
))}
|
| 32 |
+
</group>
|
| 33 |
<Rotator />
|
| 34 |
</CubesProvider>
|
| 35 |
);
|
src/contexts/cubes-context.tsx
CHANGED
|
@@ -2,23 +2,31 @@
|
|
| 2 |
|
| 3 |
import { FacingDirection } from "@/components/consts";
|
| 4 |
import { createContext, RefObject, useContext, useRef } from "react";
|
| 5 |
-
import { Mesh } from "three";
|
| 6 |
|
| 7 |
export type CubeMeshRef = RefObject<Mesh | null>;
|
| 8 |
|
| 9 |
type CubesContextType = {
|
|
|
|
| 10 |
addCube: (cubeMeshRef: CubeMeshRef) => void;
|
| 11 |
getCubes: (faceDirection: FacingDirection) => Mesh[];
|
| 12 |
};
|
| 13 |
|
| 14 |
const CubesContext = createContext<CubesContextType>({
|
|
|
|
| 15 |
addCube: () => {},
|
| 16 |
getCubes: () => [],
|
| 17 |
});
|
| 18 |
|
| 19 |
export const useCubesContext = () => useContext(CubesContext);
|
| 20 |
|
| 21 |
-
export const CubesProvider = ({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
const cubes = useRef<CubeMeshRef[]>([]);
|
| 23 |
|
| 24 |
const addCube = (cubeMeshRef: CubeMeshRef) => {
|
|
@@ -48,7 +56,7 @@ export const CubesProvider = ({ children }: { children: React.ReactNode }) => {
|
|
| 48 |
};
|
| 49 |
|
| 50 |
return (
|
| 51 |
-
<CubesContext.Provider value={{ addCube, getCubes }}>
|
| 52 |
{children}
|
| 53 |
</CubesContext.Provider>
|
| 54 |
);
|
|
|
|
| 2 |
|
| 3 |
import { FacingDirection } from "@/components/consts";
|
| 4 |
import { createContext, RefObject, useContext, useRef } from "react";
|
| 5 |
+
import { Group, Mesh } from "three";
|
| 6 |
|
| 7 |
export type CubeMeshRef = RefObject<Mesh | null>;
|
| 8 |
|
| 9 |
type CubesContextType = {
|
| 10 |
+
cubeGroupRef: RefObject<Group | null>;
|
| 11 |
addCube: (cubeMeshRef: CubeMeshRef) => void;
|
| 12 |
getCubes: (faceDirection: FacingDirection) => Mesh[];
|
| 13 |
};
|
| 14 |
|
| 15 |
const CubesContext = createContext<CubesContextType>({
|
| 16 |
+
cubeGroupRef: { current: null },
|
| 17 |
addCube: () => {},
|
| 18 |
getCubes: () => [],
|
| 19 |
});
|
| 20 |
|
| 21 |
export const useCubesContext = () => useContext(CubesContext);
|
| 22 |
|
| 23 |
+
export const CubesProvider = ({
|
| 24 |
+
cubeGroupRef,
|
| 25 |
+
children,
|
| 26 |
+
}: {
|
| 27 |
+
cubeGroupRef: RefObject<Group | null>;
|
| 28 |
+
children: React.ReactNode;
|
| 29 |
+
}) => {
|
| 30 |
const cubes = useRef<CubeMeshRef[]>([]);
|
| 31 |
|
| 32 |
const addCube = (cubeMeshRef: CubeMeshRef) => {
|
|
|
|
| 56 |
};
|
| 57 |
|
| 58 |
return (
|
| 59 |
+
<CubesContext.Provider value={{ cubeGroupRef, addCube, getCubes }}>
|
| 60 |
{children}
|
| 61 |
</CubesContext.Provider>
|
| 62 |
);
|