Inventoty.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include "Inventory.h"
  2. #include "Area.h"
  3. #include "Registries.h"
  4. using namespace Framework;
  5. Inventory::Inventory(const Framework::Vec3<float> location, bool hasInventory)
  6. : ReferenceCounter(),
  7. location(location)
  8. {
  9. if (hasInventory)
  10. {
  11. pullSlotsOrder = new Framework::RCArray<ItemSlot>();
  12. pushSlotsOrder = new Framework::RCArray<ItemSlot>();
  13. }
  14. else
  15. {
  16. pullSlotsOrder = 0;
  17. pushSlotsOrder = 0;
  18. }
  19. }
  20. Inventory::~Inventory()
  21. {
  22. if (pullSlotsOrder)
  23. pullSlotsOrder->release();
  24. if (pushSlotsOrder)
  25. pushSlotsOrder->release();
  26. }
  27. void Inventory::loadInventory(Framework::StreamReader* zReader)
  28. {
  29. if (pushSlotsOrder)
  30. {
  31. for (auto slot : *pushSlotsOrder)
  32. {
  33. int size = 0;
  34. zReader->lese((char*)&size, 4);
  35. if (size != 0)
  36. {
  37. int id = 0;
  38. zReader->lese((char*)&id, 4);
  39. Item* item = STATIC_REGISTRY(ItemType).zElement(id)->loadItem(zReader);
  40. slot->setItems(new ItemStack(item, size));
  41. }
  42. else
  43. slot->setItems(0);
  44. }
  45. }
  46. }
  47. void Inventory::addSlot(ItemSlot* slot)
  48. {
  49. int pullPrio = slot->getPullPriority();
  50. int pushPrio = slot->getPushPriority();
  51. int index = 0;
  52. for (auto stack : *pullSlotsOrder)
  53. {
  54. if (stack->getPullPriority() > pullPrio)
  55. break;
  56. index++;
  57. }
  58. pullSlotsOrder->add(dynamic_cast<ItemSlot*>(slot->getThis()), index);
  59. index = 0;
  60. for (auto stack : *pushSlotsOrder)
  61. {
  62. if (stack->getPushPriority() > pushPrio)
  63. break;
  64. index++;
  65. }
  66. pushSlotsOrder->add(slot, index);
  67. }
  68. Framework::Iterator<ItemSlot*> Inventory::begin()
  69. {
  70. return pullSlotsOrder->begin();
  71. }
  72. Framework::Iterator<ItemSlot*> Inventory::end()
  73. {
  74. return pullSlotsOrder->end();
  75. }