From f3f875ee804f7e1cf58271e9e578023067c54ffe Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Fri, 10 Jan 2025 11:12:14 -0500 Subject: [PATCH] Initial changes to clean up the code. --- dinkyecs.hpp | 4 ++-- matrix.cpp | 2 +- matrix.hpp | 12 ++++++++---- systems.cpp | 3 +++ tests/gui.cpp | 14 +------------- tests/inventory.cpp | 6 +++--- tests/matrix.cpp | 6 +----- worldbuilder.cpp | 3 ++- 8 files changed, 21 insertions(+), 29 deletions(-) diff --git a/dinkyecs.hpp b/dinkyecs.hpp index f0cf6d1..4cf59be 100644 --- a/dinkyecs.hpp +++ b/dinkyecs.hpp @@ -52,7 +52,7 @@ namespace DinkyECS { template void set_the(Comp val) { - $facts[std::type_index(typeid(Comp))] = val; + $facts.insert_or_assign(std::type_index(typeid(Comp)), val); } template @@ -68,7 +68,7 @@ namespace DinkyECS { template void set(Entity ent, Comp val) { EntityMap &map = entity_map_for(); - map[ent] = val; + map.insert_or_assign(ent, val); } template diff --git a/matrix.cpp b/matrix.cpp index 0a6e0f5..08c1a14 100644 --- a/matrix.cpp +++ b/matrix.cpp @@ -1,9 +1,9 @@ #include "matrix.hpp" -#include "constants.hpp" #include "dbc.hpp" #include #include #include +#include "constants.hpp" using namespace fmt; using std::min, std::max; diff --git a/matrix.hpp b/matrix.hpp index b6ef16f..e07488e 100644 --- a/matrix.hpp +++ b/matrix.hpp @@ -168,6 +168,10 @@ namespace matrix { size_t bottom = 0; box_t(MAT &mat, size_t at_x, size_t at_y, size_t size) : + box_t(mat, at_x, at_y, size, size) { + } + + box_t(MAT &mat, size_t at_x, size_t at_y, size_t width, size_t height) : from_x(at_x), from_y(at_y) { size_t h = matrix::height(mat); @@ -175,15 +179,15 @@ namespace matrix { // keeps it from going below zero // need extra -1 to compensate for the first next() - left = max(from_x, size) - size; + left = max(from_x, width) - width; x = left - 1; // must be -1 for next() // keeps it from going above width - right = min(from_x + size + 1, w); + right = min(from_x + width + 1, w); // same for these two - top = max(from_y, size) - size; + top = max(from_y, height) - height; y = top - (left == 0); - bottom = min(from_y + size + 1, h); + bottom = min(from_y + height + 1, h); } bool next() { diff --git a/systems.cpp b/systems.cpp index a2a7d06..35f23fb 100644 --- a/systems.cpp +++ b/systems.cpp @@ -203,6 +203,9 @@ void System::draw_entities(DinkyECS::World &world, Map &game_map, const Matrix & } void System::pickup(DinkyECS::World &world, DinkyECS::Entity actor, DinkyECS::Entity item) { + dbc::pre("System::pickup actor doesn't have inventory", world.has(actor)); + dbc::pre("System::pickup item isn't configured with InventoryItem.", world.has(item)); + auto& inventory = world.get(actor); auto& invitem = world.get(item); diff --git a/tests/gui.cpp b/tests/gui.cpp index 1544163..823770e 100644 --- a/tests/gui.cpp +++ b/tests/gui.cpp @@ -18,19 +18,7 @@ TEST_CASE("load a basic gui run but don't loop", "[gui]") { save::load_configs(world); Map game_map(40, 40); WorldBuilder builder(game_map); - builder.generate_map(); - - auto &config = world.get_the(); - // configure a player as a fact of the world - Player player{world.entity()}; - world.set_the(player); - - world.set(player.entity, {game_map.place_entity(0)}); - world.set(player.entity, {0, 0}); - world.set(player.entity, {100, 10}); - world.set(player.entity, {config.enemies["PLAYER_TILE"]["display"]}); - world.set(player.entity, {5}); - world.set(player.entity, {6,1}); + builder.generate(world); SpatialMap collider; world.set_the(collider); diff --git a/tests/inventory.cpp b/tests/inventory.cpp index 76507bb..a054778 100644 --- a/tests/inventory.cpp +++ b/tests/inventory.cpp @@ -17,9 +17,9 @@ using namespace components; DinkyECS::Entity add_items(DinkyECS::World &world, GameConfig &config) { auto sword = world.entity(); - world.set(sword, {1, config.items["SWORD_RUSTY"]}); - world.set(sword, {config.items["SWORD_RUSTY"]["display"]}); - + json& item_data = config.items["SWORD_RUSTY"]; + world.set(sword, {item_data["inventory_count"], item_data}); + components::configure(world, sword, item_data); return sword; } diff --git a/tests/matrix.cpp b/tests/matrix.cpp index 56df254..55400b7 100644 --- a/tests/matrix.cpp +++ b/tests/matrix.cpp @@ -296,11 +296,7 @@ TEST_CASE("viewport iterator", "[matrix:viewport]") { 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); + // still working on this } } } diff --git a/worldbuilder.cpp b/worldbuilder.cpp index 297b981..7a3a20b 100644 --- a/worldbuilder.cpp +++ b/worldbuilder.cpp @@ -191,7 +191,8 @@ DinkyECS::Entity place_combatant(DinkyECS::World &world, Map &game_map, std::str auto &config = world.get_the(); auto enemy = world.entity(); auto enemy_data = config.enemies[name]; - world.set(enemy, {game_map.place_entity(in_room)}); + auto pos = game_map.place_entity(in_room); + world.set(enemy, {pos}); if(enemy_data.contains("components")) { components::configure(world, enemy, enemy_data);