Last active
August 29, 2015 14:19
-
-
Save demoth/0eabf4e956e3fcf9eb57 to your computer and use it in GitHub Desktop.
inventory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Actor { | |
private static final ArrayList<ItemSlot> BOTH_HANDS = [MAIN_HAND, OFF_HAND] | |
private static final ArrayList<ItemSlot> BOTH_RINGS = [RING_RIGHT, RING_LEFT] | |
// items | |
Collection<Item> inventory = [] | |
Map<ItemSlot, Item> equipment = [:] | |
void useOrEquip(int itemId, ItemSlot targetSlot) { | |
Item item = inventory.find { it.id == itemId } | |
if (item) { | |
switch (item.type) { | |
case ItemType.EQUIPABLE: | |
List<ItemSlot> slotsToEmpty | |
ItemSlot destination = item.slot | |
if (item.isTwoHanded) { | |
slotsToEmpty = BOTH_HANDS | |
destination = MAIN_HAND | |
} else if (equipment.get(MAIN_HAND).isTwoHanded && targetSlot in BOTH_HANDS) { | |
slotsToEmpty = [MAIN_HAND] | |
destination | |
} else if ((equipment.keySet().containsAll(BOTH_HANDS) && targetSlot in BOTH_HANDS) | |
|| (equipment.keySet().containsAll(BOTH_RINGS) && targetSlot in BOTH_RINGS)) { | |
slotsToEmpty = [targetSlot] | |
destination = targetSlot | |
} else { | |
slotsToEmpty = [item.slot] | |
} | |
slotsToEmpty.each { inventory.add(equipment.remove(it)) } | |
inventory.remove item | |
equipment.put destination, item | |
break | |
case ItemType.USABLE: | |
break | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment