Inventory system is mostly working and I can pick up everything and use it.

This commit is contained in:
Zed A. Shaw 2025-01-04 13:32:26 -05:00
parent aaa6d9f9f3
commit 14b3ea7676
8 changed files with 56 additions and 47 deletions

View file

@ -2,31 +2,31 @@
namespace components {
void Inventory::add(InventoryItem item) {
std::string id = item.data["id"];
if(items.contains(id)) {
auto &slot = items[id];
slot.count += item.count;
} else {
items[id] = item;
void Inventory::add(InventoryItem new_item) {
for(auto &slot : items) {
if(new_item.data["id"] == slot.data["id"]) {
slot.count += new_item.count;
return;
}
}
items.push_back(new_item);
}
InventoryItem& Inventory::get(std::string id) {
dbc::check(items.contains(id), fmt::format("item id {} is not in inventory", id));
return items[id];
InventoryItem& Inventory::get(size_t at) {
dbc::check(at < items.size(), fmt::format("inventory index {} too big", at));
return items[at];
}
bool Inventory::decrease(std::string id, int count) {
dbc::check(items.contains(id), fmt::format("item id {} is not in inventory", id));
auto &slot = items[id];
bool Inventory::decrease(size_t at, int count) {
dbc::check(at < items.size(), fmt::format("inventory index {} too big", at));
auto &slot = items[at];
slot.count -= count;
return slot.count > 0;
}
void Inventory::remove_all(std::string id) {
dbc::check(items.contains(id), fmt::format("item id {} is not in inventory", id));
items.erase(id);
void Inventory::erase_item(size_t at) {
dbc::check(at < items.size(), fmt::format("inventory index {} too big", at));
items.erase(items.begin() + at);
}
}