Skip to content

Instantly share code, notes, and snippets.

@troygilbert
Created April 29, 2010 20:47
Show Gist options
  • Save troygilbert/384218 to your computer and use it in GitHub Desktop.
Save troygilbert/384218 to your computer and use it in GitHub Desktop.
Character Equips Item
/** This base class works for both players and enemies, as far as items are concerned. **/
public class Character
{
/** This is a list of items equiped on the character. **/
public var equipedItems:Array = [ ];
/** Equips an item on the character, unequiping it from its current owner. **/
public function equip(item:Item):void
{
if (item.owner) item.owner.unequip(item); // unequip item from its current owner
equipedItems.push(item); // add the item to our equiped items list
item.owner = this; // we're now the owner of this item
}
/** Unequips an item from the character. **/
public function unequip(item:Item):void
{
var index:int = equipedItems.indexOf(item); // find the item in our equiped list
if (index == -1) return; // item isn't equiped on us, do nothing
equipedItems.splice(index, 1); // cut out the old item from our list
item.owner = null; // no one owns the item now
}
}
/** Generic base class for items that track owners. It's often convenient to know who owns an item. **/
public class Item
{
public var owner:Character;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment