@houstonp/rubiks-cube 1.1.1 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.prettierrc +7 -0
- package/README.md +35 -27
- package/index.js +201 -208
- package/package.json +20 -20
- package/src/cube/cube.js +194 -0
- package/src/cube/cubeRotation.js +60 -0
- package/src/cube/cubeState.js +191 -0
- package/src/{materials.js → threejs/materials.js} +8 -8
- package/src/threejs/pieces.js +103 -0
- package/src/{stickers.js → threejs/stickers.js} +4 -4
- package/src/utils/debouncer.js +7 -0
- package/src/utils/rotation.js +53 -0
- package/src/animation.js +0 -168
- package/src/center.js +0 -23
- package/src/corner.js +0 -45
- package/src/cube.js +0 -333
- package/src/cubeState.js +0 -128
- package/src/edge.js +0 -36
- package/src/rotation.js +0 -52
package/src/animation.js
DELETED
|
@@ -1,168 +0,0 @@
|
|
|
1
|
-
import * as THREE from "three";
|
|
2
|
-
import Cube from "./cube";
|
|
3
|
-
|
|
4
|
-
export class AnimationQueue {
|
|
5
|
-
constructor(type = "exponential", factor = 1.3) {
|
|
6
|
-
/** @type {Animation[]} */
|
|
7
|
-
this.queue = [];
|
|
8
|
-
/** @type {Animation | undefined} */
|
|
9
|
-
this.currentAnimation = undefined;
|
|
10
|
-
/** @type {{type: "fast-forward" | "exponential", factor: number}} */
|
|
11
|
-
this.type = type;
|
|
12
|
-
this.factor = factor;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
clear() {
|
|
16
|
-
this.queue = [];
|
|
17
|
-
if (this.currentAnimation) {
|
|
18
|
-
this.currentAnimation.fastforward = true;
|
|
19
|
-
this.currentAnimation.update();
|
|
20
|
-
this.currentAnimation = undefined;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* @param {Animation} animation
|
|
26
|
-
*/
|
|
27
|
-
add(animation) {
|
|
28
|
-
if (this.type === "fast-forward") {
|
|
29
|
-
this.fastForward();
|
|
30
|
-
} else if (this.type === "exponential") {
|
|
31
|
-
this.exponential();
|
|
32
|
-
}
|
|
33
|
-
this.queue.push(animation);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/* exponentially increases the animation speed with the depth of the queue */
|
|
37
|
-
exponential() {
|
|
38
|
-
let animations = [];
|
|
39
|
-
if (this.currentAnimation) animations.push(this.currentAnimation);
|
|
40
|
-
animations = animations.concat(this.queue);
|
|
41
|
-
for (let i = 0; i < animations.length; i++) {
|
|
42
|
-
animations[i].setSpeed(this.factor ** (animations.length - i - 1));
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/* instantly completes any queued animations */
|
|
47
|
-
fastForward() {
|
|
48
|
-
if (this.currentAnimation) {
|
|
49
|
-
this.currentAnimation.setFastForward();
|
|
50
|
-
}
|
|
51
|
-
if (this.queue.length) {
|
|
52
|
-
for (const a of this.queue) {
|
|
53
|
-
a.setFastForward();
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
update() {
|
|
59
|
-
if (
|
|
60
|
-
this.currentAnimation === undefined ||
|
|
61
|
-
this.currentAnimation.finished()
|
|
62
|
-
) {
|
|
63
|
-
this.currentAnimation = this.queue.shift();
|
|
64
|
-
}
|
|
65
|
-
if (this.currentAnimation === undefined) return;
|
|
66
|
-
this.currentAnimation.update();
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
finished() {
|
|
70
|
-
return this.currentAnimation?.finished();
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
*
|
|
75
|
-
* @returns {THREE.Group | undefined}
|
|
76
|
-
*/
|
|
77
|
-
getAnimationGroup() {
|
|
78
|
-
return this.currentAnimation?.getGroup();
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
export class Animation {
|
|
83
|
-
/**
|
|
84
|
-
*
|
|
85
|
-
* @param {Cube} cube
|
|
86
|
-
* @param {"x"|"y"|"z"} axis
|
|
87
|
-
* @param {(-1|0|1)[]} layers
|
|
88
|
-
* @param {1|-1|2|-2} direction
|
|
89
|
-
* @param {number} duration milliseconds
|
|
90
|
-
*/
|
|
91
|
-
constructor(cube, axis, layers, direction, duration) {
|
|
92
|
-
this._cube = cube;
|
|
93
|
-
this._axis = axis;
|
|
94
|
-
this._layers = layers;
|
|
95
|
-
this._direction = direction;
|
|
96
|
-
this._duration = duration;
|
|
97
|
-
this._layerGroup = new THREE.Group();
|
|
98
|
-
this._finished = false;
|
|
99
|
-
this._lastUpdate = undefined;
|
|
100
|
-
this._totalRotation = 0;
|
|
101
|
-
this.fastforward = false;
|
|
102
|
-
this.speed = 1;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
setFastForward(value = true) {
|
|
106
|
-
this.fastforward = value;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
setSpeed(value = 1) {
|
|
110
|
-
this.speed = value;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
init() {
|
|
114
|
-
this._lastUpdate = Date.now();
|
|
115
|
-
const layerObjects = this._cube.getRotationLayer(this._axis, this._layers);
|
|
116
|
-
this._layerGroup.add(...layerObjects);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
teardown() {
|
|
120
|
-
this._finished = true;
|
|
121
|
-
this._layerGroup.children.forEach((piece) => {
|
|
122
|
-
piece.getWorldPosition(piece.position);
|
|
123
|
-
piece.getWorldQuaternion(piece.quaternion);
|
|
124
|
-
piece.userData.position.x = Math.round(piece.position.x);
|
|
125
|
-
piece.userData.position.y = Math.round(piece.position.y);
|
|
126
|
-
piece.userData.position.z = Math.round(piece.position.z);
|
|
127
|
-
piece.userData.rotation.x = piece.rotation.x;
|
|
128
|
-
piece.userData.rotation.y = piece.rotation.y;
|
|
129
|
-
piece.userData.rotation.z = piece.rotation.z;
|
|
130
|
-
});
|
|
131
|
-
this._cube.group.add(...this._layerGroup.children);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
update() {
|
|
135
|
-
if (this._lastUpdate === undefined) {
|
|
136
|
-
this.init();
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
var interval = (Date.now() - this._lastUpdate) * this.speed;
|
|
140
|
-
this._lastUpdate = Date.now();
|
|
141
|
-
if (this.fastforward || interval + this._totalRotation > this._duration) {
|
|
142
|
-
interval = this._duration - this._totalRotation;
|
|
143
|
-
}
|
|
144
|
-
const rotationIncrement =
|
|
145
|
-
(Math.abs(this._direction) * ((interval / this._duration) * Math.PI)) / 2;
|
|
146
|
-
this._totalRotation += interval;
|
|
147
|
-
this._layerGroup.rotateOnWorldAxis(
|
|
148
|
-
new THREE.Vector3(
|
|
149
|
-
this._axis === "x" ? this._direction : 0,
|
|
150
|
-
this._axis === "y" ? this._direction : 0,
|
|
151
|
-
this._axis === "z" ? this._direction : 0
|
|
152
|
-
).normalize(),
|
|
153
|
-
rotationIncrement
|
|
154
|
-
);
|
|
155
|
-
|
|
156
|
-
if (this._totalRotation >= this._duration) {
|
|
157
|
-
this.teardown();
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
getGroup() {
|
|
162
|
-
return this._layerGroup;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
finished() {
|
|
166
|
-
return this._finished;
|
|
167
|
-
}
|
|
168
|
-
}
|
package/src/center.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import * as THREE from "three";
|
|
2
|
-
/**
|
|
3
|
-
* @param {THREE.Geometry} sticker
|
|
4
|
-
* @param {THREE.Material} frontMaterial
|
|
5
|
-
* @param {THREE.Material} topMaterial
|
|
6
|
-
* @param {THREE.Material} coreMaterial
|
|
7
|
-
* @returns {{group: THREE.Group, frontSticker: THREE.Mesh, topSticker:THREE.Mesh}}
|
|
8
|
-
*/
|
|
9
|
-
export default function newCenter(sticker, frontMaterial, coreMaterial) {
|
|
10
|
-
const group = new THREE.Group();
|
|
11
|
-
const boxGeom = new THREE.BoxGeometry(1, 1, 1);
|
|
12
|
-
const boxMesh = new THREE.Mesh(boxGeom, coreMaterial);
|
|
13
|
-
boxMesh.userData = { type: "piece" };
|
|
14
|
-
group.add(boxMesh);
|
|
15
|
-
|
|
16
|
-
const frontSticker = new THREE.Mesh(sticker, frontMaterial);
|
|
17
|
-
frontSticker.userData = { type: "sticker" };
|
|
18
|
-
frontSticker.position.set(0, 0, 0.5);
|
|
19
|
-
frontSticker.rotation.set(0, 0, 0);
|
|
20
|
-
group.add(frontSticker);
|
|
21
|
-
|
|
22
|
-
return { group, frontSticker };
|
|
23
|
-
}
|
package/src/corner.js
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import * as THREE from "three";
|
|
2
|
-
/**
|
|
3
|
-
* @param {THREE.Geometry} sticker
|
|
4
|
-
* @param {THREE.Material} frontMaterial
|
|
5
|
-
* @param {THREE.Material} rightMaterial
|
|
6
|
-
* @param {THREE.Material} topMaterial
|
|
7
|
-
* @param {THREE.Material} coreMaterial
|
|
8
|
-
* @returns {{group: THREE.Group, frontSticker: THREE.Mesh, rightSticker:THREE.Mesh, topSticker:THREE.Mesh}}
|
|
9
|
-
*/
|
|
10
|
-
export default function newCorner(
|
|
11
|
-
sticker,
|
|
12
|
-
frontMaterial,
|
|
13
|
-
rightMaterial,
|
|
14
|
-
topMaterial,
|
|
15
|
-
coreMaterial
|
|
16
|
-
) {
|
|
17
|
-
const group = new THREE.Group();
|
|
18
|
-
const boxGeom = new THREE.BoxGeometry(1, 1, 1);
|
|
19
|
-
const boxMesh = new THREE.Mesh(boxGeom, coreMaterial);
|
|
20
|
-
boxMesh.userData = { type: "piece" };
|
|
21
|
-
group.add(boxMesh);
|
|
22
|
-
|
|
23
|
-
// front
|
|
24
|
-
const frontSticker = new THREE.Mesh(sticker, frontMaterial);
|
|
25
|
-
frontSticker.userData = { type: "sticker" };
|
|
26
|
-
frontSticker.position.set(0, 0, 0.5);
|
|
27
|
-
frontSticker.rotation.set(0, 0, 0);
|
|
28
|
-
group.add(frontSticker);
|
|
29
|
-
|
|
30
|
-
//right
|
|
31
|
-
const rightSticker = new THREE.Mesh(sticker, rightMaterial);
|
|
32
|
-
rightSticker.userData = { type: "sticker" };
|
|
33
|
-
rightSticker.position.set(0.5, 0, 0);
|
|
34
|
-
rightSticker.rotation.set(Math.PI / 2, Math.PI / 2, 0);
|
|
35
|
-
group.add(rightSticker);
|
|
36
|
-
|
|
37
|
-
//white/yellow
|
|
38
|
-
const topSticker = new THREE.Mesh(sticker, topMaterial);
|
|
39
|
-
topSticker.userData = { type: "sticker" };
|
|
40
|
-
topSticker.position.set(0, 0.5, 0);
|
|
41
|
-
topSticker.rotation.set(-Math.PI / 2, 0, -Math.PI / 2);
|
|
42
|
-
group.add(topSticker);
|
|
43
|
-
|
|
44
|
-
return { group, frontSticker, rightSticker, topSticker };
|
|
45
|
-
}
|
package/src/cube.js
DELETED
|
@@ -1,333 +0,0 @@
|
|
|
1
|
-
import * as THREE from "three";
|
|
2
|
-
import Materials from "./materials";
|
|
3
|
-
import Stickers from "./stickers";
|
|
4
|
-
import newCorner from "./corner";
|
|
5
|
-
import newEdge from "./edge";
|
|
6
|
-
import newCenter from "./center";
|
|
7
|
-
import { newCubeState } from "./cubeState";
|
|
8
|
-
|
|
9
|
-
export default class Cube {
|
|
10
|
-
constructor() {
|
|
11
|
-
this.group = new THREE.Group();
|
|
12
|
-
const core = this.createCore();
|
|
13
|
-
core.userData = {
|
|
14
|
-
position: { x: 0, y: 0, z: 0 },
|
|
15
|
-
rotation: { x: 0, y: 0, z: 0 },
|
|
16
|
-
initialPosition: { x: 0, y: 0, z: 0 },
|
|
17
|
-
initialRotation: { x: 0, y: 0, z: 0 },
|
|
18
|
-
type: "core",
|
|
19
|
-
};
|
|
20
|
-
this.group.add(core);
|
|
21
|
-
|
|
22
|
-
for (const state of newCubeState()) {
|
|
23
|
-
const piece = this.createPiece(state.position, state.type);
|
|
24
|
-
piece.position.set(
|
|
25
|
-
state.position.x * 1.04,
|
|
26
|
-
state.position.y * 1.04,
|
|
27
|
-
state.position.z * 1.04
|
|
28
|
-
);
|
|
29
|
-
piece.rotation.set(state.rotation.x, state.rotation.y, state.rotation.z);
|
|
30
|
-
piece.userData = {
|
|
31
|
-
position: Object.assign({}, state.position),
|
|
32
|
-
rotation: Object.assign({}, state.rotation),
|
|
33
|
-
initialPosition: Object.assign({}, state.position),
|
|
34
|
-
initialRotation: Object.assign({}, state.rotation),
|
|
35
|
-
type: state.type,
|
|
36
|
-
};
|
|
37
|
-
this.group.add(piece);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
reset() {
|
|
42
|
-
this.group.children.forEach((piece) => {
|
|
43
|
-
const { x, y, z } = piece.userData.initialPosition;
|
|
44
|
-
const { x: u, y: v, z: w } = piece.userData.initialRotation;
|
|
45
|
-
piece.position.set(x * 1.04, y * 1.04, z * 1.04);
|
|
46
|
-
piece.rotation.set(u, v, w);
|
|
47
|
-
piece.userData.position.x = x
|
|
48
|
-
piece.userData.position.y = y
|
|
49
|
-
piece.userData.position.z = z
|
|
50
|
-
piece.userData.rotation.x = u
|
|
51
|
-
piece.userData.rotation.y = v
|
|
52
|
-
piece.userData.rotation.z = w
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* @param {"x"|"y"|"z"} axis
|
|
57
|
-
* @param {{-1|0|1}[]} layers
|
|
58
|
-
* @returns {THREE.Object3D[]}
|
|
59
|
-
*/
|
|
60
|
-
getRotationLayer(axis, layers) {
|
|
61
|
-
if (layers.length === 0) {
|
|
62
|
-
return [...this.group.children];
|
|
63
|
-
}
|
|
64
|
-
return this.group.children.filter((piece) => {
|
|
65
|
-
if (axis === "x") {
|
|
66
|
-
return layers.includes(Math.round(piece.userData.position.x));
|
|
67
|
-
} else if (axis === "y") {
|
|
68
|
-
return layers.includes(Math.round(piece.userData.position.y));
|
|
69
|
-
} else if (axis === "z") {
|
|
70
|
-
return layers.includes(Math.round(piece.userData.position.z));
|
|
71
|
-
}
|
|
72
|
-
return false;
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
getStickerState() {
|
|
77
|
-
const state = {
|
|
78
|
-
up: [[], [], []],
|
|
79
|
-
down: [[], [], []],
|
|
80
|
-
front: [[], [], []],
|
|
81
|
-
back: [[], [], []],
|
|
82
|
-
left: [[], [], []],
|
|
83
|
-
right: [[], [], []],
|
|
84
|
-
};
|
|
85
|
-
this.group.children.forEach((piece) => {
|
|
86
|
-
if (piece.userData.type === "core") {
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
piece.children.forEach((mesh) => {
|
|
90
|
-
if (mesh.userData.type === "sticker") {
|
|
91
|
-
const piecepos = new THREE.Vector3();
|
|
92
|
-
piecepos.copy(piece.position);
|
|
93
|
-
piecepos.round();
|
|
94
|
-
const stickerpos = new THREE.Vector3();
|
|
95
|
-
mesh.getWorldPosition(stickerpos);
|
|
96
|
-
stickerpos.sub(piecepos);
|
|
97
|
-
stickerpos.multiplyScalar(2);
|
|
98
|
-
stickerpos.round();
|
|
99
|
-
if (stickerpos.x === 1) {
|
|
100
|
-
state.right[1 - Math.round(piece.position.y)][
|
|
101
|
-
1 - Math.round(piece.position.z)
|
|
102
|
-
] = mesh.material.userData.face;
|
|
103
|
-
} else if (stickerpos.x === -1) {
|
|
104
|
-
state.left[1 - Math.round(piece.position.y)][
|
|
105
|
-
1 + Math.round(piece.position.z)
|
|
106
|
-
] = mesh.material.userData.face;
|
|
107
|
-
} else if (stickerpos.y === 1) {
|
|
108
|
-
state.up[1 + Math.round(piece.position.z)][
|
|
109
|
-
1 + Math.round(piece.position.x)
|
|
110
|
-
] = mesh.material.userData.face;
|
|
111
|
-
} else if (stickerpos.y === -1) {
|
|
112
|
-
state.down[1 - Math.round(piece.position.z)][
|
|
113
|
-
1 + Math.round(piece.position.x)
|
|
114
|
-
] = mesh.material.userData.face;
|
|
115
|
-
} else if (stickerpos.z === 1) {
|
|
116
|
-
state.front[1 - Math.round(piece.position.y)][
|
|
117
|
-
1 + Math.round(piece.position.x)
|
|
118
|
-
] = mesh.material.userData.face;
|
|
119
|
-
} else if (stickerpos.z === -1) {
|
|
120
|
-
state.back[1 - Math.round(piece.position.y)][
|
|
121
|
-
1 - Math.round(piece.position.x)
|
|
122
|
-
] = mesh.material.userData.face;
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
});
|
|
126
|
-
});
|
|
127
|
-
return state;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* @param {{x:number,y:number,z:number}} position
|
|
132
|
-
* @param {"corner | edge | center"} type
|
|
133
|
-
* @returns {THREE.Group}
|
|
134
|
-
*/
|
|
135
|
-
createPiece(position, type) {
|
|
136
|
-
if (type === "corner") {
|
|
137
|
-
return this.createCorner(position).group;
|
|
138
|
-
} else if (type === "edge") {
|
|
139
|
-
return this.createEdge(position).group;
|
|
140
|
-
} else if (type === "center") {
|
|
141
|
-
return this.createCenter(position).group;
|
|
142
|
-
} else {
|
|
143
|
-
throw new Error("Invalid type: " + type);
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
/**
|
|
147
|
-
* @param {{x:number,y:number,z:number}} position
|
|
148
|
-
* @returns {THREE.Group}
|
|
149
|
-
*/
|
|
150
|
-
createCorner(position) {
|
|
151
|
-
if (position.x == 1 && position.y == 1 && position.z == 1) {
|
|
152
|
-
return newCorner(
|
|
153
|
-
Stickers.corner,
|
|
154
|
-
Materials.front,
|
|
155
|
-
Materials.right,
|
|
156
|
-
Materials.up,
|
|
157
|
-
Materials.core
|
|
158
|
-
);
|
|
159
|
-
} else if (position.x == 1 && position.y == 1 && position.z == -1) {
|
|
160
|
-
return newCorner(
|
|
161
|
-
Stickers.corner,
|
|
162
|
-
Materials.right,
|
|
163
|
-
Materials.back,
|
|
164
|
-
Materials.up,
|
|
165
|
-
Materials.core
|
|
166
|
-
);
|
|
167
|
-
} else if (position.x == 1 && position.y == -1 && position.z == 1) {
|
|
168
|
-
return newCorner(
|
|
169
|
-
Stickers.corner,
|
|
170
|
-
Materials.right,
|
|
171
|
-
Materials.front,
|
|
172
|
-
Materials.down,
|
|
173
|
-
Materials.core
|
|
174
|
-
);
|
|
175
|
-
} else if (position.x == 1 && position.y == -1 && position.z == -1) {
|
|
176
|
-
return newCorner(
|
|
177
|
-
Stickers.corner,
|
|
178
|
-
Materials.back,
|
|
179
|
-
Materials.right,
|
|
180
|
-
Materials.down,
|
|
181
|
-
Materials.core
|
|
182
|
-
);
|
|
183
|
-
} else if (position.x == -1 && position.y == 1 && position.z == 1) {
|
|
184
|
-
return newCorner(
|
|
185
|
-
Stickers.corner,
|
|
186
|
-
Materials.left,
|
|
187
|
-
Materials.front,
|
|
188
|
-
Materials.up,
|
|
189
|
-
Materials.core
|
|
190
|
-
);
|
|
191
|
-
} else if (position.x == -1 && position.y == 1 && position.z == -1) {
|
|
192
|
-
return newCorner(
|
|
193
|
-
Stickers.corner,
|
|
194
|
-
Materials.back,
|
|
195
|
-
Materials.left,
|
|
196
|
-
Materials.up,
|
|
197
|
-
Materials.core
|
|
198
|
-
);
|
|
199
|
-
} else if (position.x == -1 && position.y == -1 && position.z == 1) {
|
|
200
|
-
return newCorner(
|
|
201
|
-
Stickers.corner,
|
|
202
|
-
Materials.front,
|
|
203
|
-
Materials.left,
|
|
204
|
-
Materials.down,
|
|
205
|
-
Materials.core
|
|
206
|
-
);
|
|
207
|
-
} else if (position.x == -1 && position.y == -1 && position.z == -1) {
|
|
208
|
-
return newCorner(
|
|
209
|
-
Stickers.corner,
|
|
210
|
-
Materials.left,
|
|
211
|
-
Materials.back,
|
|
212
|
-
Materials.down,
|
|
213
|
-
Materials.core
|
|
214
|
-
);
|
|
215
|
-
} else {
|
|
216
|
-
throw new Error("Invalid corner position: " + position);
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
/**
|
|
220
|
-
* @param {{x:number,y:number,z:number}} position
|
|
221
|
-
* @returns {THREE.Group}
|
|
222
|
-
*/
|
|
223
|
-
createEdge(position) {
|
|
224
|
-
if (position.x == 1 && position.y == 1 && position.z == 0) {
|
|
225
|
-
return newEdge(
|
|
226
|
-
Stickers.edge,
|
|
227
|
-
Materials.right,
|
|
228
|
-
Materials.up,
|
|
229
|
-
Materials.core
|
|
230
|
-
);
|
|
231
|
-
} else if (position.x == 1 && position.y == -1 && position.z == 0) {
|
|
232
|
-
return newEdge(
|
|
233
|
-
Stickers.edge,
|
|
234
|
-
Materials.right,
|
|
235
|
-
Materials.down,
|
|
236
|
-
Materials.core
|
|
237
|
-
);
|
|
238
|
-
} else if (position.x == 1 && position.y == 0 && position.z == 1) {
|
|
239
|
-
return newEdge(
|
|
240
|
-
Stickers.edge,
|
|
241
|
-
Materials.front,
|
|
242
|
-
Materials.right,
|
|
243
|
-
Materials.core
|
|
244
|
-
);
|
|
245
|
-
} else if (position.x == 1 && position.y == 0 && position.z == -1) {
|
|
246
|
-
return newEdge(
|
|
247
|
-
Stickers.edge,
|
|
248
|
-
Materials.right,
|
|
249
|
-
Materials.back,
|
|
250
|
-
Materials.core
|
|
251
|
-
);
|
|
252
|
-
} else if (position.x == -1 && position.y == 1 && position.z == 0) {
|
|
253
|
-
return newEdge(
|
|
254
|
-
Stickers.edge,
|
|
255
|
-
Materials.left,
|
|
256
|
-
Materials.up,
|
|
257
|
-
Materials.core
|
|
258
|
-
);
|
|
259
|
-
} else if (position.x == -1 && position.y == -1 && position.z == 0) {
|
|
260
|
-
return newEdge(
|
|
261
|
-
Stickers.edge,
|
|
262
|
-
Materials.left,
|
|
263
|
-
Materials.down,
|
|
264
|
-
Materials.core
|
|
265
|
-
);
|
|
266
|
-
} else if (position.x == -1 && position.y == 0 && position.z == 1) {
|
|
267
|
-
return newEdge(
|
|
268
|
-
Stickers.edge,
|
|
269
|
-
Materials.front,
|
|
270
|
-
Materials.left,
|
|
271
|
-
Materials.core
|
|
272
|
-
);
|
|
273
|
-
} else if (position.x == -1 && position.y == 0 && position.z == -1) {
|
|
274
|
-
return newEdge(
|
|
275
|
-
Stickers.edge,
|
|
276
|
-
Materials.left,
|
|
277
|
-
Materials.back,
|
|
278
|
-
Materials.core
|
|
279
|
-
);
|
|
280
|
-
} else if (position.x == 0 && position.y == 1 && position.z == 1) {
|
|
281
|
-
return newEdge(
|
|
282
|
-
Stickers.edge,
|
|
283
|
-
Materials.front,
|
|
284
|
-
Materials.up,
|
|
285
|
-
Materials.core
|
|
286
|
-
);
|
|
287
|
-
} else if (position.x == 0 && position.y == 1 && position.z == -1) {
|
|
288
|
-
return newEdge(
|
|
289
|
-
Stickers.edge,
|
|
290
|
-
Materials.up,
|
|
291
|
-
Materials.back,
|
|
292
|
-
Materials.core
|
|
293
|
-
);
|
|
294
|
-
} else if (position.x == 0 && position.y == -1 && position.z == 1) {
|
|
295
|
-
return newEdge(
|
|
296
|
-
Stickers.edge,
|
|
297
|
-
Materials.down,
|
|
298
|
-
Materials.front,
|
|
299
|
-
Materials.core
|
|
300
|
-
);
|
|
301
|
-
} else if (position.x == 0 && position.y == -1 && position.z == -1) {
|
|
302
|
-
return newEdge(
|
|
303
|
-
Stickers.edge,
|
|
304
|
-
Materials.back,
|
|
305
|
-
Materials.down,
|
|
306
|
-
Materials.core
|
|
307
|
-
);
|
|
308
|
-
} else {
|
|
309
|
-
throw new Error("Invalid edge position: " + position);
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
/**
|
|
313
|
-
* @param {{x:number,y:number,z:number}} position
|
|
314
|
-
* @returns {THREE.Group}
|
|
315
|
-
*/
|
|
316
|
-
createCenter(position) {
|
|
317
|
-
var centerColor = Materials.up;
|
|
318
|
-
if (position.x !== 0) {
|
|
319
|
-
centerColor = position.x > 0 ? Materials.right : Materials.left;
|
|
320
|
-
} else if (position.y !== 0) {
|
|
321
|
-
centerColor = position.y > 0 ? Materials.up : Materials.down;
|
|
322
|
-
} else if (position.z !== 0) {
|
|
323
|
-
centerColor = position.z > 0 ? Materials.front : Materials.back;
|
|
324
|
-
}
|
|
325
|
-
return newCenter(Stickers.center, centerColor, Materials.core);
|
|
326
|
-
}
|
|
327
|
-
/**
|
|
328
|
-
* @returns {THREE.Group}
|
|
329
|
-
*/
|
|
330
|
-
createCore() {
|
|
331
|
-
return new THREE.Mesh(new THREE.SphereGeometry(1.55), Materials.core);
|
|
332
|
-
}
|
|
333
|
-
}
|
package/src/cubeState.js
DELETED
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @typedef {{x: number,y: number,z: number}} vector
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @type {vector[]}
|
|
7
|
-
*/
|
|
8
|
-
const corners = [
|
|
9
|
-
{ position: { x: 1, y: 1, z: 1 }, rotation: { x: 0, y: 0, z: 0 } },
|
|
10
|
-
{ position: { x: 1, y: 1, z: -1 }, rotation: { x: 0, y: Math.PI / 2, z: 0 } },
|
|
11
|
-
{
|
|
12
|
-
position: { x: 1, y: -1, z: 1 },
|
|
13
|
-
rotation: { x: 0, y: Math.PI / 2, z: Math.PI },
|
|
14
|
-
},
|
|
15
|
-
{
|
|
16
|
-
position: { x: 1, y: -1, z: -1 },
|
|
17
|
-
rotation: { x: 0, y: Math.PI, z: Math.PI },
|
|
18
|
-
},
|
|
19
|
-
|
|
20
|
-
{
|
|
21
|
-
position: { x: -1, y: 1, z: 1 },
|
|
22
|
-
rotation: { x: 0, y: -Math.PI / 2, z: 0 },
|
|
23
|
-
},
|
|
24
|
-
{ position: { x: -1, y: 1, z: -1 }, rotation: { x: 0, y: Math.PI, z: 0 } },
|
|
25
|
-
{ position: { x: -1, y: -1, z: 1 }, rotation: { x: 0, y: 0, z: Math.PI } },
|
|
26
|
-
{
|
|
27
|
-
position: { x: -1, y: -1, z: -1 },
|
|
28
|
-
rotation: { x: 0, y: -Math.PI / 2, z: Math.PI },
|
|
29
|
-
},
|
|
30
|
-
];
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* @type {vector[]}
|
|
34
|
-
*/
|
|
35
|
-
const edges = [
|
|
36
|
-
{ position: { x: 1, y: 1, z: 0 }, rotation: { x: 0, y: Math.PI / 2, z: 0 } },
|
|
37
|
-
{
|
|
38
|
-
position: { x: 1, y: 0, z: 1 },
|
|
39
|
-
rotation: { x: 0, y: 0, z: -Math.PI / 2 },
|
|
40
|
-
},
|
|
41
|
-
{
|
|
42
|
-
position: { x: 1, y: 0, z: -1 },
|
|
43
|
-
rotation: { x: 0, y: Math.PI / 2, z: -Math.PI / 2 },
|
|
44
|
-
},
|
|
45
|
-
{
|
|
46
|
-
position: { x: 1, y: -1, z: 0 },
|
|
47
|
-
rotation: { x: Math.PI, y: Math.PI / 2, z: 0 },
|
|
48
|
-
},
|
|
49
|
-
{ position: { x: 0, y: 1, z: 1 }, rotation: { x: 0, y: 0, z: 0 } },
|
|
50
|
-
{
|
|
51
|
-
position: { x: 0, y: 1, z: -1 },
|
|
52
|
-
rotation: { x: -Math.PI / 2, y: 0, z: 0 },
|
|
53
|
-
},
|
|
54
|
-
{ position: { x: 0, y: -1, z: 1 }, rotation: { x: Math.PI / 2, y: 0, z: 0 } },
|
|
55
|
-
{ position: { x: 0, y: -1, z: -1 }, rotation: { x: Math.PI, y: 0, z: 0 } },
|
|
56
|
-
{
|
|
57
|
-
position: { x: -1, y: 1, z: 0 },
|
|
58
|
-
rotation: { x: 0, y: -Math.PI / 2, z: 0 },
|
|
59
|
-
},
|
|
60
|
-
{ position: { x: -1, y: 0, z: 1 }, rotation: { x: 0, y: 0, z: Math.PI / 2 } },
|
|
61
|
-
{
|
|
62
|
-
position: { x: -1, y: 0, z: -1 },
|
|
63
|
-
rotation: { x: 0, y: -Math.PI / 2, z: Math.PI / 2 },
|
|
64
|
-
},
|
|
65
|
-
{
|
|
66
|
-
position: { x: -1, y: -1, z: 0 },
|
|
67
|
-
rotation: { x: 0, y: -Math.PI / 2, z: Math.PI },
|
|
68
|
-
},
|
|
69
|
-
];
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* @type {vector[]}
|
|
73
|
-
*/
|
|
74
|
-
const centers = [
|
|
75
|
-
{ position: { x: 1, y: 0, z: 0 }, rotation: { x: 0, y: Math.PI / 2, z: 0 } },
|
|
76
|
-
{ position: { x: 0, y: 1, z: 0 }, rotation: { x: -Math.PI / 2, y: 0, z: 0 } },
|
|
77
|
-
{ position: { x: 0, y: 0, z: 1 }, rotation: { x: 0, y: 0, z: 0 } },
|
|
78
|
-
{ position: { x: 0, y: 0, z: -1 }, rotation: { x: 0, y: Math.PI, z: 0 } },
|
|
79
|
-
{ position: { x: 0, y: -1, z: 0 }, rotation: { x: Math.PI / 2, y: 0, z: 0 } },
|
|
80
|
-
{
|
|
81
|
-
position: { x: -1, y: 0, z: 0 },
|
|
82
|
-
rotation: { x: 0, y: -Math.PI / 2, z: 0 },
|
|
83
|
-
},
|
|
84
|
-
];
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* @typedef {Object} state
|
|
88
|
-
* @property {string} type
|
|
89
|
-
* @property {vector} initialPosition
|
|
90
|
-
* @property {vector} initialRotation
|
|
91
|
-
* @property {vector} position
|
|
92
|
-
* @property {vector} rotation
|
|
93
|
-
*/
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* @returns {state[]}
|
|
97
|
-
*/
|
|
98
|
-
export function newCubeState() {
|
|
99
|
-
let cubeState = [];
|
|
100
|
-
corners.forEach(({ position, rotation }) => {
|
|
101
|
-
cubeState.push({
|
|
102
|
-
type: "corner",
|
|
103
|
-
initialPosition: position,
|
|
104
|
-
initialRotation: rotation,
|
|
105
|
-
position: position,
|
|
106
|
-
rotation: rotation,
|
|
107
|
-
});
|
|
108
|
-
});
|
|
109
|
-
edges.forEach(({ position, rotation }) => {
|
|
110
|
-
cubeState.push({
|
|
111
|
-
type: "edge",
|
|
112
|
-
initialPosition: position,
|
|
113
|
-
initialRotation: rotation,
|
|
114
|
-
position: position,
|
|
115
|
-
rotation: rotation,
|
|
116
|
-
});
|
|
117
|
-
});
|
|
118
|
-
centers.forEach(({ position, rotation }) => {
|
|
119
|
-
cubeState.push({
|
|
120
|
-
type: "center",
|
|
121
|
-
initialPosition: position,
|
|
122
|
-
initialRotation: rotation,
|
|
123
|
-
position: position,
|
|
124
|
-
rotation: rotation,
|
|
125
|
-
});
|
|
126
|
-
});
|
|
127
|
-
return cubeState;
|
|
128
|
-
}
|