From 57589f73124a6461e9167aedb917098674f24ba0 Mon Sep 17 00:00:00 2001 From: Collin Smith Date: Thu, 16 Jan 2020 16:17:54 -0800 Subject: [PATCH] Moved pickup and store methods to ItemData Added support for store listeners CharData store methods are now simple calls up to player ItemData reference --- core/src/com/riiablo/save/CharData.java | 13 ++------ core/src/com/riiablo/save/ItemData.java | 43 +++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/core/src/com/riiablo/save/CharData.java b/core/src/com/riiablo/save/CharData.java index b97c6b85..c668fe9e 100644 --- a/core/src/com/riiablo/save/CharData.java +++ b/core/src/com/riiablo/save/CharData.java @@ -326,10 +326,7 @@ public class CharData implements ItemData.UpdateListener, Pool.Poolable { } public void itemToCursor(int i) { - assert itemData.cursor == ItemData.INVALID_ITEM; - itemData.cursor = i; - Item item = itemData.getItem(i); - item.location = Location.CURSOR; + itemData.pickup(i); } public void storeToCursor(int i) { @@ -337,13 +334,7 @@ public class CharData implements ItemData.UpdateListener, Pool.Poolable { } public void cursorToStore(StoreLoc storeLoc, int x, int y) { - assert itemData.cursor != ItemData.INVALID_ITEM; - Item item = itemData.getItem(itemData.cursor); - item.location = Location.STORED; - item.storeLoc = storeLoc; - item.gridX = (byte) x; - item.gridY = (byte) y; - itemData.cursor = ItemData.INVALID_ITEM; + itemData.storeCursor(storeLoc, x, y); } public void swapStoreItem(int i, StoreLoc storeLoc, int x, int y) { diff --git a/core/src/com/riiablo/save/ItemData.java b/core/src/com/riiablo/save/ItemData.java index 5f12ee83..96a85575 100644 --- a/core/src/com/riiablo/save/ItemData.java +++ b/core/src/com/riiablo/save/ItemData.java @@ -27,6 +27,8 @@ public class ItemData { int alternate = D2S.PRIMARY; final Array alternateListeners = new Array<>(false, 16); + final Array storeListeners = new Array<>(false, 16); + final EnumIntMap equipped = new EnumIntMap<>(BodyLoc.class, INVALID_ITEM); final Array equipListeners = new Array<>(false, 16); @@ -165,6 +167,29 @@ public class ItemData { return getLocation(Location.STORED, storeLoc); } + void pickup(int i) { + assert cursor == INVALID_ITEM; + Item item = itemData.get(i); + if (item.location == Location.STORED) notifyStoreRemoved(item); + cursor = i; + item.location = Location.CURSOR; + } + + void storeCursor(StoreLoc storeLoc, int x, int y) { + assert cursor != ItemData.INVALID_ITEM; + store(storeLoc, cursor, x, y); + cursor = INVALID_ITEM; + } + + void store(StoreLoc storeLoc, int i, int x, int y) { + Item item = itemData.get(i); + item.location = Location.STORED; + item.storeLoc = storeLoc; + item.gridX = (byte) x; + item.gridY = (byte) y; + notifyStoreAdded(item); + } + void equip(BodyLoc bodyLoc, Item item) { assert !itemData.contains(item, true); equip(bodyLoc, add(item)); @@ -282,4 +307,22 @@ public class ItemData { public interface UpdateListener { void onUpdated(ItemData itemData); } + + public boolean addStoreListener(StoreListener l) { + storeListeners.add(l); + return true; + } + + private void notifyStoreAdded(Item item) { + for (StoreListener l : storeListeners) l.onAdded(this, item.storeLoc, item); + } + + private void notifyStoreRemoved(Item item) { + for (StoreListener l : storeListeners) l.onRemoved(this, item.storeLoc, item); + } + + public interface StoreListener { + void onAdded(ItemData items, StoreLoc storeLoc, Item item); + void onRemoved(ItemData items, StoreLoc storeLoc, Item item); + } }