Spaces:
Running
Running
add debug points
Browse files
src/components/rotation-controller.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
-
import { Group, Mesh, Vector3 } from 'three';
|
| 2 |
|
| 3 |
-
import { FacingDirection, RotationStep } from './consts';
|
| 4 |
|
| 5 |
export class RotationController {
|
| 6 |
private static instance: RotationController;
|
|
@@ -102,7 +102,7 @@ export class RotationController {
|
|
| 102 |
if (!RotationController.instance) {
|
| 103 |
RotationController.instance = new RotationController();
|
| 104 |
if (typeof window !== 'undefined') {
|
| 105 |
-
// @ts-
|
| 106 |
window.rotationController = RotationController.instance;
|
| 107 |
}
|
| 108 |
}
|
|
@@ -157,9 +157,39 @@ export class RotationController {
|
|
| 157 |
});
|
| 158 |
}
|
| 159 |
|
| 160 |
-
|
| 161 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
const status = ['front', 'back', 'right', 'left', 'up', 'down'].map((f) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
const faceDirection = f as FacingDirection;
|
| 164 |
const cubes = this.getCubes(faceDirection);
|
| 165 |
const indices = cubes.map((cube) => this.getCubeFaceData(cube, faceDirection)).sort((a, b) => a.rank - b.rank);
|
|
@@ -172,17 +202,18 @@ export class RotationController {
|
|
| 172 |
);
|
| 173 |
}
|
| 174 |
}
|
| 175 |
-
return indices.map((i) => i.face.userData.faceColorIndex);
|
| 176 |
});
|
| 177 |
-
console.log('Python Gym Step Code:');
|
| 178 |
console.log(rotationsPy.join('\n'));
|
| 179 |
-
return status;
|
| 180 |
}
|
| 181 |
|
| 182 |
setCubeSpeed(cubeSpeed: number) {
|
| 183 |
this.cubeSpeed = cubeSpeed;
|
| 184 |
}
|
| 185 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
addRotationStep(...step: Array<RotationStep>) {
|
| 187 |
this.rotationSteps.push(...step);
|
| 188 |
}
|
|
|
|
| 1 |
+
import { Group, Mesh, MeshStandardMaterial, Vector3 } from 'three';
|
| 2 |
|
| 3 |
+
import { Actions, Color2Index, FacingDirection, RotationStep } from './consts';
|
| 4 |
|
| 5 |
export class RotationController {
|
| 6 |
private static instance: RotationController;
|
|
|
|
| 102 |
if (!RotationController.instance) {
|
| 103 |
RotationController.instance = new RotationController();
|
| 104 |
if (typeof window !== 'undefined') {
|
| 105 |
+
// @ts-expect-error window is defined
|
| 106 |
window.rotationController = RotationController.instance;
|
| 107 |
}
|
| 108 |
}
|
|
|
|
| 157 |
});
|
| 158 |
}
|
| 159 |
|
| 160 |
+
setState(stateArray: Array<Array<number>>) {
|
| 161 |
+
const colors = ['', '', '', '', '', ''];
|
| 162 |
+
Object.entries(Color2Index).forEach(([c, i]) => {
|
| 163 |
+
colors[i] = c;
|
| 164 |
+
});
|
| 165 |
+
['front', 'back', 'right', 'left', 'up', 'down'].forEach((f, idx1) => {
|
| 166 |
+
const faceDirection = f as FacingDirection;
|
| 167 |
+
const cubes = this.getCubes(faceDirection);
|
| 168 |
+
const indices = cubes.map((cube) => this.getCubeFaceData(cube, faceDirection)).sort((a, b) => a.rank - b.rank);
|
| 169 |
+
indices.forEach((i, idx2) => {
|
| 170 |
+
const colorIdx = stateArray[idx1][idx2];
|
| 171 |
+
i.face.userData.faceColor = colors[colorIdx];
|
| 172 |
+
i.face.userData.faceColorIndex = colorIdx;
|
| 173 |
+
const material = i.face.material as MeshStandardMaterial;
|
| 174 |
+
material.color.set(colors[colorIdx]);
|
| 175 |
+
});
|
| 176 |
+
});
|
| 177 |
+
this.initializeFaces();
|
| 178 |
+
}
|
| 179 |
+
|
| 180 |
+
getState() {
|
| 181 |
const status = ['front', 'back', 'right', 'left', 'up', 'down'].map((f) => {
|
| 182 |
+
const faceDirection = f as FacingDirection;
|
| 183 |
+
const cubes = this.getCubes(faceDirection);
|
| 184 |
+
const indices = cubes.map((cube) => this.getCubeFaceData(cube, faceDirection)).sort((a, b) => a.rank - b.rank);
|
| 185 |
+
return indices.map((i) => i.face.userData.faceColorIndex);
|
| 186 |
+
});
|
| 187 |
+
return status;
|
| 188 |
+
}
|
| 189 |
+
|
| 190 |
+
_printStateTransitions() {
|
| 191 |
+
const rotationsPy: Array<string> = [];
|
| 192 |
+
['front', 'back', 'right', 'left', 'up', 'down'].forEach((f) => {
|
| 193 |
const faceDirection = f as FacingDirection;
|
| 194 |
const cubes = this.getCubes(faceDirection);
|
| 195 |
const indices = cubes.map((cube) => this.getCubeFaceData(cube, faceDirection)).sort((a, b) => a.rank - b.rank);
|
|
|
|
| 202 |
);
|
| 203 |
}
|
| 204 |
}
|
|
|
|
| 205 |
});
|
|
|
|
| 206 |
console.log(rotationsPy.join('\n'));
|
|
|
|
| 207 |
}
|
| 208 |
|
| 209 |
setCubeSpeed(cubeSpeed: number) {
|
| 210 |
this.cubeSpeed = cubeSpeed;
|
| 211 |
}
|
| 212 |
|
| 213 |
+
addRotationStepCode(...codes: Array<number>) {
|
| 214 |
+
this.addRotationStep(...codes.map((code) => Actions[code]));
|
| 215 |
+
}
|
| 216 |
+
|
| 217 |
addRotationStep(...step: Array<RotationStep>) {
|
| 218 |
this.rotationSteps.push(...step);
|
| 219 |
}
|
src/components/rubiks-cube.tsx
CHANGED
|
@@ -37,6 +37,14 @@ export const RubiksCube = forwardRef<RubiksCubeRef, RubiksCubeProps>(({ cubeRoug
|
|
| 37 |
cubePieceRefs.current.forEach((cubePieceRef) => {
|
| 38 |
cubePieceRef.resetPosition();
|
| 39 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
rotationController.initializeFaces();
|
| 41 |
});
|
| 42 |
},
|
|
|
|
| 37 |
cubePieceRefs.current.forEach((cubePieceRef) => {
|
| 38 |
cubePieceRef.resetPosition();
|
| 39 |
});
|
| 40 |
+
rotationController.setState([
|
| 41 |
+
[0, 0, 0, 0],
|
| 42 |
+
[1, 1, 1, 1],
|
| 43 |
+
[2, 2, 2, 2],
|
| 44 |
+
[3, 3, 3, 3],
|
| 45 |
+
[4, 4, 4, 4],
|
| 46 |
+
[5, 5, 5, 5],
|
| 47 |
+
]);
|
| 48 |
rotationController.initializeFaces();
|
| 49 |
});
|
| 50 |
},
|
src/components/state-modal.tsx
CHANGED
|
@@ -8,7 +8,7 @@ import { forwardRef, useImperativeHandle, useState } from 'react';
|
|
| 8 |
import { Index2Color } from './consts';
|
| 9 |
|
| 10 |
export type StateModalRef = {
|
| 11 |
-
open: (
|
| 12 |
};
|
| 13 |
|
| 14 |
export const StateModal = forwardRef<StateModalRef, unknown>((_, ref) => {
|
|
|
|
| 8 |
import { Index2Color } from './consts';
|
| 9 |
|
| 10 |
export type StateModalRef = {
|
| 11 |
+
open: (state: Array<Array<number>>) => void;
|
| 12 |
};
|
| 13 |
|
| 14 |
export const StateModal = forwardRef<StateModalRef, unknown>((_, ref) => {
|
src/components/ui-controls.tsx
CHANGED
|
@@ -36,8 +36,8 @@ export const UIControls = () => {
|
|
| 36 |
};
|
| 37 |
|
| 38 |
const showState = () => {
|
| 39 |
-
const
|
| 40 |
-
stateModalRef.current?.open(
|
| 41 |
};
|
| 42 |
|
| 43 |
const solve = () => {
|
|
|
|
| 36 |
};
|
| 37 |
|
| 38 |
const showState = () => {
|
| 39 |
+
const state = rotationController.getState();
|
| 40 |
+
stateModalRef.current?.open(state);
|
| 41 |
};
|
| 42 |
|
| 43 |
const solve = () => {
|