Moved pickup and store methods to ItemData

Added support for store listeners
CharData store methods are now simple calls up to player ItemData reference
This commit is contained in:
Collin Smith 2020-01-16 16:17:54 -08:00
parent d20f8fcab4
commit 57589f7312
2 changed files with 45 additions and 11 deletions

View File

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

View File

@ -27,6 +27,8 @@ public class ItemData {
int alternate = D2S.PRIMARY;
final Array<AlternateListener> alternateListeners = new Array<>(false, 16);
final Array<StoreListener> storeListeners = new Array<>(false, 16);
final EnumIntMap<BodyLoc> equipped = new EnumIntMap<>(BodyLoc.class, INVALID_ITEM);
final Array<EquipListener> 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);
}
}