Spaces:
Sleeping
Sleeping
refactor
Browse files- src/components/consts.tsx +16 -0
- src/components/cube-piece.tsx +12 -34
- src/components/rotation-panel.tsx +13 -12
- src/components/rubiks-cube.tsx +3 -6
src/components/consts.tsx
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export type FacingDirection =
|
| 2 |
+
| "front"
|
| 3 |
+
| "back"
|
| 4 |
+
| "left"
|
| 5 |
+
| "right"
|
| 6 |
+
| "top"
|
| 7 |
+
| "bottom";
|
| 8 |
+
|
| 9 |
+
export const Rotations: Record<FacingDirection, [number, number, number]> = {
|
| 10 |
+
front: [0, 0, 0],
|
| 11 |
+
back: [0, Math.PI, 0],
|
| 12 |
+
left: [0, -Math.PI / 2, 0],
|
| 13 |
+
right: [0, Math.PI / 2, 0],
|
| 14 |
+
top: [-Math.PI / 2, 0, 0],
|
| 15 |
+
bottom: [Math.PI / 2, 0, 0],
|
| 16 |
+
};
|
src/components/cube-piece.tsx
CHANGED
|
@@ -2,6 +2,7 @@
|
|
| 2 |
|
| 3 |
import { RoundedBox } from "@react-three/drei";
|
| 4 |
import { useState } from "react";
|
|
|
|
| 5 |
|
| 6 |
// Standard Rubik's cube colors
|
| 7 |
const CUBE_COLORS = {
|
|
@@ -22,7 +23,7 @@ export const CubePiece = ({ roughness, initialPosition }: CubePieceProps) => {
|
|
| 22 |
const [x, y, z] = initialPosition;
|
| 23 |
const [position] = useState<[number, number, number]>([x, y, z]);
|
| 24 |
|
| 25 |
-
const visibleFaces = {
|
| 26 |
front: z > 0,
|
| 27 |
back: z < 0,
|
| 28 |
left: x < 0,
|
|
@@ -30,6 +31,14 @@ export const CubePiece = ({ roughness, initialPosition }: CubePieceProps) => {
|
|
| 30 |
top: y > 0,
|
| 31 |
bottom: y < 0,
|
| 32 |
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
return (
|
| 35 |
<mesh position={position}>
|
|
@@ -43,43 +52,12 @@ export const CubePiece = ({ roughness, initialPosition }: CubePieceProps) => {
|
|
| 43 |
|
| 44 |
{Object.entries(visibleFaces).map(([face, isVisible]) => {
|
| 45 |
if (!isVisible) return null;
|
| 46 |
-
|
| 47 |
const color = CUBE_COLORS[face as keyof typeof CUBE_COLORS];
|
| 48 |
-
let stickerPosition: [number, number, number] = [0, 0, 0];
|
| 49 |
-
let stickerRotation: [number, number, number] = [0, 0, 0];
|
| 50 |
-
|
| 51 |
-
switch (face) {
|
| 52 |
-
case "front":
|
| 53 |
-
stickerPosition = [0, 0, 0.48];
|
| 54 |
-
stickerRotation = [0, 0, 0];
|
| 55 |
-
break;
|
| 56 |
-
case "back":
|
| 57 |
-
stickerPosition = [0, 0, -0.48];
|
| 58 |
-
stickerRotation = [0, Math.PI, 0];
|
| 59 |
-
break;
|
| 60 |
-
case "left":
|
| 61 |
-
stickerPosition = [-0.48, 0, 0];
|
| 62 |
-
stickerRotation = [0, -Math.PI / 2, 0];
|
| 63 |
-
break;
|
| 64 |
-
case "right":
|
| 65 |
-
stickerPosition = [0.48, 0, 0];
|
| 66 |
-
stickerRotation = [0, Math.PI / 2, 0];
|
| 67 |
-
break;
|
| 68 |
-
case "top":
|
| 69 |
-
stickerPosition = [0, 0.48, 0];
|
| 70 |
-
stickerRotation = [-Math.PI / 2, 0, 0];
|
| 71 |
-
break;
|
| 72 |
-
case "bottom":
|
| 73 |
-
stickerPosition = [0, -0.48, 0];
|
| 74 |
-
stickerRotation = [Math.PI / 2, 0, 0];
|
| 75 |
-
break;
|
| 76 |
-
}
|
| 77 |
-
|
| 78 |
return (
|
| 79 |
<mesh
|
| 80 |
key={face}
|
| 81 |
-
position={
|
| 82 |
-
rotation={
|
| 83 |
>
|
| 84 |
<planeGeometry args={[0.8, 0.8]} />
|
| 85 |
<meshStandardMaterial
|
|
|
|
| 2 |
|
| 3 |
import { RoundedBox } from "@react-three/drei";
|
| 4 |
import { useState } from "react";
|
| 5 |
+
import { FacingDirection, Rotations } from "./consts";
|
| 6 |
|
| 7 |
// Standard Rubik's cube colors
|
| 8 |
const CUBE_COLORS = {
|
|
|
|
| 23 |
const [x, y, z] = initialPosition;
|
| 24 |
const [position] = useState<[number, number, number]>([x, y, z]);
|
| 25 |
|
| 26 |
+
const visibleFaces: Record<FacingDirection, boolean> = {
|
| 27 |
front: z > 0,
|
| 28 |
back: z < 0,
|
| 29 |
left: x < 0,
|
|
|
|
| 31 |
top: y > 0,
|
| 32 |
bottom: y < 0,
|
| 33 |
};
|
| 34 |
+
const positions: Record<FacingDirection, [number, number, number]> = {
|
| 35 |
+
front: [0, 0, 0.48],
|
| 36 |
+
back: [0, 0, -0.48],
|
| 37 |
+
left: [-0.48, 0, 0],
|
| 38 |
+
right: [0.48, 0, 0],
|
| 39 |
+
top: [0, 0.48, 0],
|
| 40 |
+
bottom: [0, -0.48, 0],
|
| 41 |
+
};
|
| 42 |
|
| 43 |
return (
|
| 44 |
<mesh position={position}>
|
|
|
|
| 52 |
|
| 53 |
{Object.entries(visibleFaces).map(([face, isVisible]) => {
|
| 54 |
if (!isVisible) return null;
|
|
|
|
| 55 |
const color = CUBE_COLORS[face as keyof typeof CUBE_COLORS];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
return (
|
| 57 |
<mesh
|
| 58 |
key={face}
|
| 59 |
+
position={positions[face as FacingDirection]}
|
| 60 |
+
rotation={Rotations[face as FacingDirection]}
|
| 61 |
>
|
| 62 |
<planeGeometry args={[0.8, 0.8]} />
|
| 63 |
<meshStandardMaterial
|
src/components/rotation-panel.tsx
CHANGED
|
@@ -2,16 +2,21 @@
|
|
| 2 |
|
| 3 |
import { useLoader } from "@react-three/fiber";
|
| 4 |
import { TextureLoader } from "three";
|
|
|
|
| 5 |
|
| 6 |
type RotationPanelProps = {
|
| 7 |
direction: "clockwise" | "counter-clockwise";
|
| 8 |
-
|
| 9 |
};
|
| 10 |
|
| 11 |
-
export const RotationPanel = ({
|
|
|
|
|
|
|
|
|
|
| 12 |
const clockwise = direction === "clockwise";
|
| 13 |
const texture = useLoader(TextureLoader, `/textures/${direction}.png`);
|
| 14 |
-
|
|
|
|
| 15 |
front: clockwise ? [0.5, 0, 1.01] : [-0.5, 0, 1.01],
|
| 16 |
back: clockwise ? [-0.5, 0, -1.01] : [0.5, 0, -1.01],
|
| 17 |
left: clockwise ? [-1.01, 0, 0.5] : [-1.01, 0, -0.5],
|
|
@@ -19,16 +24,12 @@ export const RotationPanel = ({ direction, face }: RotationPanelProps) => {
|
|
| 19 |
top: clockwise ? [0.5, 1.01, 0] : [-0.5, 1.01, 0],
|
| 20 |
bottom: clockwise ? [0.5, -1.01, 0] : [-0.5, -1.01, 0],
|
| 21 |
};
|
| 22 |
-
|
| 23 |
-
front: [0, 0, 0],
|
| 24 |
-
back: [0, Math.PI, 0],
|
| 25 |
-
left: [0, -Math.PI / 2, 0],
|
| 26 |
-
right: [0, Math.PI / 2, 0],
|
| 27 |
-
top: [-Math.PI / 2, 0, 0],
|
| 28 |
-
bottom: [Math.PI / 2, 0, 0],
|
| 29 |
-
};
|
| 30 |
return (
|
| 31 |
-
<mesh
|
|
|
|
|
|
|
|
|
|
| 32 |
<planeGeometry args={[0.8, 1.6]} />
|
| 33 |
<meshStandardMaterial
|
| 34 |
color={"#aaaaaa"}
|
|
|
|
| 2 |
|
| 3 |
import { useLoader } from "@react-three/fiber";
|
| 4 |
import { TextureLoader } from "three";
|
| 5 |
+
import { Rotations, FacingDirection } from "./consts";
|
| 6 |
|
| 7 |
type RotationPanelProps = {
|
| 8 |
direction: "clockwise" | "counter-clockwise";
|
| 9 |
+
facingDirection: FacingDirection;
|
| 10 |
};
|
| 11 |
|
| 12 |
+
export const RotationPanel = ({
|
| 13 |
+
direction,
|
| 14 |
+
facingDirection,
|
| 15 |
+
}: RotationPanelProps) => {
|
| 16 |
const clockwise = direction === "clockwise";
|
| 17 |
const texture = useLoader(TextureLoader, `/textures/${direction}.png`);
|
| 18 |
+
|
| 19 |
+
const position: Record<FacingDirection, [number, number, number]> = {
|
| 20 |
front: clockwise ? [0.5, 0, 1.01] : [-0.5, 0, 1.01],
|
| 21 |
back: clockwise ? [-0.5, 0, -1.01] : [0.5, 0, -1.01],
|
| 22 |
left: clockwise ? [-1.01, 0, 0.5] : [-1.01, 0, -0.5],
|
|
|
|
| 24 |
top: clockwise ? [0.5, 1.01, 0] : [-0.5, 1.01, 0],
|
| 25 |
bottom: clockwise ? [0.5, -1.01, 0] : [-0.5, -1.01, 0],
|
| 26 |
};
|
| 27 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
return (
|
| 29 |
+
<mesh
|
| 30 |
+
position={position[facingDirection]}
|
| 31 |
+
rotation={Rotations[facingDirection]}
|
| 32 |
+
>
|
| 33 |
<planeGeometry args={[0.8, 1.6]} />
|
| 34 |
<meshStandardMaterial
|
| 35 |
color={"#aaaaaa"}
|
src/components/rubiks-cube.tsx
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import { Fragment } from "react";
|
| 2 |
import { CubePiece } from "./cube-piece";
|
| 3 |
import { RotationPanel } from "./rotation-panel";
|
|
|
|
| 4 |
|
| 5 |
const CUBE_POSITIONS: Array<[number, number, number]> = [];
|
| 6 |
for (let x = -0.5; x <= 0.5; x++) {
|
|
@@ -29,15 +30,11 @@ export const RubiksCube = ({ roughness }: RubiksCubeProps) => {
|
|
| 29 |
<Fragment key={face}>
|
| 30 |
<RotationPanel
|
| 31 |
direction="clockwise"
|
| 32 |
-
|
| 33 |
-
face as "front" | "back" | "left" | "right" | "top" | "bottom"
|
| 34 |
-
}
|
| 35 |
/>
|
| 36 |
<RotationPanel
|
| 37 |
direction="counter-clockwise"
|
| 38 |
-
|
| 39 |
-
face as "front" | "back" | "left" | "right" | "top" | "bottom"
|
| 40 |
-
}
|
| 41 |
/>
|
| 42 |
</Fragment>
|
| 43 |
))}
|
|
|
|
| 1 |
import { Fragment } from "react";
|
| 2 |
import { CubePiece } from "./cube-piece";
|
| 3 |
import { RotationPanel } from "./rotation-panel";
|
| 4 |
+
import { FacingDirection } from "./consts";
|
| 5 |
|
| 6 |
const CUBE_POSITIONS: Array<[number, number, number]> = [];
|
| 7 |
for (let x = -0.5; x <= 0.5; x++) {
|
|
|
|
| 30 |
<Fragment key={face}>
|
| 31 |
<RotationPanel
|
| 32 |
direction="clockwise"
|
| 33 |
+
facingDirection={face as FacingDirection}
|
|
|
|
|
|
|
| 34 |
/>
|
| 35 |
<RotationPanel
|
| 36 |
direction="counter-clockwise"
|
| 37 |
+
facingDirection={face as FacingDirection}
|
|
|
|
|
|
|
| 38 |
/>
|
| 39 |
</Fragment>
|
| 40 |
))}
|