12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- #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;
- }
|