-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgrid.js
35 lines (32 loc) · 967 Bytes
/
grid.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
import { Thing } from "./thing.js";
const arrayOfLength = len => Array.apply(null, Array(len));
export class Grid {
constructor(x, y, cols, rows, gridsize, cell=Thing) {
this.x = x;
this.y = y;
this.cols = cols;
this.rows = rows;
this.gridsize = gridsize;
this._grid = arrayOfLength(rows);
for (let y = 0; y < this.rows; y++) {
this._grid[y] = [];
for (let x = 0; x < this.cols; x++) {
this._grid[y][x] = new cell(gridsize * x, gridsize * y, gridsize, gridsize);
}
}
}
draw(ctx, drawSprite) {
for (let row of this._grid) {
for (let cell of row) {
cell.draw(ctx, drawSprite);
}
}
}
update(ratio, keyboard, mouse) {
for (let row of this._grid) {
for (let cell of row) {
cell.update(ratio, keyboard, mouse);
}
}
}
}