Inventoty.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. }
  43. }
  44. }
  45. void Inventory::addSlot( ItemSlot* slot )
  46. {
  47. int pullPrio = slot->getPullPriority();
  48. int pushPrio = slot->getPushPriority();
  49. int index = 0;
  50. for( auto stack : *pullSlotsOrder )
  51. {
  52. if( stack->getPullPriority() > pullPrio )
  53. break;
  54. index++;
  55. }
  56. pullSlotsOrder->add( dynamic_cast<ItemSlot*>(slot->getThis()), index );
  57. index = 0;
  58. for( auto stack : *pushSlotsOrder )
  59. {
  60. if( stack->getPushPriority() > pushPrio )
  61. break;
  62. index++;
  63. }
  64. pushSlotsOrder->add( slot, index );
  65. }