-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrid.mjs
78 lines (71 loc) · 1.77 KB
/
Grid.mjs
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
import Cell from './Cell.mjs'
/**
* The base class for all grids
* @param {number} size The size of the grid in pixels
* @param {number} scale The size of each cell in pixels
*/
class Grid {
constructor(size, scale) {
this.size = size
this.scale = scale
this.cols = size / scale
this.rows = size / scale
this.cells = []
for (let x = 0; x < this.cols; x++) {
this.cells[x] = []
for (let y = 0; y < this.rows; y++) {
this.cells[x][y] = new Cell(x, y, this)
}
}
// Set the entrance and exit
this.cells[0][0].sides.top = false
this.cells[this.cols - 1][this.rows - 1].sides.bottom = false
}
/**
* Calls the draw method on each cell
*/
draw() {
this.cells.forEach(col => {
col.forEach(cell => {
cell.draw()
})
})
}
/**
* Lookup a cell
* @param {number} x The column index
* @param {number} y The row index
* @return {Cell|undefined} The cell if it exists or undefined if not
*/
getCell(x, y) {
const { cols, rows } = this
if (x < 0 || x > cols - 1 || y < 0 || y > rows - 1) return undefined
return this.cells[x][y]
}
/**
* Remove the borders seperating two cells
* @param {Cell} a The first cell to join
* @param {Cell} b The second cell to join
*/
joinCells(a, b) {
// Remove sides between the two cells
if (a.y > b.y) {
// a is below b
a.sides.top = false
b.sides.bottom = false
} else if (a.x < b.x) {
// a is left of b
a.sides.right = false
b.sides.left = false
} else if (a.y < b.y) {
// a is above b
a.sides.bottom = false
b.sides.top = false
} else {
// a is right of b
a.sides.left = false
b.sides.right = false
}
}
}
export default Grid