-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtable.js
41 lines (36 loc) · 1.07 KB
/
table.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
var TileMap = require('./tilemap');
function Table(viewport, tilemap, updateCallback) {
this.viewport = viewport;
this.tilemap = tilemap;
this.updateCallback = updateCallback;
this.reset();
}
Table.prototype.reset = function() {
this.domMap = new TileMap(this.tilemap.width, this.tilemap.height);
this.createNodes();
}
Table.prototype.createNodes = function() {
// Clear nodes in viewport
while(this.viewport.firstChild) {
this.viewport.removeChild(this.viewport.firstChild);
}
// Add nodes in viewport
for(var y = 0; y < this.domMap.height; ++y) {
var row = document.createElement('tr');
this.viewport.appendChild(row);
for(var x = 0; x < this.domMap.width; ++x) {
var column = document.createElement('td');
row.appendChild(column);
column.tx = x;
column.ty = y;
this.domMap.set(x, y, column);
this.updateNode(x, y);
}
}
}
Table.prototype.updateNode = function(x, y) {
var node = this.domMap.get(x, y);
var tile = this.tilemap.get(x, y);
this.updateCallback(node, tile, x, y);
}
module.exports = Table;