From: dsc Date: Sat, 12 Mar 2011 17:23:37 +0000 (-0800) Subject: Fixes intermittent teleport-to-(0,0) bullet issue. Dead shields were still being... X-Git-Url: http://git.less.ly:3516/?a=commitdiff_plain;h=46f85d1f7ed05bee1ed24c94adee8992f010d9da;p=tanks.git Fixes intermittent teleport-to-(0,0) bullet issue. Dead shields were still being notified to act(), despite being correctly removed from all the bookkeeping locations (pathmap, game, etc) ... except owner.components. This itself would have just been a memory leak except that act() did not test whether the shield was alive. So when a dead shield would move, it could still hit things--it was only looking for things to hit and not testing if it was itself dead. These phantom collisions would result in NaN coords, which becomes 0 when coerced to int by DOMElement. Bam. --- diff --git a/src/ezl/util/tree/quadtree.cjs b/src/ezl/util/tree/quadtree.cjs index 5b22c8b..c25a9a5 100644 --- a/src/ezl/util/tree/quadtree.cjs +++ b/src/ezl/util/tree/quadtree.cjs @@ -107,6 +107,12 @@ Y.subclass('QuadTree', { // 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)); }, diff --git a/src/tanks/map/pathing/traversal.cjs b/src/tanks/map/pathing/traversal.cjs index 45b185e..826b027 100644 --- a/src/tanks/map/pathing/traversal.cjs +++ b/src/tanks/map/pathing/traversal.cjs @@ -80,6 +80,18 @@ Y.subclass('Traversal', { // 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); diff --git a/src/tanks/thing/component.cjs b/src/tanks/thing/component.cjs index b2323c4..c9779db 100644 --- a/src/tanks/thing/component.cjs +++ b/src/tanks/thing/component.cjs @@ -33,11 +33,12 @@ Thing.subclass('Component', { init : function initComponent(owner){ - this.owner = owner; + this.owner = owner.addComponent(this); this.game = owner.game; Thing.init.call(this, owner.align); } + }) ; diff --git a/src/tanks/thing/player.cjs b/src/tanks/thing/player.cjs index d438aa9..0805a3d 100644 --- a/src/tanks/thing/player.cjs +++ b/src/tanks/thing/player.cjs @@ -36,7 +36,7 @@ Tank.subclass('Player', { - init : function init(align){ + init : function initPlayer(align){ Tank.init.call(this, align); this.gameLog = []; @@ -55,7 +55,8 @@ Tank.subclass('Player', { 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); } }); }, diff --git a/src/tanks/thing/shield.cjs b/src/tanks/thing/shield.cjs index ff7d62c..43a2820 100644 --- a/src/tanks/thing/shield.cjs +++ b/src/tanks/thing/shield.cjs @@ -60,6 +60,8 @@ Component.subclass('Shield', { }, 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; diff --git a/src/tanks/thing/thing.cjs b/src/tanks/thing/thing.cjs index 9bc1ecc..abd4000 100644 --- a/src/tanks/thing/thing.cjs +++ b/src/tanks/thing/thing.cjs @@ -23,6 +23,7 @@ Thing = exports['Thing'] = new evt.Class('Thing', { __mixins__ : [ Quantified, Speciated ], + __bind__ : [ 'onComponentDeath' ], // Config showAttackCooldown : null, @@ -161,6 +162,26 @@ new evt.Class('Thing', { 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 *** //