Skip to content

Instantly share code, notes, and snippets.

@demoth
Last active August 29, 2015 14:19
Show Gist options
  • Save demoth/0eabf4e956e3fcf9eb57 to your computer and use it in GitHub Desktop.
Save demoth/0eabf4e956e3fcf9eb57 to your computer and use it in GitHub Desktop.
inventory
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