Added LocationListener to subscribe to item location changes (specifically for belt and possibly cursor as well)

This commit is contained in:
Collin Smith 2020-01-17 03:16:35 -08:00
parent 1465af857c
commit d8ff97e4a5
2 changed files with 30 additions and 7 deletions

View File

@ -364,7 +364,7 @@ public class CharData implements ItemData.UpdateListener, Pool.Poolable {
itemData.cursor = itemData.unequip(bodyLoc);
item = itemData.getItem(itemData.cursor);
}
item.location = Location.CURSOR;
itemData.setLocation(item, Location.CURSOR);
}
public void cursorToBody(BodyLoc bodyLoc, boolean merc) {
@ -396,11 +396,12 @@ public class CharData implements ItemData.UpdateListener, Pool.Poolable {
public void cursorToBelt(int x, int y) {
assert itemData.cursor != ItemData.INVALID_ITEM;
Item item = itemData.getItem(itemData.cursor);
item.location = Location.BELT;
int i = itemData.cursor;
itemData.cursor = ItemData.INVALID_ITEM;
Item item = itemData.getItem(i);
item.gridX = (byte) x;
item.gridY = (byte) y;
itemData.cursor = ItemData.INVALID_ITEM;
itemData.setLocation(item, Location.BELT);
}
public void swapBeltItem(int i) {

View File

@ -28,6 +28,7 @@ public class ItemData {
final Array<AlternateListener> alternateListeners = new Array<>(false, 16);
final Array<StoreListener> storeListeners = new Array<>(false, 16);
final Array<LocationListener> locationListeners = new Array<>(false, 16);
final EnumIntMap<BodyLoc> equipped = new EnumIntMap<>(BodyLoc.class, INVALID_ITEM);
final Array<EquipListener> equipListeners = new Array<>(false, 16);
@ -172,7 +173,7 @@ public class ItemData {
Item item = itemData.get(i);
if (item.location == Location.STORED) notifyStoreRemoved(item);
cursor = i;
item.location = Location.CURSOR;
setLocation(item, Location.CURSOR);
}
void storeCursor(StoreLoc storeLoc, int x, int y) {
@ -183,7 +184,7 @@ public class ItemData {
void store(StoreLoc storeLoc, int i, int x, int y) {
Item item = itemData.get(i);
item.location = Location.STORED;
setLocation(item, Location.STORED);
item.storeLoc = storeLoc;
item.gridX = (byte) x;
item.gridY = (byte) y;
@ -197,7 +198,7 @@ public class ItemData {
void equip(BodyLoc bodyLoc, int i) {
Item item = itemData.get(i);
item.location = Location.EQUIPPED;
setLocation(item, Location.EQUIPPED);
item.bodyLoc = bodyLoc;
int j = equipped.put(bodyLoc, i);
assert j == INVALID_ITEM : "Item " + j + " should have been unequipped by this point.";
@ -325,4 +326,25 @@ public class ItemData {
void onAdded(ItemData items, StoreLoc storeLoc, Item item);
void onRemoved(ItemData items, StoreLoc storeLoc, Item item);
}
void setLocation(Item item, Location location) {
if (item.location != location) {
Location oldLocation = item.location;
item.location = location;
notifyLocationChanged(item, oldLocation);
}
}
public boolean addLocationListener(LocationListener l) {
locationListeners.add(l);
return true;
}
private void notifyLocationChanged(Item item, Location oldLocation) {
for (LocationListener l : locationListeners) l.onChanged(this, oldLocation, item.location, item);
}
public interface LocationListener {
void onChanged(ItemData items, Location oldLocation, Location location, Item item);
}
}