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
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(){
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){
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