From 18a5f6bfa99da418087ea02d645f97e848109be7 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Mon, 20 Jan 2025 15:12:31 -0500 Subject: [PATCH] Setup for the LevelManager to take charge of all level operations. --- gui.cpp | 7 +++++++ gui.hpp | 1 + levelmanager.cpp | 45 +++++++++++++++++++++++++++++++++++++++--- levelmanager.hpp | 23 ++++++++++++++++++++- map.hpp | 3 --- status.txt | 3 ++- tests/levelmanager.cpp | 28 +++++++++++++++++++++++--- 7 files changed, 99 insertions(+), 11 deletions(-) diff --git a/gui.cpp b/gui.cpp index 705d4d5..75b38da 100644 --- a/gui.cpp +++ b/gui.cpp @@ -287,6 +287,9 @@ void GUI::handle_world_events() { } break; case eGUI::STAIRS_DOWN: { auto& device = std::any_cast(data); + + next_level(); + $status_ui.log(format("Up stairs has test {}.", (bool)device.config["test"])); toggle_modal(&$next_level_ui, $next_level); @@ -495,3 +498,7 @@ int GUI::main(bool run_once) { return 0; } + +void GUI::next_level() { + +} diff --git a/gui.hpp b/gui.hpp index a4bd27a..4ffc04e 100644 --- a/gui.hpp +++ b/gui.hpp @@ -158,6 +158,7 @@ public: void save_world(); void shake(); void shutdown(); + void next_level(); int main(bool run_once=false); void pause_screen(); diff --git a/levelmanager.cpp b/levelmanager.cpp index 6afbf8c..910b42c 100644 --- a/levelmanager.cpp +++ b/levelmanager.cpp @@ -1,10 +1,49 @@ #include "levelmanager.hpp" +#include "worldbuilder.hpp" +#include "constants.hpp" +#include "save.hpp" + +using lighting::LightRender; +using std::shared_ptr, std::make_shared; LevelManager::LevelManager() { - // does nothing yet } +size_t LevelManager::create_level() { + GameLevel level; + level.index = $levels.size(); + + level.world = make_shared(); + save::load_configs(*level.world); + + level.map = make_shared(GAME_MAP_X, GAME_MAP_Y); + WorldBuilder builder(*level.map); + builder.generate(*level.world); + + level.lights = make_shared(level.map->width(), level.map->height()); + + $levels.push_back(level); + + dbc::check(level.index == $levels.size() - 1, "Level index is not the same as $levels.size() - 1, off by one error"); + return level.index; +} + +GameLevel &LevelManager::next() { + dbc::check($current_level < $levels.size(), "attempt to get next level when at end"); + $current_level++; + return $levels.at($current_level); +} + +GameLevel &LevelManager::previous() { + dbc::check($current_level > 0, "attempt to go to previous level when at 0"); + $current_level--; + return $levels.at($current_level); +} + +GameLevel &LevelManager::current() { + return $levels.at($current_level); +} -void LevelManager::create_level() { - // does nothing yet +GameLevel &LevelManager::get(size_t index) { + return $levels.at(index); } diff --git a/levelmanager.hpp b/levelmanager.hpp index 5c3db49..16af2e2 100644 --- a/levelmanager.hpp +++ b/levelmanager.hpp @@ -1,9 +1,30 @@ #pragma once +#include "dinkyecs.hpp" +#include "lights.hpp" +#include "map.hpp" +#include +#include + + +struct GameLevel { + size_t index = 0; + std::shared_ptr map = nullptr; + std::shared_ptr world = nullptr; + std::shared_ptr lights = nullptr; +}; class LevelManager { + public: + std::vector $levels; + size_t $current_level = 0; LevelManager(); - void create_level(); + size_t create_level(); + GameLevel &next(); + GameLevel &previous(); + GameLevel ¤t(); + size_t current_index(); + GameLevel &get(size_t index); }; diff --git a/map.hpp b/map.hpp index 9a8cdbc..0c69f0a 100644 --- a/map.hpp +++ b/map.hpp @@ -39,9 +39,6 @@ public: Map(Matrix &walls, Pathing &paths); - // disable copying - Map(Map &map) = delete; - Matrix& paths() { return $paths.paths(); } TileMap& tiles() { return $tiles; } Matrix& input_map() { return $paths.input(); } diff --git a/status.txt b/status.txt index 7e6fbfa..951f01a 100644 --- a/status.txt +++ b/status.txt @@ -1,5 +1,6 @@ -TODAY'S GOAL: +TODO: +* What's the result of out of memory error with shared/unique ptr? * Config is all over the place. Can I get rid of constant.hpp? Or most of it? Also renderer.cpp:RenderConfig is weird too. Too much indirection all around. * GUI needs to become a statemachine now. Too many panels open at too many times. * Panels should be able to take a width/height and center theirself for me diff --git a/tests/levelmanager.cpp b/tests/levelmanager.cpp index 74e736d..5305f32 100644 --- a/tests/levelmanager.cpp +++ b/tests/levelmanager.cpp @@ -10,9 +10,31 @@ #include "levelmanager.hpp" using namespace fmt; -using namespace components; using std::string; -TEST_CASE("", "[levelmanager]") { - // empty test to get setup +TEST_CASE("basic level manager test", "[levelmanager]") { + LevelManager lm; + + size_t level1 = lm.create_level(); + 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); }