lttl refactor of Item methods.
authordsc <david.schoonover@gmail.com>
Fri, 25 Feb 2011 07:52:31 +0000 (23:52 -0800)
committerdsc <david.schoonover@gmail.com>
Fri, 25 Feb 2011 07:52:31 +0000 (23:52 -0800)
src/tanks/thing/item.cjs
src/tanks/thing/thing.cjs

index e7b89b3..21cae60 100644 (file)
@@ -20,7 +20,7 @@ Item =
 exports['Item'] =
 Thing.subclass('Item', {
     // __mixins__ : [  ],
-    __bind__ : [ 'onCollide', 'onAcquired', 'onLost', 'onEquip', 'onUnequip', 'onBuffDeath' ],
+    __bind__ : [ 'equip', 'unequip', 'onCollide', 'onAcquired', 'onLost', 'onBuffDeath' ],
     isItem : true,
     
     align : 0, // 0 reserved for neutral units
@@ -73,8 +73,8 @@ Thing.subclass('Item', {
         this.on('collide',      this.onCollide);
         this.on('item.acquire', this.onAcquired);
         this.on('item.lose',    this.onLost);
-        this.on('item.equip',   this.onEquip);
-        this.on('item.unequip', this.onUnequip);
+        this.on('item.equip',   this.equip);
+        this.on('item.unequip', this.unequip);
     },
     
     activate : function activate(){
@@ -95,64 +95,68 @@ Thing.subclass('Item', {
         return this;
     },
     
-    /// Event Handlers ///
-    
-    onBuffDeath : function onBuffDeath(evt, buff){
-        this.currentBuffs.remove(buff);
-        this.currentActive.remove(buff);
-        this.currentPassive.remove(buff);
+    removeFromMap : function removeFromMap(){
+        this.remove(); // removes map object
+        this.game.map.removeBlocker(this); // remove map zone
     },
     
-    onEquip : function onEquip(evt){
-        // console.log(this+' equipped!');
-        var owner   = this.owner
-        ,   proj    = this.projectile
-        ,   buffs   = this.currentBuffs
-        ,   passive = this.currentPassive
-        ;
+    /**
+     * Equips this item to owner or given unit. Does not manipulate inventory, or remove from map.
+     * @param {Tank} [unit=this.owner] Unit to equip. If omitted, uses this.owner; if unowned, does nothing.
+     * @return {this}
+     */
+    equip : function equip(unit){
+        if (unit && unit.isUnit)
+            this.owner = unit;
+        
+        if (!this.owner)
+            return this;
+        
+        this.isEquipped = true;
+        if ( this.currentPassive.length )
+            this.currentPassive.invoke('die', 'item.reequipped');
+        
+        var equipped = this.passives.invoke('instantiate', this.owner);
+        equipped.invoke('on', 'buff.die', this.onBuffDeath);
+        this.currentBuffs.extend(equipped);
+        this.currentPassive.extend(equipped);
+        
+        if (this.projectile)
+            this.owner.projectile = this.projectile;
         
-        if ( owner ) {
-            this.isEquipped = true;
-            if ( passive.length )
-                passive.invoke('die', 'item.reequipped');
-            
-            var equipped = this.passives.invoke('instantiate', owner);
-            equipped.invoke('on', 'buff.die', this.onBuffDeath);
-            buffs.extend(equipped);
-            passive.extend(equipped);
-            
-            if (proj) owner.projectile = proj;
-        }
         return this;
     },
     
-    onUnequip : function onUnequip(evt){
-        // console.log(this+' unequipped!');
-        var owner = this.owner
-        ,   proj  = this.projectile
-        ;
-        
+    /**
+     * Unequips item from owner.
+     * @return {this}
+     */
+    unequip : function unequip(){
         this.isEquipped = false;
+        var owner = this.owner
+        ,   proj  = this.projectile;
         if ( owner ) {
             this.currentPassive.invoke('die', 'item.unequipped');
             if (proj && proj === owner.projectile) // only reset projectile if it's mine
                 owner.projectile = owner.defaultProjectile;
         }
+        
         return this;
     },
     
+    /// Event Handlers ///
+    
     onCollide : function onCollide(evt){
         var unit = evt.data.unit
         ,   inv  = unit.inventory
         if ( inv && inv.canAddItem(this) )
-            inv.addItem(this);
+            inv.addItem(this); // fires 'item.acquire' if successful
+        // TODO: else ignore for this traversal
     },
     
     onAcquired : function onAcquired(evt, container){
         this.owner = evt.data.unit;
-        // console.log(this.owner+' acquired '+this+' ('+this.desc+')!');
-        this.remove(); // removes map object
-        this.game.map.removeBlocker(this); // remove map zone
+        this.removeFromMap();
     },
     
     onLost : function onLost(evt){
@@ -172,9 +176,18 @@ Thing.subclass('Item', {
         this.owner = null;
         
         if ( /\blost\.drop\b/.test(evt.type) )
-            unit.game.addUnit(this, unit.loc); // TODO: stop item from being instantly picked up.
+            unit.game.addUnit(this, unit.loc); // TODO: stop item from being instantly picked up, heh.
     },
     
+    onBuffDeath : function onBuffDeath(evt, buff){
+        this.currentBuffs.remove(buff);
+        this.currentActive.remove(buff);
+        this.currentPassive.remove(buff);
+    },
+    
+    
+    /// UI ///
+    
     render : function render(parent){
         this.remove();
         var loc = this.loc
index fbe7e42..da41a46 100644 (file)
@@ -56,11 +56,14 @@ new evt.Class('Thing', {
     // Unit Flags
     blocking     : BoundsType.BLOCKING,
     density      : DensityType.DENSE,
-    isWall       : false,
+    
+    isUnit       : true,
     isCombatant  : false,
+    isWall       : false,
     isProjectile : false,
-    isRenderable : false,       // Agent will present itself for rendering when ready // FIXME: stupid hack
+    
     active       : true,        // Agent takes actions?
+    isRenderable : false,       // Agent will present itself for rendering when ready // FIXME: stupid hack
     isReflective : false,       // Projectiles bounce off agent rather than explode?
     hasInventory : false,       // Agent can acquire items?
     dropOnDeath  : false,       // Agent drops all items in inventory on death?