rewrite puzzle 1 to support NxM
This commit is contained in:
@@ -7,14 +7,27 @@ class PuzzleGame1 {
|
|||||||
let w = data.w, h = data.h, wh = w*h;
|
let w = data.w, h = data.h, wh = w*h;
|
||||||
this.object = new Group();
|
this.object = new Group();
|
||||||
const aq = new MotionEngine();
|
const aq = new MotionEngine();
|
||||||
const pr = [[0, -1], [0, 1], [1, 0], [-1, 0], [0, 0], [0, 2]];
|
const pr = [[0, 2], [0, -1], [0, 1], [1, 0], [-1, 0], [0, 0]];
|
||||||
let d = 1.2;
|
let d = 1.2;
|
||||||
|
|
||||||
let bm = new BoxGeometry(1, 1, 1);
|
let bm = new BoxGeometry(1, 1, 1);
|
||||||
let uv = bm.getAttribute('uv');
|
|
||||||
|
let material = new MeshBasicMaterial({
|
||||||
|
map: await engine.loadTexture(data.$go.asset.name)
|
||||||
|
});
|
||||||
|
//material.map.encoding = sRGBEncoding;
|
||||||
|
|
||||||
for (let i = 0; i < wh; i++) {
|
for (let i = 0; i < wh; i++) {
|
||||||
let s = [(i % w) / w, Math.trunc(i / w) / h];
|
let b = bm.clone();
|
||||||
|
let uv = b.getAttribute('uv');
|
||||||
|
|
||||||
|
//0-8->R, 8-16->L, 16-24->Top, 24-32->Bottom, 32-40->Front, 40-48->Back
|
||||||
|
let x = i % w, y = Math.trunc(i / w);
|
||||||
|
let idxs = [(i+1) % wh, (i+2)%wh, (i+3)%wh, (i+4)%wh, i, y*w + w - x -1]
|
||||||
|
for (let i = 0; i < 6; i++) {
|
||||||
|
let idx = idxs[i];
|
||||||
|
let x = idx % w, y = Math.trunc(idx / w)
|
||||||
|
let s = [x / w, y / h];
|
||||||
//top left
|
//top left
|
||||||
uv.array[8 * i] = s[0];
|
uv.array[8 * i] = s[0];
|
||||||
uv.array[8 * i + 1] = s[1] + 1 / h;
|
uv.array[8 * i + 1] = s[1] + 1 / h;
|
||||||
@@ -29,21 +42,14 @@ class PuzzleGame1 {
|
|||||||
uv.array[8 * i + 7] = s[1];
|
uv.array[8 * i + 7] = s[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
let material = new MeshBasicMaterial({
|
|
||||||
map: await engine.loadTexture(data.$go.asset.name)
|
|
||||||
});
|
|
||||||
//material.map.encoding = sRGBEncoding;
|
|
||||||
|
|
||||||
for (let i = 0; i < wh; i++) {
|
|
||||||
let b = bm.clone();
|
|
||||||
let mesh = new Mesh(b, material);
|
let mesh = new Mesh(b, material);
|
||||||
mesh.position.set((i % w) * d, (Math.trunc(i / w)) * d, 0);
|
mesh.position.set((i % w) * d, (Math.trunc(i / w)) * d, 0);
|
||||||
let ri;
|
let ri = Math.floor(Math.random() * 5);
|
||||||
do {
|
|
||||||
ri = Math.floor(Math.random() * 6);
|
|
||||||
} while (ri == this.object.children.length);
|
|
||||||
mesh.rotation.set(pr[ri][0] * Math.PI / 2, pr[ri][1] * Math.PI / 2, 0);
|
mesh.rotation.set(pr[ri][0] * Math.PI / 2, pr[ri][1] * Math.PI / 2, 0);
|
||||||
mesh._ri = ri;
|
mesh._ri = ri;
|
||||||
|
if (idxs[4] == idxs[5]){
|
||||||
|
mesh._dd = true;
|
||||||
|
}
|
||||||
this.object.add(mesh);
|
this.object.add(mesh);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,13 +58,11 @@ class PuzzleGame1 {
|
|||||||
};
|
};
|
||||||
|
|
||||||
var check = () => {
|
var check = () => {
|
||||||
if (!this.object.children.length) return false;
|
|
||||||
let i = 0;
|
let i = 0;
|
||||||
for (let c of this.object.children) {
|
this.object.children.forEach(o=>{
|
||||||
if (Math.abs(c.rotation.x - pr[i][0] * Math.PI / 2) > 0.0001 || Math.abs(c.rotation.y - pr[i][1] * Math.PI / 2) > 0.0001) return false;
|
if (o._ri == 5 || o._dd && o._ri == 0) i++;
|
||||||
i++;
|
})
|
||||||
}
|
return i == wh;
|
||||||
return true;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let clickFn = (i) => {
|
let clickFn = (i) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user