parent
d7353a02df
commit
135d9a128b
@ -0,0 +1,32 @@ |
||||
#include "inventory.hpp" |
||||
|
||||
|
||||
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; |
||||
} |
||||
} |
||||
|
||||
InventoryItem& Inventory::get(std::string id) { |
||||
dbc::check(items.contains(id), fmt::format("item id {} is not in inventory", id)); |
||||
return items[id]; |
||||
} |
||||
|
||||
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]; |
||||
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); |
||||
} |
||||
} |
@ -0,0 +1,31 @@ |
||||
#pragma once |
||||
#include "lights.hpp" |
||||
#include <nlohmann/json.hpp> |
||||
#include <fmt/core.h> |
||||
|
||||
|
||||
namespace components { |
||||
using namespace nlohmann; |
||||
using lighting::LightSource; |
||||
|
||||
struct InventoryItem { |
||||
int count; |
||||
json data; |
||||
}; |
||||
|
||||
struct Inventory { |
||||
int gold; |
||||
LightSource light; |
||||
std::unordered_map<std::string, InventoryItem> items; |
||||
|
||||
size_t count() { return items.size(); } |
||||
|
||||
void add(InventoryItem item); |
||||
|
||||
bool decrease(std::string id, int count); |
||||
|
||||
InventoryItem& get(std::string id); |
||||
|
||||
void remove_all(std::string id); |
||||
}; |
||||
} |
@ -0,0 +1,61 @@ |
||||
#include <catch2/catch_test_macros.hpp> |
||||
#include <fmt/core.h> |
||||
#include <string> |
||||
#include "rand.hpp" |
||||
#include <nlohmann/json.hpp> |
||||
#include <fstream> |
||||
#include "components.hpp" |
||||
#include "dinkyecs.hpp" |
||||
#include "save.hpp" |
||||
#include "systems.hpp" |
||||
|
||||
using namespace nlohmann; |
||||
using namespace fmt; |
||||
using std::string; |
||||
using namespace components; |
||||
|
||||
|
||||
DinkyECS::Entity add_items(DinkyECS::World &world, GameConfig &config) { |
||||
auto sword = world.entity(); |
||||
world.set<InventoryItem>(sword, {1, config.items["SWORD_RUSTY"]}); |
||||
world.set<Tile>(sword, {config.items["SWORD_RUSTY"]["display"]}); |
||||
|
||||
return sword; |
||||
} |
||||
|
||||
TEST_CASE("basic inventory test", "[inventory]") { |
||||
DinkyECS::World world; |
||||
save::load_configs(world); |
||||
auto& config = world.get_the<GameConfig>(); |
||||
auto sword = add_items(world, config); |
||||
|
||||
auto player = world.entity(); |
||||
world.set<Inventory>(player, {}); |
||||
|
||||
auto &inventory = world.get<Inventory>(player); |
||||
|
||||
System::pickup(world, player, sword); |
||||
REQUIRE(inventory.count() == 1); |
||||
// get the item and confirm there is 1
|
||||
auto &item1 = inventory.get("SWORD_RUSTY"); |
||||
REQUIRE(item1.count == 1); |
||||
|
||||
System::pickup(world, player, sword); |
||||
System::pickup(world, player, sword); |
||||
System::pickup(world, player, sword); |
||||
REQUIRE(inventory.count() == 1); |
||||
REQUIRE(item1.count == 4); |
||||
|
||||
inventory.decrease("SWORD_RUSTY", 1); |
||||
REQUIRE(item1.count == 3); |
||||
|
||||
inventory.decrease("SWORD_RUSTY", 2); |
||||
REQUIRE(item1.count == 1); |
||||
|
||||
bool active = inventory.decrease("SWORD_RUSTY", 1); |
||||
REQUIRE(item1.count == 0); |
||||
REQUIRE(active == false); |
||||
|
||||
inventory.remove_all("SWORD_RUSTY"); |
||||
REQUIRE(inventory.count() == 0); |
||||
} |
Loading…
Reference in new issue