123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- #include "ItemStack.h"
- #include "Item.h"
- ItemStack::ItemStack(Item* item, int currentSize, int maxSize)
- : ReferenceCounter(),
- item(item),
- size(currentSize),
- maxSize(maxSize)
- {}
- ItemStack::ItemStack(Item* item, int currentSize)
- : ItemStack(item, currentSize, item->getMaxStackSize())
- {}
- ItemStack::~ItemStack()
- {
- if (item) item->release();
- }
- void ItemStack::setMaxSize(int size)
- {
- maxSize = size;
- }
- ItemStack* ItemStack::split(int size)
- {
- size = MIN(size, this->size);
- this->size -= size;
- return new ItemStack(
- dynamic_cast<Item*>(item->getThis()), size, item->getMaxStackSize());
- }
- Item* ItemStack::extractFromStack()
- {
- if (size == 0) return 0;
- --size;
- return item->zItemType()->cloneItem(item);
- }
- bool ItemStack::addToStack(Item* item)
- {
- if (size < maxSize && this->item->canBeStackedWith(item))
- {
- ++size;
- item->release();
- return true;
- }
- item->release();
- return false;
- }
- void ItemStack::addItemStack(ItemStack* zItemStack)
- {
- if (zItemStack->zItem()->canBeStackedWith(item))
- {
- int numToAdd = MIN(zItemStack->getSize(), maxSize - size);
- size += numToAdd;
- zItemStack->size -= numToAdd;
- }
- }
- bool ItemStack::isStackable(Item* zItem) const
- {
- return item->canBeStackedWith(zItem);
- }
- const Item* ItemStack::zItem() const
- {
- return item;
- }
- int ItemStack::getSize() const
- {
- return size;
- }
- int ItemStack::getMaxSize() const
- {
- return maxSize;
- }
|