-
Notifications
You must be signed in to change notification settings - Fork 0
/
tile.js
75 lines (66 loc) · 2.17 KB
/
tile.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
export class Tile {
constructor(name, nodes, rotations, disallowSelfConnect) {
this.name = name;
this.nodes = nodes;
this.image = null;
this.index = -1;
this.rotations = !!rotations ? rotations : 0;
this.rotated = 0;
this.up = [];
this.right = [];
this.down = [];
this.left = [];
this.disallowSelfConnect = !!disallowSelfConnect;
}
static compare(tile, edge, index) {
return tile.nodes[edge] === index;
}
reverseString(s) {
let arr = s.toString().split("");
arr = arr.reverse();
return arr.join("");
}
compareEdge(a, b) {
return a == this.reverseString(b);
}
analyze(tiles) {
for (let i = 0; i < tiles.length; i++) {
let tile = tiles[i];
if (this.disallowSelfConnect && tile.disallowSelfConnect && this.name === tile.name) { continue; }
// UP
if (this.compareEdge(tile.nodes[2], this.nodes[0])) {
this.up.push(i);
}
// RIGHT
if (this.compareEdge(tile.nodes[3], this.nodes[1])) {
this.right.push(i);
}
// DOWN
if (this.compareEdge(tile.nodes[0], this.nodes[2])) {
this.down.push(i);
}
// LEFT
if (this.compareEdge(tile.nodes[1], this.nodes[3])) {
this.left.push(i);
}
}
}
rotate(num, p5) {
const w = this.image.width;
const h = this.image.height;
const newImg = p5.createGraphics(w, h);
newImg.imageMode(p5.CENTER);
newImg.translate(w / 2, h / 2);
newImg.rotate(p5.HALF_PI * num);
newImg.image(this.image, 0, 0);
const newNodes = [];
const len = this.nodes.length;
for (let i = 0; i < len; i++) {
newNodes[i] = this.nodes[(i - num + len) % len];
}
const result = new Tile(this.name, newNodes, 0, this.disallowSelfConnect);
result.rotated = num;
result.image = newImg;
return result;
}
}