Inventory.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #pragma once
  2. #include <Vec3.h>
  3. #include <ReferenceCounter.h>
  4. #include <HashMap.h>
  5. #include <Critical.h>
  6. #include "ItemSlot.h"
  7. #include "Area.h"
  8. class ItemFilter;
  9. class Inventory;
  10. class InventoryInteraction
  11. {
  12. private:
  13. Inventory* current;
  14. Inventory* other;
  15. Direction dir;
  16. void lock();
  17. void unlock();
  18. void transaction(Inventory* zSource, Inventory* zTarget, ItemFilter* zFilter, Direction sourceView, Direction targetView, int count);
  19. public:
  20. InventoryInteraction(Inventory* zCurrent, Inventory* zOther, Direction dir);
  21. InventoryInteraction(const InventoryInteraction& interaction);
  22. ~InventoryInteraction();
  23. InventoryInteraction& operator=(const InventoryInteraction& data);
  24. void endInteraction();
  25. void pullItems(int count, ItemFilter* zFilter);
  26. void pushItems(int count, ItemFilter* zFilter);
  27. };
  28. class Inventory : public virtual Framework::ReferenceCounter
  29. {
  30. private:
  31. Framework::RCArray<ItemSlot>* pullSlotsOrder;
  32. Framework::RCArray<ItemSlot>* pushSlotsOrder;
  33. Framework::HashMap<int, Framework::Array<ItemSlot*>*>* itemCache;
  34. Framework::Critical cs;
  35. int nextSlotId;
  36. void updateCache(ItemSlot* zSlot, int beforeKey);
  37. protected:
  38. Framework::Vec3<float> location;
  39. virtual bool allowPullStack(ItemSlot* zSlot, Direction dir) const;
  40. virtual bool allowPushStack(ItemSlot* zSlot, Direction dir, const Item* zItem, int& count) const;
  41. virtual void afterPullStack(ItemSlot* zSlot, Direction dir, const Item* zItem, int count);
  42. virtual void afterPushStack(ItemSlot* zSlot, Direction dir, const Item* zItem, int count);
  43. virtual void loadInventory(Framework::StreamReader* zReader);
  44. virtual void saveInventory(Framework::StreamWriter* zWriter);
  45. ItemSlot* zSlot(int id);
  46. public:
  47. Inventory(const Framework::Vec3<float> location, bool hasInventory);
  48. virtual ~Inventory();
  49. void addSlot(ItemSlot* slot);
  50. void localTransaction(Framework::Array< ItemSlot* >* zSourceSlots, Framework::Array< ItemSlot* >* zTargetSlots, ItemFilter* zFilter, int count, Direction outDir, Direction inDir);
  51. ItemStack* takeItemsOut(ItemSlot* zSlot, int count, Direction dir);
  52. virtual void addItems(ItemStack* items, Direction dir);
  53. InventoryInteraction interactWith(Inventory* zInventory, Direction dir);
  54. void unsaveAddItem(ItemStack* zStack, Direction dir);
  55. int numberOfAddableItems(const ItemStack* zStack, Direction dir) const;
  56. Framework::Iterator<ItemSlot*> begin();
  57. Framework::Iterator<ItemSlot*> end();
  58. friend InventoryInteraction;
  59. };