-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentityManager.js
194 lines (139 loc) · 4 KB
/
entityManager.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
entityManager.js
A module which handles arbitrary entity-management for "Asteroids"
We create this module as a single global object, and initialise it
with suitable 'data' and 'methods'.
"Private" properties are denoted by an underscore prefix convention.
*/
"use strict";
// Tell jslint not to complain about my use of underscore prefixes (nomen),
// my flattening of some indentation (white), or my use of incr/decr ops
// (plusplus).
//
/*jslint nomen: true, white: true, plusplus: true*/
var entityManager = {
// "PRIVATE" DATA
_rocks : [],
_Wires : [],
_players : [],
_bShowRocks : true,
// "PRIVATE" METHODS
_generateRocks : function() {
var i,
NUM_ROCKS = 4;
for (i = 0; i < NUM_ROCKS; ++i) {
this.generateRock();
}
},
_findNearestplayer : function(posX, posY) {
var closestplayer = null,
closestIndex = -1,
closestSq = 1000 * 1000;
for (var i = 0; i < this._players.length; ++i) {
var thisplayer = this._players[i];
var playerPos = thisplayer.getPos();
var distSq = util.wrappedDistSq(
playerPos.posX, playerPos.posY,
posX, posY,
g_canvas.width, g_canvas.height);
if (distSq < closestSq) {
closestplayer = thisplayer;
closestIndex = i;
closestSq = distSq;
}
}
return {
theplayer : closestplayer,
theIndex: closestIndex
};
},
_forEachOf: function(aCategory, fn) {
for (var i = 0; i < aCategory.length; ++i) {
fn.call(aCategory[i]);
}
},
// PUBLIC METHODS
// A special return value, used by other objects,
// to request the blessed release of death!
//
KILL_ME_NOW : -1,
// Some things must be deferred until after initial construction
// i.e. thing which need `this` to be defined.
//
deferredSetup : function () {
this._categories = [this._rocks, this._Wires, this._players];
},
init: function() {
//this._generateRocks();
//this._generateplayer();
},
fireWire: function(cx, cy, velX, velY, rotation) {
if(this._Wires.length === 0)
{
this._Wires.push(new Wire({
cx : cx
}));
}
},
generateRock : function(descr) {
/* this._rocks.push(new Rock(descr));*/
},
generateplayer : function(descr) {
this._players.push(new player(descr));
},
killNearestplayer : function(xPos, yPos) {
var theplayer = this._findNearestplayer(xPos, yPos).theplayer;
if (theplayer) {
theplayer.kill();
}
},
yoinkNearestplayer : function(xPos, yPos) {
var theplayer = this._findNearestplayer(xPos, yPos).theplayer;
if (theplayer) {
theplayer.setPos(xPos, yPos);
}
},
resetplayers: function() {
this._forEachOf(this._players, player.prototype.reset);
},
haltplayers: function() {
this._forEachOf(this._players, player.prototype.halt);
},
toggleRocks: function() {
this._bShowRocks = !this._bShowRocks;
},
update: function(du) {
for (var c = 0; c < this._categories.length; ++c) {
var aCategory = this._categories[c];
var i = 0;
while (i < aCategory.length) {
var status = aCategory[i].update(du);
if (status === this.KILL_ME_NOW) {
// remove the dead guy, and shuffle the others down to
// prevent a confusing gap from appearing in the array
aCategory.splice(i,1);
}
else {
++i;
}
}
}
if (this._rocks.length === 0) this._generateRocks();
},
render: function(ctx) {
var debugX = 10, debugY = 100;
for (var c = 0; c < this._categories.length; ++c) {
var aCategory = this._categories[c];
if (!this._bShowRocks &&
aCategory == this._rocks)
continue;
for (var i = 0; i < aCategory.length; ++i) {
aCategory[i].render(ctx);
//debug.text(".", debugX + i * 10, debugY);
}
debugY += 10;
}
}
}
// Some deferred setup which needs the object to have been created first
entityManager.deferredSetup();