// return acc;
// },
+ has : function has(value){
+ return this.reduce(function(acc, v, r, tree){
+ return acc || (v === value);
+ }, false, this);
+ },
+
set : function set(x1,y1, x2,y2, value){
return this._set(new Region(x1,y1, x2,y2, value));
},
// Calculate how much time we actually used
var consumed = tr.timeToMove(to.x-or.x, to.y-or.y);
+ // if (isNaN(consumed)) {
+ // console.error('OMFG NaN!', {
+ // thing: this.thing,
+ // blocker: this.blocker,
+ // to : to,
+ // origin : or,
+ // thisTo: this.to,
+ // bbox: this.bbox,
+ // trajectory: tr,
+ // dt: dt
+ // });
+ // }
this.consumeTime(consumed);
// console.log('stepTo('+dt+') --> wanted '+oto+' but BLOCKED! by '+this.blocker+', so ending at '+to+', consuming '+consumed);
init : function initComponent(owner){
- this.owner = owner;
+ this.owner = owner.addComponent(this);
this.game = owner.game;
Thing.init.call(this, owner.align);
}
+
})
;
- init : function init(align){
+ init : function initPlayer(align){
Tank.init.call(this, align);
this.gameLog = [];
var self = this;
this.game.on('ready', function(){
for (var i=0; i<1; i += 0.25) {
- self.components.push( Shield.create('shield', self, i * 2*Math.PI) );
+ // Component.init will add it to the owner's bookkeeping
+ Shield.create('shield', self, i * 2*Math.PI);
}
});
},
},
act : function act(elapsed, now){
+ if (this.dead) return this;
+
var oloc = this.owner.loc
, tr = this.trajectory;
tr.x = oloc.x; tr.y = oloc.y;
exports['Thing'] =
new evt.Class('Thing', {
__mixins__ : [ Quantified, Speciated ],
+ __bind__ : [ 'onComponentDeath' ],
// Config
showAttackCooldown : null,
return this.trajectory.comesWithin(pt, w,h);
},
+ addComponent : function addComponent(c){
+ var comps = this.components;
+ if ( c && !comps.has(c) ) {
+ comps.push(c);
+ c.on('destroy', this.onComponentDeath);
+ }
+ return this;
+ },
+
+ onComponentDeath : function onComponentDeath(evt){
+ this.removeComponent( evt.data.unit );
+ },
+
+ removeComponent : function removeComponent(c){
+ if (c) {
+ this.components.remove(c);
+ c.removeListener('destroy', this.onComponentDeath);
+ }
+ return this;
+ },
// *** Gameplay Methods *** //