Repository navigation
Expand file tree
/
Copy pathSpaceObject.js
More file actions
351 lines (281 loc) · 9.26 KB
/
Copy pathSpaceObject.js
File metadata and controls
351 lines (281 loc) · 9.26 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*********************************************************************
* SpaceObject - base class, where some of the game physics happens
* Copyright (c) 2012 Steve Purkis
*
* var o = new SpaceObject( game, spatial );
*/
require('asteroidUtils.js');
var _spaceObjectId = 0; // OID: use a counter for now...
function SpaceObject(game, spatial) {
if (game) return this.initialize(game, spatial);
return this;
}
SpaceObject.prototype.preRender = function() {
; // override me
}
SpaceObject.prototype.createPreRenderCanvas = function(width, height) {
var canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
var ctx = canvas.getContext('2d');
return render = {
canvas: canvas,
ctx: ctx,
};
}
SpaceObject.prototype.shouldDraw = function(offset) {
var dX = this.x+this.radius - offset.x;
var dY = this.y+this.radius - offset.y;
if (dX < 0 || dY < 0) return false;
return true;
}
SpaceObject.prototype.draw = function(offset) {
throw this.constructor.toString() + ".draw not overriden";
// all objects should set:
this.x_last = this.x;
this.y_last = this.y;
};
SpaceObject.prototype.redraw = function() {
throw this.constructor.toString() + ".redraw not overriden";
// all objects should set:
this.x_last = this.x;
this.y_last = this.y;
};
SpaceObject.prototype.initialize = function(game, spatial) {
// if (spatial == null) spatial = {};
this.game = game;
this.ctx = game.ctx; // canvas context
this.id = this.createObjectId();
this.health = spatial.health || 100; // health as a percentage
this.healthChanged = false;
this.damage = spatial.damage || 0; // damage on impact
// call updateX/Y to set on-screen coords
this.x = 0; this.updateX(spatial.x); // starting position on x axis
this.y = 0; this.updateY(spatial.y); // starting position on y axis
this.image = spatial.image; // image used to draw object
this.stationary = spatial.stationary == null // should this object move?
? false
: spatial.stationary;
this.vX = spatial.vX || 0; // speed along X axis
this.vY = spatial.vY || 0; // speed along Y axis
this.maxV = spatial.maxV || 2; // max velocity
this.maxVSquared = this.maxV*this.maxV; // cache for speed
this.mass = spatial.mass || 0; // mass of object
this.radius = spatial.radius || 1; // radius of object
this.radiusSquared = Math.pow(this.radius,2); // cache for speed
this.thrust = spatial.initialThrust || 0; // thrust along facing
this.maxThrust = spatial.maxThrust || 0.5;
this.thrustChanged = false;
this.facing = spatial.facing || 0; // currently facing angle (rad)
this.spin = spatial.spin || 0; // spin in Rad/sec
this.maxSpin = deg_to_rad[10]; // max spin
this.attached = []; // objects this is attached to
this.colliding = []; // objects this is colliding with
this.update = true;
// Cache of commonly used calculations for performance
this.cache = {
G_x_mass: this.game.G * this.mass,
mass_x_2: 2 * this.mass
};
return this;
}
SpaceObject.prototype.createObjectId = function() {
var prefix = this.oid_prefix || 'obj';
return prefix + _spaceObjectId++;
}
/**
* returns true if this object has changed & needs re-drawing
*/
SpaceObject.prototype.updatePositions = function(objects) {
if (! this.update) return false;
var changed = false;
this.applyDelayedUpdates();
if (this.updateFacing(this.spin)) changed = true;
if (this.updateX(this.vX)) changed = true;
if (this.updateY(this.vY)) changed = true;
return changed;
}
/**
* hook to reset or save any state before update is called
*/
SpaceObject.prototype.resetBeforeUpdate = function() {
// reset changes from last update:
this.healthChanged = false;
this.thrustChanged = false;
}
/***
* updateease x & y coords, and return:
* true if the on-screen coords have changed
* false if they're the same
*/
SpaceObject.prototype.updateX = function(dX) {
if (this.stationary) return false;
if (dX == 0) return false;
this.x += dX;
// TODO: incremental draws
//would this movement be visible?
//if (Math.abs(this.x_last - this.x) < 0.1) return false;
return true;
}
SpaceObject.prototype.setX = function(x) {
if (this.stationary) return false;
this.x = x;
}
SpaceObject.prototype.updateY = function(dY) {
if (this.stationary) return false;
if (dY == 0) return false;
this.y += dY;
// TODO: incremental draws
// would this movement be visible?
//if (Math.abs(this.y_last - this.y) < 0.1) return false;
return true;
}
SpaceObject.prototype.setY = function(y) {
if (this.stationary) return false;
this.y = y;
}
SpaceObject.prototype.updateFacing = function(delta) {
if (delta == 0) return false;
this.facing += delta;
if (this.facing >= deg_to_rad[360] || this.facing <= deg_to_rad[360]) {
this.facing = this.facing % deg_to_rad[360];
}
if (this.facing < 0) {
this.facing = deg_to_rad[360] + this.facing;
}
return true;
}
// called before we update velocity
SpaceObject.prototype.delayUpdateVelocity = function(dvX, dvY) {
if (this._updates == null) this.init_updates();
this._updates.dvX += dvX;
this._updates.dvY += dvY;
}
SpaceObject.prototype.delaySetVelocity = function(vX, vY) {
if (this._updates == null) this.init_updates();
this._updates.set.push({vX: vX, vY: vY});
}
SpaceObject.prototype.init_updates = function() {
this._updates = {dvX: 0, dvY: 0, set: []};
}
SpaceObject.prototype.applyDelayedUpdates = function() {
if (! this._updates) return;
var u = this._updates;
delete this._updates;
// apply sets first: these are likely collisions, and acceleration due to
// gravity should still apply after a collision
if (u.set.length > 0) {
var new_vX = null;
var new_vY = null;
for (var i=0; i < u.set.length; i++) {
if (i==0) { new_vX = 0; new_vY = 0; }
new_vX += u.set[i].vX;
new_vY += u.set[i].vY;
}
new_vX += u.dvX;
new_vY += u.dvY;
this.setVelocity( new_vX, new_vY );
} else {
this.updateVelocity( u.dvX, u.dvY );
}
}
SpaceObject.prototype.updateVelocity = function(dX, dY) {
if (this.stationary) return false;
var newVx = this.vX + dX;
var newVy = this.vY + dY;
var magnitude_squared = newVx*newVx + newVy*newVy; // avoid sqrt
if (magnitude_squared > this.maxVSquared) {
// scale back newV along same vector
var angle = Math.atan2(newVy, newVx);
newVx = Math.cos(angle) * this.maxV;
newVy = Math.sin(angle) * this.maxV;
}
this.vX = newVx;
this.vY = newVy;
}
SpaceObject.prototype.setVelocity = function(vX, vY) {
this.vX = vX;
this.vY = vY;
}
SpaceObject.prototype.accelerateAlong = function(angle, thrust) {
var accel = thrust/this.mass;
var dX = Math.cos(angle) * accel;
var dY = Math.sin(angle) * accel;
this.updateVelocity(dX, dY);
}
SpaceObject.prototype.incSpin = function(delta) {
if (delta != 0) {
this.spin += delta;
if (this.spin > this.maxSpin) {
this.spin = this.maxSpin;
} else if (this.spin < -this.maxSpin) {
this.spin = -this.maxSpin;
}
}
}
SpaceObject.prototype.collided = function(object, collision) {
this.colliding[object.id] = object;
if (this.damage) {
var damageDone = this.damage;
if (collision.impactSpeed != null) {
damageDone = Math.ceil(damageDone * collision.impactSpeed);
}
object.decHealth( damageDone );
// console.log( this.id + " -->X " + object.id + " damage: " + damageDone );
}
}
SpaceObject.prototype.collidingWith = function(object) {
if (this.colliding[object.id]) return true;
return false;
}
SpaceObject.prototype.stopCollidingWith = function(object) {
delete this.colliding[object.id];
}
SpaceObject.prototype.decHealth = function(delta) {
this.healthChanged = true;
this.health -= delta;
if (this.health <= 0) {
this.health = -1;
this.die();
}
}
SpaceObject.prototype.incHealth = function(delta) {
this.healthChanged = true;
this.health += delta;
if (this.health > 100) this.health = 100;
}
SpaceObject.prototype.die = function() {
this.died = true;
this.update = false;
this.game.objectDied( this );
}
SpaceObject.prototype.attach = function(object) {
this.attached[object.id] = object;
}
SpaceObject.prototype.attachedTo = function(object) {
if (this.attached[object.id]) return true;
return false;
}
SpaceObject.prototype.detach = function(object) {
delete this.attached[object.id];
}
SpaceObject.prototype.incThrust = function(delta) {
this.thrustChanged = true;
this.thrust += delta;
if (this.thrust > this.maxThrust) this.thrust = this.maxThrust;
}
SpaceObject.prototype.decThrust = function(delta) {
this.thrustChanged = true;
this.thrust -= delta;
if (this.thrust < -this.maxThrust) this.thrust = -this.maxThrust;
}
SpaceObject.prototype.resetThrust = function(delta) {
this.thrustChanged = true;
this.thrust=0;
}
SpaceObject.prototype.toString = function() {
return this.id + '=('+
'x:' + this.x.toFixed(1) + ', y:' + this.y.toFixed(1) +
', vX:' + this.vX.toFixed(5) + ', vY:' + this.vY.toFixed(5) +
')';
}