Inventoty.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. }