-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMap.js
109 lines (99 loc) · 3.09 KB
/
Map.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
const posEven = [[0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, 0]];
const posOdd = [[1, 1], [0, 1], [-1, 0], [0, -1], [1, -1], [1, 0]];
// eslint-disable-next-line no-unused-vars
class Map {
constructor (ctx, cellSize, width, height, styles) {
this.ctx = ctx;
this.styles = styles;
this.cellSize = cellSize;
this.cellWidth = Math.sqrt(3) * cellSize;
this.cellHeight = 2 * cellSize;
this.horizontalSpacing = this.cellWidth;
this.verticalSpacing = (3 / 4) * this.cellHeight;
this.numberCellX = Math.ceil(width / this.cellWidth);
this.numberCellY = Math.ceil(height / this.verticalSpacing) + 1;
this.cells = [];
this.generateMap();
}
generateMap () {
let x;
let y;
for (let j = 0; j < this.numberCellY; j++) {
for (let i = 0; i < this.numberCellX; i++) {
[x, y] = this.calculatePosition(i, j);
this.cells.push(new Cell(x, y, i, j, this.cellSize, this.ctx, this.styles));
}
}
}
draw () {
for (const c of this.cells) {
c.draw();
}
}
calculatePosition (i, j) {
return [
(i * this.horizontalSpacing) + ((j % 2) * (this.horizontalSpacing / 2)),
j * this.verticalSpacing
];
}
getCellAt (x, y) {
let c;
if (x < 0 || x >= this.numberCellX || y < 0 || y >= this.numberCellY) {
c = undefined;
} else {
c = this.cells[x + (y * this.numberCellX)];
}
return c;
}
isAllCellsVisited () {
for (const c of this.cells) {
if (!c.visited) {
return false;
}
}
return true;
}
getNeigbors (c) {
const cellPos = c.mapPos;
const pos = cellPos[1]%2 == 0 ? posEven : posOdd;
const neigbors = [];
let neigbor;
for (const p of pos) {
// console.log(cellPos, p);
neigbor = this.getCellAt(cellPos[0] + p[0], cellPos[1] + p[1]);
if (neigbor instanceof Cell) {
neigbors.push(neigbor);
}
}
return neigbors;
}
getRandomNotVisitedNeigbors (c) {
const neigbors = this.getNeigbors(c);
const notVisited = [];
for (const n of neigbors) {
if (!n.visited) {
notVisited.push(n);
}
}
if (notVisited.length > 0) {
return notVisited[Math.floor(Math.random() * notVisited.length)];
} else {
return undefined;
}
}
removeWall (c1, c2) {
const c1Pos = c1.mapPos;
const c2Pos = c2.mapPos;
const pos = c1Pos[1] % 2 == 0 ? posEven : posOdd;
const delta = [c2Pos[0] - c1Pos[0], c2Pos[1] - c1Pos[1]];
let wallIndices;
for (const [i, p] of pos.entries()) {
if (p[0] == delta[0] && p[1] == delta[1]) {
wallIndices = i;
break;
}
}
c1.removeWall(wallIndices);
c2.removeWall((wallIndices + 3) % 6);
}
}