12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- #pragma once
- #include <Vec3.h>
- #include <ReferenceCounter.h>
- #include <HashMap.h>
- #include <Critical.h>
- #include <ImmutablePair.h>
- #include "ItemSlot.h"
- #include "Area.h"
- class ItemFilter;
- class Inventory;
- class NetworkMessage;
- class Entity;
- class InventoryInteraction
- {
- private:
- Inventory* current;
- Inventory* other;
- Direction dir;
- void lock();
- void unlock();
- void transaction(Inventory* zSource, Inventory* zTarget, ItemFilter* zFilter, Direction sourceView, Direction targetView, int count);
- public:
- InventoryInteraction(Inventory* zCurrent, Inventory* zOther, Direction dir);
- InventoryInteraction(const InventoryInteraction& interaction);
- ~InventoryInteraction();
- InventoryInteraction& operator=(const InventoryInteraction& data);
- void endInteraction();
- void pullItems(int count, ItemFilter* zFilter);
- void pushItems(int count, ItemFilter* zFilter);
- };
- class Inventory : public virtual Framework::ReferenceCounter
- {
- private:
- Framework::Array<Framework::ImmutablePair<int, Framework::Text>> observers;
- Framework::RCArray<ItemSlot>* pullSlotsOrder;
- Framework::RCArray<ItemSlot>* pushSlotsOrder;
- Framework::HashMap<int, Framework::Array<ItemSlot*>*>* itemCache;
- Framework::Critical cs;
- Framework::Array<std::function<void(ItemSlot* zSlot, Direction dir, const Item* zItem, int count)>> afterPullStackCalls;
- Framework::Array<std::function<void(ItemSlot* zSlot, Direction dir, const Item* zItem, int count)>> afterPushStackCalls;
- Framework::Array<std::function<void(Entity* zSource, Framework::Text id)>> observerAddedCalls;
- int nextSlotId;
- void updateCache(ItemSlot* zSlot, int beforeKey);
- protected:
- Framework::Vec3<float> location;
- virtual bool allowPullStack(ItemSlot* zSlot, Direction dir) const;
- virtual bool allowPushStack(ItemSlot* zSlot, Direction dir, const Item* zItem, int& count) const;
- virtual void afterPullStack(ItemSlot* zSlot, Direction dir, const Item* zItem, int count);
- virtual void afterPushStack(ItemSlot* zSlot, Direction dir, const Item* zItem, int count);
- virtual void loadInventory(Framework::StreamReader* zReader);
- virtual void saveInventory(Framework::StreamWriter* zWriter);
- void removeObserver(Entity* zSource, Framework::Text id);
- void addObserver(Entity* zSource, Framework::Text id);
- public:
- Inventory(const Framework::Vec3<float> location, bool hasInventory);
- virtual ~Inventory();
- void notyObservers(NetworkMessage* msg);
- const ItemSlot* zSlot(int id) const;
- void addSlot(ItemSlot* slot);
- void localTransaction(Framework::Array< ItemSlot* >* zSourceSlots, Framework::Array<ItemSlot*>* zTargetSlots, ItemFilter* zFilter, int count, Direction outDir, Direction inDir);
- ItemStack* takeItemsOut(ItemSlot* zSlot, int count, Direction dir);
- virtual void addItems(ItemStack* zItems, Direction dir);
- virtual void addItems(ItemSlot* zSlot, ItemStack* zItems, Direction dir);
- InventoryInteraction interactWith(Inventory* zInventory, Direction dir);
- void unsaveAddItem(ItemStack* zStack, Direction dir);
- int numberOfAddableItems(const ItemStack* zStack, Direction dir) const;
- Framework::Iterator<ItemSlot*> begin();
- Framework::Iterator<ItemSlot*> end();
- void inventoryApi(Framework::StreamReader* zRequest, NetworkMessage* zResponse, Entity* zSource);
- void registerAfterPullStackCall(std::function<void(ItemSlot* zSlot, Direction dir, const Item* zItem, int count)> call);
- void registerAfterPushStackCall(std::function<void(ItemSlot* zSlot, Direction dir, const Item* zItem, int count)> call);
- void registerObserverAddedCall(std::function<void(Entity* zSource, Framework::Text id)> call);
- friend InventoryInteraction;
- };
|