ItemStack.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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) item->release();
  15. }
  16. void ItemStack::setMaxSize(int size)
  17. {
  18. maxSize = size;
  19. }
  20. ItemStack* ItemStack::split(int size)
  21. {
  22. size = MIN(size, this->size);
  23. this->size -= size;
  24. return new ItemStack(
  25. dynamic_cast<Item*>(item->getThis()), size, item->getMaxStackSize());
  26. }
  27. Item* ItemStack::extractFromStack()
  28. {
  29. if (size == 0) return 0;
  30. --size;
  31. return item->zItemType()->cloneItem(item);
  32. }
  33. bool ItemStack::addToStack(Item* item)
  34. {
  35. if (size < maxSize && this->item->canBeStackedWith(item))
  36. {
  37. ++size;
  38. item->release();
  39. return true;
  40. }
  41. item->release();
  42. return false;
  43. }
  44. void ItemStack::addItemStack(ItemStack* zItemStack)
  45. {
  46. if (zItemStack->zItem()->canBeStackedWith(item))
  47. {
  48. int numToAdd = MIN(zItemStack->getSize(), maxSize - size);
  49. size += numToAdd;
  50. zItemStack->size -= numToAdd;
  51. }
  52. }
  53. bool ItemStack::isStackable(Item* zItem) const
  54. {
  55. return item->canBeStackedWith(zItem);
  56. }
  57. const Item* ItemStack::zItem() const
  58. {
  59. return item;
  60. }
  61. int ItemStack::getSize() const
  62. {
  63. return size;
  64. }
  65. int ItemStack::getMaxSize() const
  66. {
  67. return maxSize;
  68. }