generated from tassaron/canvas-game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.js
41 lines (37 loc) · 1.1 KB
/
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
36
37
38
39
40
41
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.width = cols * gridsize;
this.height = rows * gridsize;
this.clear(cell);
}
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);
}
}
}
clear(cell=Thing) {
this._grid = arrayOfLength(this.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(this.gridsize * x, this.gridsize * y, this.gridsize, this.gridsize);
}
}
}
}