42 lines
		
	
	
	
		
			1,007 B
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			42 lines
		
	
	
	
		
			1,007 B
		
	
	
	
		
			C++
		
	
	
	
	
	
| #include "inventory.hpp"
 | |
| 
 | |
| 
 | |
| namespace components {
 | |
|   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(size_t at) {
 | |
|     dbc::check(at < items.size(), fmt::format("inventory index {} too big", at));
 | |
|     return items[at];
 | |
|   }
 | |
| 
 | |
|   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::erase_item(size_t at) {
 | |
|     dbc::check(at < items.size(), fmt::format("inventory index {} too big", at));
 | |
|     items.erase(items.begin() + at);
 | |
|   }
 | |
| 
 | |
|   int Inventory::item_index(std::string id) {
 | |
|     for(size_t i = 0; i < items.size(); i++) {
 | |
|       if(items[i].data["id"] == id) {
 | |
|         return i;
 | |
|       }
 | |
|     }
 | |
| 
 | |
|     return -1;
 | |
|   }
 | |
| }
 | 
