ItemStack.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include "ItemStack.h"
  2. #include "Item.h"
  3. ItemStack::ItemStack(Item* item, int currentSize, int maxSize)
  4. : ReferenceCounter(),
  5. item(item),
  6. size(currentSize),
  7. maxSize(maxSize)
  8. {}
  9. ItemStack::ItemStack(Item* item, int currentSize)
  10. : ItemStack(item, currentSize, item->getMaxStackSize())
  11. {}
  12. ItemStack::~ItemStack()
  13. {
  14. if (item)
  15. item->release();
  16. }
  17. void ItemStack::setMaxSize(int size)
  18. {
  19. maxSize = size;
  20. }
  21. ItemStack* ItemStack::split(int size)
  22. {
  23. size = MIN(size, this->size);
  24. this->size -= size;
  25. return new ItemStack(dynamic_cast<Item*>(item->getThis()), size, item->getMaxStackSize());
  26. }
  27. Item* ItemStack::extractFromStack()
  28. {
  29. if (size == 0)
  30. return 0;
  31. --size;
  32. return item->zItemType()->cloneItem(item);
  33. }
  34. bool ItemStack::addToStack(Item* item)
  35. {
  36. if (size < maxSize && this->item->canBeStackedWith(item))
  37. {
  38. ++size;
  39. item->release();
  40. return true;
  41. }
  42. item->release();
  43. return false;
  44. }
  45. void ItemStack::addItemStack(ItemStack* zItemStack)
  46. {
  47. if (zItemStack->zItem()->canBeStackedWith(item))
  48. {
  49. int numToAdd = MIN(zItemStack->getSize(), maxSize - size);
  50. size += numToAdd;
  51. zItemStack->size -= numToAdd;
  52. }
  53. }
  54. bool ItemStack::isStackable(Item* zItem) const
  55. {
  56. return item->canBeStackedWith(zItem);
  57. }
  58. const Item* ItemStack::zItem() const
  59. {
  60. return item;
  61. }
  62. int ItemStack::getSize() const
  63. {
  64. return size;
  65. }
  66. int ItemStack::getMaxSize() const
  67. {
  68. return maxSize;
  69. }