-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface.js
171 lines (149 loc) · 4.86 KB
/
face.js
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
import Game from "./game";
export default class Face {
faceId
#axe
/** @type Cube[] */
_group = []
#pivotPoint;
constructor(faceId) {
this.faceId = faceId;
this.#init();
}
#init() {
if (this.faceId===0||this.faceId===1||this.faceId.toString().startsWith("10")) this.#axe="x";
if (this.faceId===2||this.faceId===3||this.faceId.toString().startsWith("12")) this.#axe="y";
if (this.faceId===4||this.faceId===5||this.faceId.toString().startsWith("14")) this.#axe="z";
}
get pivotPoint() {
return this.#pivotPoint;
}
set pivotPoint(value) {
this.#pivotPoint = value;
}
get axe() {
return this.#axe;
}
set group(value) {
this._group = value;
}
get group() {
return this._group;
}
addToGroup(added) {
if (Array.isArray(added)) {
this._group.push(...added);
}
else {
this._group.push(added);
}
}
correctCubesPositions() {
for (let i = 0; i < this._group.length; i++) {
let cube = this._group[i];
cube.correctPosition();
}
}
reorderCubes() {
let cubes = this._group;
let coefs;
let fakeFaceId = this.faceId;
if (this.faceId > 5) {
if (this.faceId.toString().startsWith("100")) {
fakeFaceId = 0;
}
else if (this.faceId.toString().startsWith("120")) {
fakeFaceId = 2;
}
else if (this.faceId.toString().startsWith("140")) {
fakeFaceId = 4;
}
}
switch (fakeFaceId) {
case 0:
coefs = [0, 1, -1];
break;
case 1:
coefs = [0, 1, 1];
break;
case 2:
coefs = [-1, 0, -1];
break;
case 3:
coefs = [1, 0, -1];
break;
case 4:
coefs = [1, 1, 0];
break;
case 5:
coefs = [-1, 1, 0];
break;
default:
coefs = [0, 0, 0];
break;
}
cubes.sort(function (a, b) {
let aPos = a.ThreeCube.position;
let bPos = b.ThreeCube.position;
if (coefs[1] !== 0 && aPos.y !== bPos.y) return coefs[1] * (aPos.y - bPos.y);
if (coefs[0] !== 0 && aPos.x !== bPos.x) return coefs[0] * (aPos.x - bPos.x);
return coefs[2] * (aPos.z - bPos.z);
});
this._group = cubes;
}
turn(move, forcedStep = null) {
let pivotPoint = this.pivotPoint;
let axis = new THREE.Vector3();
axis[this.axe] = 1;
let step = forcedStep === null ? Game.speeds[move.speed] : forcedStep;
let theta = move.clockwise ? -step : step;
if ([1,3,5].includes(this.faceId)) theta = -1*theta;
for (let i = 0; i < this._group.length; i++) {
let cube = this._group[i];
Face.rotateAboutPoint(cube.ThreeCube, pivotPoint.position, axis, theta, false);
}
}
correctCubesColorsAxis() {
let staticColorAxis = this.axe;
let keys = ["x", "y", "z"].filter(key => key !== staticColorAxis);
let newKeys = [...keys].reverse();
for (let i = 0; i < this._group.length; i++) {
let cube = this._group[i];
let colors = {...cube.colors};
colors[keys[0]] = cube.colors[newKeys[0]];
colors[keys[1]] = cube.colors[newKeys[1]];
cube.colors = colors;
let transparentIds = {...cube.transparentIds};
transparentIds[keys[0]] = cube.transparentIds[newKeys[0]];
transparentIds[keys[1]] = cube.transparentIds[newKeys[1]];
cube.transparentIds = transparentIds;
}
}
static rotateAboutPoint(obj, point, axis, theta, pointIsWorld){
// rotate object around axis in world space (the axis passes through point)
// axis is assumed to be normalized
// assumes object does not have a rotated parent
let q = new THREE.Quaternion();
q.setFromAxisAngle( axis, theta );
obj.applyQuaternion( q );
obj.position.sub( point );
obj.position.applyQuaternion( q );
obj.position.add( point );
}
searchForCorrectCubeInFace(color) {
for (let i = 0; i < this.group.length; i++) {
if (this.group[i].colors[this.axe] === color) {
return this.group[i];
}
}
return null;
}
/** @type Face */
static searchForFaceOfCube(faces, cube, faceToIgnore = -1) {
for (let i = 0; i < faces.length; i++) {
if (faces[i].group.includes(cube) && i !== faceToIgnore) {
return faces[i];
}
}
return null;
}
}