Basic inventory system working and can pick up items but needs to be reflected in the UI next.
This commit is contained in:
parent
d7353a02df
commit
135d9a128b
14 changed files with 212 additions and 48 deletions
61
tests/inventory.cpp
Normal file
61
tests/inventory.cpp
Normal file
|
@ -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);
|
||||
}
|
|
@ -252,6 +252,7 @@ TEST_CASE("prototype circle algorithm", "[matrix:circle]") {
|
|||
size_t height = Random::uniform<size_t>(10, 15);
|
||||
int pos_mod = Random::uniform<int>(-3,3);
|
||||
Map map(width,height);
|
||||
|
||||
// create a target for the paths
|
||||
Point start{.x=map.width() / 2 + pos_mod, .y=map.height()/2 + pos_mod};
|
||||
|
||||
|
@ -275,3 +276,31 @@ TEST_CASE("prototype circle algorithm", "[matrix:circle]") {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("viewport iterator", "[matrix:viewport]") {
|
||||
size_t width = Random::uniform<size_t>(20, 22);
|
||||
size_t height = Random::uniform<size_t>(21, 25);
|
||||
Map map(width,height);
|
||||
WorldBuilder builder(map);
|
||||
builder.generate();
|
||||
|
||||
size_t view_width = width/2;
|
||||
size_t view_height = height/2;
|
||||
Point player = map.place_entity(1);
|
||||
Point start = map.center_camera(player, view_width, view_height);
|
||||
|
||||
size_t end_x = std::min(view_width, map.width() - start.x);
|
||||
size_t end_y = std::min(view_height, map.height() - start.y);
|
||||
|
||||
matrix::viewport it{map.walls(), start, int(view_width), int(view_height)};
|
||||
|
||||
for(size_t y = 0; y < end_y; ++y) {
|
||||
for(size_t x = 0; x < end_x && it.next(); ++x) {
|
||||
|
||||
println("view x/y={},{}; w/h={},{}; start={},{}",
|
||||
it.x, it.y, it.width, it.height, it.start.x, it.start.y);
|
||||
println("orig x/y={},{}; w/h={},{}; start={},{}\n",
|
||||
x+start.x, y+start.y, view_width, view_height, start.x, start.y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue