-
Notifications
You must be signed in to change notification settings - Fork 0
/
sp.js
157 lines (84 loc) · 2.55 KB
/
sp.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// no optimizations, just gitrdone
var GridSP = function(options) {
var defaults = {
worldSize: pt(256,256),
cellSize: 8,
gridSize: pt(32,32),
points: [],
bounds: [],
objs: Object.create(null),
};
var e = $.extend({}, defaults, options);
for(x in e) this[x] = e[x];
this.init();
}
GridSP.prototype.init = function() {
var sz = this.gridSize.x * this.gridSize.y;
for(var i = 0; i < sz; i++) {
// create(null) is to obviate hasOwnProperty
this.bounds[i] = Object.create(null);
this.points[i] = Object.create(null);
};
}
// removes and entity from the grid
GridSP.prototype.removeObj = function( eid) {
var l = this.objs[eid];
for(var j = 0; j < l.length; j++) {
var i = l[j];
delete this.points[i][eid];
delete this.bounds[i][eid];
}
delete this.objs[eid];
}
// use this one to add an entity
GridSP.prototype.addObj = function(eid) {
var aabb = game.getComp(eid, 'aabb');
var pos = game.getComp(eid, 'position');
this.insert(translateRect(aabb, pos), ptc(pos), eid);
}
// blindly adds, internal use only
GridSP.prototype.insert = function(rect, p, eid) {
// FIXME: min/max tests for edges
var minx = floor(min(rect.l, rect.r) / this.cellSize);
var maxx = ceil(max(rect.l, rect.r) / this.cellSize);
var miny = floor(min(rect.t, rect.b) / this.cellSize);
var maxy = ceil(max(rect.t, rect.b) / this.cellSize);
var l = [];
for(var y = miny; y <= maxy; y++) {
for(var x = minx; x <= maxx; x++) {
var i = x + y * this.gridSize.x;
this.points[i][eid] = p;
this.bounds[i][eid] = rect;
l.push(i);
}
}
this.objs[eid] = l;
}
// center to center, for things that are small enough for the bounds to not matter
GridSP.prototype.rangeRadiusFast = function(qp, rad) {
// FIXME: min/max tests for edges
var minx = floor((qp.x - rad) / this.cellSize);
var maxx = ceil((qp.x + rad) / this.cellSize);
var miny = floor((qp.y - rad) / this.cellSize);
var maxy = ceil((qp.y + rad) / this.cellSize);
var l = [];
for(var y = miny; y <= maxy; y++) {
for(var x = minx; x <= maxx; x++) {
var i = x + y * this.gridSize.x;
for(eid in this.points[i]) {
if(circleIntersect(qp, rad, this.points[i][eid], 0)) {
l.push(eid);
}
}
}
}
return l;
}
// this one runs as a system
GridSP.prototype.updatePositions = function() {
var comps = ['position', 'nextpos'];
runSystem(game.c, comps, function(ent) {
// there should be some extra cleverness in this function. simplicity for now.
// nothing in the grid actually moves atm
});
}