Have a plan for the new inventory and looting system, now have to implement it. Temporarily you can't pick anything up, but it will go away.
parent
b8d2d1870d
commit
ab391aaa97
@ -1,76 +0,0 @@ |
||||
#include "inventory.hpp" |
||||
#include <fmt/core.h> |
||||
|
||||
|
||||
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); |
||||
} |
||||
|
||||
bool Inventory::has_item(size_t at) { |
||||
return at < items.size(); |
||||
} |
||||
|
||||
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; |
||||
} |
||||
|
||||
std::pair<bool, std::string> Inventory::use(GameLevel &level, size_t at) { |
||||
auto& player_combat = level.world->get<components::Combat>(level.player); |
||||
auto& item = get(at); |
||||
|
||||
if(item.count == 0) return {false, item.data["name"]}; |
||||
|
||||
dbc::log("INVENTORY IS HARDCODED YOU FUCKING MORON!!!!!"); |
||||
|
||||
if(item.data["id"] == "SWORD_RUSTY") { |
||||
auto weapon = components::get<components::Weapon>(item.data); |
||||
player_combat.damage = weapon.damage; |
||||
} else if(item.data["id"] == "POTION_HEALING_SMALL") { |
||||
auto cure = components::get<components::Curative>(item.data); |
||||
player_combat.hp = std::min(player_combat.hp + cure.hp, player_combat.max_hp); |
||||
} else if(item.data["id"] == "TORCH_BAD") { |
||||
auto new_light = components::get<components::LightSource>(item.data); |
||||
level.world->set<components::LightSource>(level.player, new_light); |
||||
light = new_light; |
||||
} else { |
||||
return {false, fmt::format("UNKNOWN ITEM: {}", (std::string)item.data["id"])}; |
||||
} |
||||
|
||||
decrease(at, 1); |
||||
return {true, item.data["name"]}; |
||||
} |
||||
|
||||
|
||||
|
||||
} |
@ -1,35 +0,0 @@ |
||||
#pragma once |
||||
#include "components.hpp" |
||||
#include <nlohmann/json.hpp> |
||||
#include "levelmanager.hpp" |
||||
|
||||
namespace components { |
||||
using namespace nlohmann; |
||||
|
||||
struct InventoryItem { |
||||
int count; |
||||
json data; |
||||
}; |
||||
|
||||
struct Inventory { |
||||
int gold=0; |
||||
LightSource light{0, 0}; |
||||
std::vector<InventoryItem> items{}; |
||||
|
||||
size_t count() { return items.size(); } |
||||
|
||||
void add(InventoryItem item); |
||||
|
||||
bool decrease(size_t at, int count); |
||||
|
||||
bool has_item(size_t at); |
||||
|
||||
InventoryItem& get(size_t at); |
||||
|
||||
int item_index(std::string id); |
||||
|
||||
void erase_item(size_t at); |
||||
|
||||
std::pair<bool, std::string> use(GameLevel &level, size_t at); |
||||
}; |
||||
} |
@ -1,75 +0,0 @@ |
||||
#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 "inventory.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(components::ComponentMap component_map, DinkyECS::World &world, GameConfig &config) { |
||||
auto sword = world.entity(); |
||||
json& item_data = config.items["SWORD_RUSTY"]; |
||||
world.set<InventoryItem>(sword, {item_data["inventory_count"], item_data}); |
||||
components::configure_entity(component_map, world, sword, item_data); |
||||
return sword; |
||||
} |
||||
|
||||
TEST_CASE("basic inventory test", "[inventory]") { |
||||
// BUG: rewrite this
|
||||
/*
|
||||
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(0); |
||||
REQUIRE(item1.count == 1); |
||||
|
||||
int item_at = inventory.item_index("SWORD_RUSTY"); |
||||
REQUIRE(item_at == 0); |
||||
|
||||
REQUIRE(inventory.item_index("SADFASFSADF") == -1); |
||||
|
||||
System::pickup(world, player, sword); |
||||
REQUIRE(item1.count == 2); |
||||
|
||||
System::pickup(world, player, sword); |
||||
REQUIRE(item1.count == 3); |
||||
|
||||
System::pickup(world, player, sword); |
||||
REQUIRE(inventory.count() == 1); |
||||
|
||||
REQUIRE(item1.count == 4); |
||||
|
||||
inventory.decrease(0, 1); |
||||
REQUIRE(item1.count == 3); |
||||
|
||||
inventory.decrease(0, 2); |
||||
REQUIRE(item1.count == 1); |
||||
|
||||
bool active = inventory.decrease(0, 1); |
||||
REQUIRE(item1.count == 0); |
||||
REQUIRE(active == false); |
||||
|
||||
inventory.erase_item(0); |
||||
REQUIRE(inventory.count() == 0); |
||||
*/ |
||||
} |
Loading…
Reference in new issue