/
grid.js
executable file
·103 lines (94 loc) · 2.52 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
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
var Grid = module.exports.Grid = function(opt) {
this.name = opt.name;
this._id= opt._id;
this.width = this.height = 16;
if(opt && opt.width && opt.height){
this.width = opt.width;
this.height = opt.height;
}
this.data = [];
for( var i = 0; i < this.width * this.height; i++) {
this.data.push(0);
}
this.clearTo({index: -1});
if(typeof opt.data === 'string'){
opt.data = JSON.parse(opt.data);
}
if(opt.data && opt.data.length === this.data.length){
this.data = opt.data;
}
};
Grid.prototype.clearTo = function(value) {
for ( var i = 0; i < this.width * this.height; i++) {
this.data[i] = value;
}
};
Grid.prototype.blit = function(grid, x, y, width, height) {
if (typeof width === 'undefined') {
width = grid.width;
}
if (typeof height === 'undefined') {
height = grid.height;
}
width = Math.min(width, this.width - x);
height = Math.min(height, this.height - y);
for (var i = 0; i < height; i++) {
for (var j = 0; j < width; j++) {
this.put(x+j, y+i, grid.get(j,i));
}
}
};
Grid.prototype.get = function(x, y) {
if (x >= 0 && x < this.width && y >= 0 && y < this.height) {
return this.data[x + y * this.width];
}
console.log('grid.get out of bounds: ' + x + ' | ' + y);
};
Grid.prototype.put = function(x, y, value) {
if(x >= 0 && x < this.width && y >= 0 && y < this.height){
this.data[y * this.width + x] = value;
}
},
Grid.prototype.copy = function(frame){
if(typeof frame === 'undefined'){
frame = {};
frame.x = 0;
frame.y = 0;
frame.width = this.width;
frame.height = this.height;
}
frame.x = Math.max(0, frame.x);
frame.y = Math.max(0, frame.y);
frame.width = Math.max(this.width, frame.width);
frame.height = Math.max(this.height, frame.height);
copy = new Grid({width:frame.width, height:frame.height});
for(var i = 0; i< frame.height; i++){
for(var j = 0; j< frame.width; j++){
copy.put(j,i,this.get(frame.x + j, frame.y + i));
}
}
return copy;
};
Grid.prototype.shift = function(x, y, clear){
var copy = this.copy();
if(typeof clear !== 'undefined'){
this.clearTo(clear);
}
this.blit(copy,x,y);
};
Grid.prototype.resize= function(width, height, clear){
var copy = this.copy();
this.width= width;
this.height = height;
this.data=[];
for( var i = 0; i < this.width * this.height; i++) {
this.data.push(0);
}
if(typeof clear !== 'undefined'){
this.clearTo(clear);
}
this.blit(copy,0,0);
};
Grid.prototype.rename = function(name){
this.name = name;
};