Spaces:
Sleeping
Sleeping
File size: 5,081 Bytes
c6ef322 1afe868 20ba9a1 1afe868 20ba9a1 1afe868 20ba9a1 1afe868 20ba9a1 1afe868 20ba9a1 1afe868 20ba9a1 1afe868 20ba9a1 1afe868 20ba9a1 4c67d79 c6ef322 4c67d79 c6ef322 20ba9a1 7994c21 20ba9a1 1afe868 20ba9a1 1afe868 20ba9a1 1afe868 20ba9a1 4c67d79 1afe868 20ba9a1 1afe868 20ba9a1 4c67d79 c6ef322 20ba9a1 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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
import { Group, Mesh, Vector3 } from 'three';
import { FacingDirection, RotationStep } from './consts';
export class RotationController {
private static instance: RotationController;
private cubeGroup?: Group;
private cubes: Array<Mesh>;
private cubeSpeed: number;
private rotationSteps: Array<RotationStep>;
private rotatingStep: RotationStep | null;
private rotatingGroup: Group;
constructor() {
this.cubes = [];
this.cubeSpeed = 2;
this.rotationSteps = [];
this.rotatingStep = null;
this.rotatingGroup = new Group();
}
private rotate(step: RotationStep, group: Group, delta: number) {
let sign = 0;
let axis: 'x' | 'y' | 'z' = 'x';
switch (step.faceDirection) {
case 'front':
sign = step.direction === 'clockwise' ? -1 : 1;
axis = 'z';
break;
case 'back':
sign = step.direction === 'clockwise' ? 1 : -1;
axis = 'z';
break;
case 'left':
sign = step.direction === 'clockwise' ? 1 : -1;
axis = 'x';
break;
case 'right':
sign = step.direction === 'clockwise' ? -1 : 1;
axis = 'x';
break;
case 'top':
sign = step.direction === 'clockwise' ? -1 : 1;
axis = 'y';
break;
case 'bottom':
sign = step.direction === 'clockwise' ? 1 : -1;
axis = 'y';
break;
}
group.rotation[axis] += sign * delta * this.cubeSpeed;
if (Math.abs(group.rotation[axis]) > Math.PI / 2) {
group.rotation[axis] = (Math.PI / 2) * sign;
return true;
}
return false;
}
private getCubeFaceColorIndex(mesh: Mesh, faceDirection: FacingDirection) {
const faces = mesh.children.filter((child) => child.userData.isFace);
let axis: 'x' | 'y' | 'z' = 'x';
switch (faceDirection) {
case 'front':
case 'back':
axis = 'z';
break;
case 'right':
case 'left':
axis = 'x';
break;
case 'top':
case 'bottom':
axis = 'y';
break;
}
let maxFace: Mesh | null = null;
let maxValue = -Infinity;
for (const face of faces) {
const worldPosition = new Vector3();
face.getWorldPosition(worldPosition);
const axisValue = Math.abs(worldPosition[axis]);
if (axisValue > maxValue) {
maxValue = axisValue;
maxFace = face as Mesh;
}
}
if (!maxFace) return -1; // this should never happen
return maxFace.userData.faceColorIndex as number;
}
static getInstance() {
if (!RotationController.instance) {
RotationController.instance = new RotationController();
}
return RotationController.instance;
}
stopRotation(cb: () => void) {
this.rotationSteps = [];
const cancel = setInterval(() => {
if (!this.rotatingStep) {
clearInterval(cancel);
cb();
}
}, 50);
}
setCubeGroup(cubeGroup: Group) {
this.cubeGroup = cubeGroup;
}
addCube(cube: Mesh) {
if (!this.cubes.includes(cube)) {
this.cubes.push(cube);
}
}
getCubes(faceDirection: FacingDirection) {
switch (faceDirection) {
case 'front':
return this.cubes.filter((m) => m.position.z > 0);
case 'back':
return this.cubes.filter((m) => m.position.z < 0);
case 'right':
return this.cubes.filter((m) => m.position.x > 0);
case 'left':
return this.cubes.filter((m) => m.position.x < 0);
case 'top':
return this.cubes.filter((m) => m.position.y > 0);
case 'bottom':
return this.cubes.filter((m) => m.position.y < 0);
}
}
getStatus() {
return ['front', 'back', 'right', 'left', 'top', 'bottom'].map((f) => {
const faceDirection = f as FacingDirection;
const cubes = this.getCubes(faceDirection);
const indices = cubes.map((cube) => this.getCubeFaceColorIndex(cube, faceDirection));
return indices;
});
}
setCubeSpeed(cubeSpeed: number) {
this.cubeSpeed = cubeSpeed;
}
addRotationStep(...step: Array<RotationStep>) {
this.rotationSteps.push(...step);
}
frameCallback(state: unknown, delta: number) {
if (!this.cubeGroup) return;
if (this.rotationSteps.length === 0 && !this.rotatingStep) return;
if (!this.rotatingStep) {
const step = this.rotationSteps.shift();
if (!step) return;
this.rotatingStep = step;
const cubes = this.getCubes(step.faceDirection);
this.rotatingGroup = new Group();
this.cubeGroup?.add(this.rotatingGroup);
cubes.forEach((cube) => this.rotatingGroup.attach(cube));
}
const done = this.rotate(this.rotatingStep, this.rotatingGroup, delta);
if (done) {
this.rotatingStep = null;
const children = [...this.rotatingGroup.children];
children.forEach((child) => this.cubeGroup?.attach(child));
this.cubeGroup?.remove(this.rotatingGroup);
}
}
}
export const rotationController = RotationController.getInstance();
export const frameCallback = rotationController.frameCallback.bind(rotationController);
|