Fixes intermittent teleport-to-(0,0) bullet issue. Dead shields were still being...
authordsc <david.schoonover@gmail.com>
Sat, 12 Mar 2011 17:23:37 +0000 (09:23 -0800)
committerdsc <david.schoonover@gmail.com>
Sat, 12 Mar 2011 17:23:37 +0000 (09:23 -0800)
src/ezl/util/tree/quadtree.cjs
src/tanks/map/pathing/traversal.cjs
src/tanks/thing/component.cjs
src/tanks/thing/player.cjs
src/tanks/thing/shield.cjs
src/tanks/thing/thing.cjs

index 5b22c8b..c25a9a5 100644 (file)
@@ -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));
     },
index 45b185e..826b027 100644 (file)
@@ -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);
             
index b2323c4..c9779db 100644 (file)
@@ -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);
     }
     
+    
 })
 ;
index d438aa9..0805a3d 100644 (file)
@@ -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);
             }
         });
     },
index ff7d62c..43a2820 100644 (file)
@@ -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;
index 9bc1ecc..abd4000 100644 (file)
@@ -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 *** //