Mana
Loading...
Searching...
No Matches
inventory.h
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2004-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#pragma once
23
24#include <list>
25#include <memory>
26#include <vector>
27
28class Inventory;
29class Item;
30
32{
33public:
34 virtual ~InventoryListener() = default;
35
36 virtual void slotsChanged(Inventory* inventory) = 0;
37
38protected:
40};
41
43{
44 public:
45 static const int NO_SLOT_INDEX = -1;
53
54
60 Inventory(Type type, int size = -1);
61
63
67 int getSize() const { return mItems.size(); }
68
72 Item *getItem(int index) const;
73
80 Item *findItem(int itemId) const;
81
85 void addItem(int id, int quantity);
86
90 void setItem(int index, int id, int quantity);
91
95 void removeItemAt(int index);
96
100 bool contains(Item *item) const;
101
105 int getFreeSlot() const;
106
110 void clear();
111
116 { return mUsed; }
117
121 int getLastUsedSlot() const;
122
125
126 int getType() const
127 { return mType; }
128
129 bool isMainInventory() const
130 { return mType == INVENTORY; }
131
132 protected:
133 std::list<InventoryListener *> mInventoryListeners;
134
136
138 std::vector<std::unique_ptr<Item>> mItems;
139 int mUsed = 0;
140};
virtual ~InventoryListener()=default
virtual void slotsChanged(Inventory *inventory)=0
void clear()
Reset all item slots.
Definition inventory.cpp:96
Type mType
Definition inventory.h:137
int getType() const
Definition inventory.h:126
void setItem(int index, int id, int quantity)
Sets the item at the given position.
Definition inventory.cpp:66
bool isMainInventory() const
Definition inventory.h:129
void removeInventoryListener(InventoryListener *listener)
int mUsed
The number of slots in use.
Definition inventory.h:139
void addItem(int id, int quantity)
Adds a new item in a free slot.
Definition inventory.cpp:61
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.
Definition inventory.h:138
std::list< InventoryListener * > mInventoryListeners
Definition inventory.h:133
Item * findItem(int itemId) const
Searches for the specified item by it's id.
Definition inventory.cpp:52
Item * getItem(int index) const
Returns the item at the specified index.
Definition inventory.cpp:44
static const int NO_SLOT_INDEX
Slot has no index.
Definition inventory.h:45
void addInventoryListener(InventoryListener *listener)
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.
Definition inventory.h:67
int getNumberOfSlotsUsed() const
Get the number of slots filled with an item.
Definition inventory.h:115
bool contains(Item *item) const
Checks if the given item is in the inventory.
Represents one or more instances of a certain item type.
Definition item.h:35