xxxxxxxxxx
93
class Cube {
constructor(dim, size) {
this.dim = dim;
this.size = size;
this.currentMove = new Move(0, 0, 0, 1);
this.allMoves = []
this.cubies = [];
// Build Cubies Array
let r = (dim - 1) / 2;
for (var x = -r; x <= r; x++) {
for (var y = -r; y <= r; y++) {
for (var z = -r; z <= r; z++) {
this.cubies.push(new Cubie(x, y, z, dim));
}
}
}
}
draw() {
scale(this.size);
rotateX(-0.5);
rotateY(0.5);
rotateZ(0.1);
// Update Move
this.currentMove.update(this);
// Draw the Cube
for (var c of this.cubies) {
push();
if (abs(c.z) > 0 && c.z == this.currentMove.z) {
rotateZ(this.currentMove.angle);
} else if (abs(c.x) > 0 && c.x == this.currentMove.x) {
rotateX(this.currentMove.angle);
} else if (abs(c.y) > 0 && c.y == this.currentMove.y) {
rotateY(-this.currentMove.angle);
}
c.draw();
pop();
}
}
turnX(idx, dir) {
for (var c of this.cubies) {
if (c.x == idx) {
let m = new p5.Matrix();
m.rotate(dir * HALF_PI, [1, 0, 0]);
m.translate([0, c.y, c.z]);
c.update(c.x, m.mat4[13], m.mat4[14]);
c.turnFaces("X", dir);
}
}
}
turnY(idx, dir) {
for (var c of this.cubies) {
if (c.y == idx) {
let m = new p5.Matrix();
m.rotate(dir * HALF_PI, [0, -1, 0]);
m.translate([c.x, 0, c.z]);
c.update(m.mat4[12], c.y, m.mat4[14]);
c.turnFaces("Y", dir);
}
}
}
turnZ(idx, dir) {
for (var c of this.cubies) {
if (c.z == idx) {
let m = new p5.Matrix();
m.rotate(dir * HALF_PI, [0, 0, 1]);
m.translate([c.x, c.y, 1]);
c.update(m.mat4[12], m.mat4[13], c.z);
c.turnFaces("Z", dir);
}
}
}
applyMove(move) {
// Start moving
this.currentMove = move;
this.currentMove.start();
// Store the move
this.allMoves.push(move);
}
solveMoves() {
console.log(this.allMoves);
console.log(this.allMoves.length);
}
}