12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- #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;
- ItemStack *result = new ItemStack(
- dynamic_cast<Item*>(item->getThis()), size, item->getMaxStackSize());
- if (size == 0)
- {
- item->release();
- item = 0;
- }
- return result;
- }
- Item* ItemStack::extractFromStack()
- {
- if (size == 0) return 0;
- --size;
- Item* result = item->zItemType()->cloneItem(item);
- if (size == 0)
- {
- item->release();
- item = 0;
- }
- return result;
- }
- bool ItemStack::addToStack(Item* item)
- {
- if (size < maxSize && (size == 0 || this->item->canBeStackedWith(item)))
- {
- if (!this->item)
- {
- this->item = item->zItemType()->cloneItem(item);
- }
- ++size;
- item->release();
- return true;
- }
- item->release();
- return false;
- }
- void ItemStack::addItemStack(ItemStack* zItemStack)
- {
- if (item && zItemStack->zItem()->canBeStackedWith(item))
- {
- int numToAdd = MIN(zItemStack->getSize(), maxSize - size);
- size += numToAdd;
- zItemStack->size -= numToAdd;
- }
- }
- bool ItemStack::isStackable(Item* zItem) const
- {
- return size == 0 || item->canBeStackedWith(zItem);
- }
- const Item* ItemStack::zItem() const
- {
- return item;
- }
- int ItemStack::getSize() const
- {
- return size;
- }
- int ItemStack::getMaxSize() const
- {
- return maxSize;
- }
|