From 5aca2fb56a872026a4831e23cb1267912c7a566f Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Wed, 20 Aug 2025 00:48:20 -0400 Subject: [PATCH] Tests are now clean so next step is to officially nuke the level manager. --- game_level.cpp | 18 ++++++++++++++++-- gui/overlay_ui.cpp | 11 +++++------ meson.build | 1 - tests/levelmanager.cpp | 40 ---------------------------------------- tests/lighting.cpp | 7 ++++--- tests/map.cpp | 28 +++++++++++++++------------- tests/matrix.cpp | 13 +++++++------ tests/rituals.cpp | 1 - 8 files changed, 47 insertions(+), 72 deletions(-) delete mode 100644 tests/levelmanager.cpp diff --git a/game_level.cpp b/game_level.cpp index e934099..aab54c3 100644 --- a/game_level.cpp +++ b/game_level.cpp @@ -9,58 +9,72 @@ namespace Game { bool initialized = false; void init() { - LEVELS = make_shared(); - initialized = true; + if(!initialized) { + LEVELS = make_shared(); + initialized = true; + } } LevelManager& get_the_manager() { + dbc::check(initialized, "Forgot to call Game::init()"); return *LEVELS; } shared_ptr current_world() { + dbc::check(initialized, "Forgot to call Game::init()"); return current().world; } shared_ptr create_bossfight() { + dbc::check(initialized, "Forgot to call Game::init()"); return LEVELS->create_bossfight(current_world()); } GameLevel& create_level() { + dbc::check(initialized, "Forgot to call Game::init()"); LEVELS->create_level(current_world()); return next(); } GameLevel &next() { + dbc::check(initialized, "Forgot to call Game::init()"); return LEVELS->next(); } GameLevel &previous() { + dbc::check(initialized, "Forgot to call Game::init()"); return LEVELS->previous(); } GameLevel ¤t() { + dbc::check(initialized, "Forgot to call Game::init()"); return LEVELS->current(); } size_t current_index() { + dbc::check(initialized, "Forgot to call Game::init()"); return LEVELS->current_index(); } GameLevel &get(size_t index) { + dbc::check(initialized, "Forgot to call Game::init()"); return LEVELS->get(index); } DinkyECS::Entity spawn_enemy(const std::string& named) { + dbc::check(initialized, "Forgot to call Game::init()"); return LEVELS->spawn_enemy(named); } components::Position& player_position() { + dbc::check(initialized, "Forgot to call Game::init()"); auto world = current_world(); auto& player = world->get_the(); return world->get(player.entity); } DinkyECS::Entity the_player() { + dbc::check(initialized, "Forgot to call Game::init()"); return current().player; } } diff --git a/gui/overlay_ui.cpp b/gui/overlay_ui.cpp index 8859c1a..313eabe 100644 --- a/gui/overlay_ui.cpp +++ b/gui/overlay_ui.cpp @@ -18,23 +18,22 @@ namespace gui { $gui.init(); } - inline void make_clickable_area(std::shared_ptr world, guecs::UI &gui, const std::string &name) { + inline void make_clickable_area(guecs::UI &gui, const std::string &name) { auto area = gui.entity(name); gui.set(area, { [&](auto) { + auto world = Game::current_world(); world->send(Events::GUI::AIM_CLICK, area, {}); } }); } void OverlayUI::init() { - auto world = Game::current_world(); - // gui.init is in the constructor - make_clickable_area(world, $gui, "top"); - make_clickable_area(world, $gui, "middle"); - make_clickable_area(world, $gui, "bottom"); + make_clickable_area($gui, "top"); + make_clickable_area($gui, "middle"); + make_clickable_area($gui, "bottom"); } void OverlayUI::render(sf::RenderWindow& window) { diff --git a/meson.build b/meson.build index 7590106..c610312 100644 --- a/meson.build +++ b/meson.build @@ -142,7 +142,6 @@ executable('runtests', sources + [ 'tests/event_router.cpp', 'tests/fsm.cpp', 'tests/inventory.cpp', - 'tests/levelmanager.cpp', 'tests/lighting.cpp', 'tests/loot.cpp', 'tests/map.cpp', diff --git a/tests/levelmanager.cpp b/tests/levelmanager.cpp deleted file mode 100644 index 190027e..0000000 --- a/tests/levelmanager.cpp +++ /dev/null @@ -1,40 +0,0 @@ -#include -#include -#include "map.hpp" -#include "dinkyecs.hpp" -#include "worldbuilder.hpp" -#include "save.hpp" -#include "systems.hpp" -#include "spatialmap.hpp" -#include "levelmanager.hpp" - -using namespace fmt; -using std::string; - -TEST_CASE("basic level manager test", "[levelmanager]") { - LevelManager lm; - - // starts off with one already but I need to change that - size_t level1 = lm.current_index(); - size_t level2 = lm.create_level(); - - auto& test1_level = lm.get(level1); - auto& test2_level = lm.get(level2); - - REQUIRE(test1_level.map->width() > 0); - REQUIRE(test1_level.map->height() > 0); - REQUIRE(test1_level.index == 0); - - REQUIRE(test2_level.map->width() > 0); - REQUIRE(test2_level.map->height() > 0); - REQUIRE(test2_level.index == 1); - - auto& cur_level = lm.current(); - REQUIRE(cur_level.index == 0); - - auto& next_level = lm.next(); - REQUIRE(next_level.index == 1); - - auto& prev_level = lm.previous(); - REQUIRE(prev_level.index == 0); -} diff --git a/tests/lighting.cpp b/tests/lighting.cpp index 8789d26..57e0f81 100644 --- a/tests/lighting.cpp +++ b/tests/lighting.cpp @@ -4,15 +4,16 @@ #include #include "map.hpp" #include "levelmanager.hpp" +#include "game_level.hpp" #include "lights.hpp" #include "point.hpp" using namespace lighting; TEST_CASE("lighting a map works", "[lighting]") { - LevelManager levels; - GameLevel level = levels.current(); - auto &map = *level.map; + Game::init(); + auto& level = Game::current(); + auto& map = *level.map; Point light1, light2; diff --git a/tests/map.cpp b/tests/map.cpp index 21370b1..febf9e0 100644 --- a/tests/map.cpp +++ b/tests/map.cpp @@ -19,9 +19,10 @@ json load_test_data(const string &fname) { TEST_CASE("camera control", "[map]") { textures::init(); components::init(); - LevelManager levels; - GameLevel level = levels.current(); - auto &map = *level.map; + Game::init(); + + auto& level = Game::current(); + auto& map = *level.map; Point center = map.center_camera({10,10}, 5, 5); @@ -35,21 +36,22 @@ TEST_CASE("camera control", "[map]") { REQUIRE(translation.y == 2); } -TEST_CASE("map placement test", "[map]") { +TEST_CASE("map placement test", "[map-fail]") { textures::init(); components::init(); - for(int i = 0; i < 20; i++) { - LevelManager levels; - GameLevel level = levels.current(); - auto &map = *level.map; + Game::init(); + + + for(int i = 0; i < 5; i++) { + auto& level = Game::create_level(); - for(size_t rnum = 0; rnum < map.room_count(); rnum++) { + for(size_t rnum = 0; rnum < level.map->room_count(); rnum++) { Point pos; - REQUIRE(map.place_entity(rnum, pos)); + REQUIRE(level.map->place_entity(rnum, pos)); - REQUIRE(!map.iswall(pos.x, pos.y)); - REQUIRE(map.inmap(pos.x, pos.y)); + REQUIRE(!level.map->iswall(pos.x, pos.y)); + REQUIRE(level.map->inmap(pos.x, pos.y)); } } } @@ -87,7 +89,7 @@ TEST_CASE("map image test", "[map]") { textures::init(); Game::init(); - GameLevel level = Game::current(); + auto& level = Game::current(); Matrix map_tiles = matrix::make(7,7); EntityGrid entity_map; diff --git a/tests/matrix.cpp b/tests/matrix.cpp index cfefaf5..a166687 100644 --- a/tests/matrix.cpp +++ b/tests/matrix.cpp @@ -4,20 +4,21 @@ #include "config.hpp" #include "matrix.hpp" #include "rand.hpp" -#include "levelmanager.hpp" +#include "game_level.hpp" #include #include +#include "map.hpp" +#include +#include "levelmanager.hpp" using namespace nlohmann; using namespace fmt; using std::string; using matrix::Matrix; -shared_ptr make_map() { - // BUG? I mean, it's a shared_ptr so it should keep it around but.... - LevelManager levels; - GameLevel level = levels.current(); - return level.map; +std::shared_ptr make_map() { + Game::init(); + return Game::current().map; } TEST_CASE("basic matrix iterator", "[matrix:basic]") { diff --git a/tests/rituals.cpp b/tests/rituals.cpp index d1ac25b..57a5361 100644 --- a/tests/rituals.cpp +++ b/tests/rituals.cpp @@ -2,7 +2,6 @@ #include #include "rituals.hpp" #include "simplefsm.hpp" -#include "levelmanager.hpp" #include "ai_debug.hpp" TEST_CASE("ritual::Engine basic tests", "[rituals]") {