31static bool slotUsed(
const Item *item)
49 return mItems[index].get();
55 if (item && item->
getId() == itemId)
68 if (index < 0 || index >=
getSize())
70 Log::warn(
"Invalid inventory index: %d", index);
78 auto item = std::make_unique<Item>(
id, quantity);
80 mItems[index] = std::move(item);
87 mItems[index]->setQuantity(quantity);
115 return std::any_of(
mItems.begin(),
117 [
id = item->
getId()](
auto &i) {
118 return i && i->getId() == id;
124 for (
int i = 0; i <
getSize(); i++)
125 if (!slotUsed(
mItems[i].get()))
133 for (
int i =
getSize() - 1; i >= 0; i--)
134 if (slotUsed(
mItems[i].get()))
153 inventoryListener->slotsChanged(
this);
void clear()
Reset all item slots.
void setItem(int index, int id, int quantity)
Sets the item at the given position.
void removeInventoryListener(InventoryListener *listener)
int mUsed
The number of slots in use.
void addItem(int id, int quantity)
Adds a new item in a free slot.
void distributeSlotsChangedEvent()
int getLastUsedSlot() const
Returns the index of the last occupied slot or 0 if none occupied.
std::vector< std::unique_ptr< Item > > mItems
The holder of items.
std::list< InventoryListener * > mInventoryListeners
Item * findItem(int itemId) const
Searches for the specified item by it's id.
Item * getItem(int index) const
Returns the item at the specified index.
void addInventoryListener(InventoryListener *listener)
Inventory(Type type, int size=-1)
Constructor.
void removeItemAt(int index)
Remove the item at the specified index from the inventory.
int getFreeSlot() const
Returns id of next free slot or -1 if all occupied.
int getSize() const
Returns the size that this instance is configured for.
bool contains(Item *item) const
Checks if the given item is in the inventory.
Represents one or more instances of a certain item type.
int getQuantity() const
Returns the number of items.
int getId() const
Returns the item id.
void setInvIndex(int index)
Sets the inventory index of this item.
void warn(const char *log_text,...) LOG_PRINTF_ATTR
InventoryHandler * getInventoryHandler()