From a83ee77eeaf2e0fcff14195cccb3b1dadb3dc8c9 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Wed, 20 Aug 2025 01:10:42 -0400 Subject: [PATCH] levelmanager.* is now gone, but the code is just moved over to game_level. Now to clean up the api and give it a new name. --- game_level.cpp | 130 ++++++++++++++++++++++++++++++++++++++++++-- game_level.hpp | 18 +++++- gui/debug_ui.cpp | 9 +-- gui/debug_ui.hpp | 5 +- gui/fsm.cpp | 1 - gui/fsm.hpp | 2 +- gui/status_ui.cpp | 1 - levelmanager.cpp | 100 ---------------------------------- levelmanager.hpp | 45 --------------- meson.build | 1 - raycaster.cpp | 2 +- raycaster.hpp | 4 +- systems.cpp | 1 - tests/lighting.cpp | 1 - tests/map.cpp | 1 - tests/matrix.cpp | 3 +- tools/arena_fsm.hpp | 1 - 17 files changed, 147 insertions(+), 178 deletions(-) delete mode 100644 levelmanager.cpp delete mode 100644 levelmanager.hpp diff --git a/game_level.cpp b/game_level.cpp index aab54c3..7e4d6ca 100644 --- a/game_level.cpp +++ b/game_level.cpp @@ -1,6 +1,129 @@ #include "game_level.hpp" -#include "levelmanager.hpp" #include "components.hpp" +#include "worldbuilder.hpp" +#include "constants.hpp" +#include "save.hpp" +#include "systems.hpp" +#include "components.hpp" +#include "rituals.hpp" + +using lighting::LightRender; +using std::shared_ptr, std::make_shared; +using namespace components; + +struct LevelScaling { + int map_width=20; + int map_height=20; +}; + +class LevelManager { + public: + std::vector $levels; + size_t $current_level = 0; + + LevelManager(); + + shared_ptr create_bossfight(shared_ptr prev_world); + size_t create_level(shared_ptr prev_world = nullptr); + GameLevel &next(); + GameLevel &previous(); + GameLevel ¤t(); + size_t current_index() { return $current_level; } + GameLevel &get(size_t index); + LevelScaling scale_level(); + + DinkyECS::Entity spawn_enemy(const std::string& named); +}; + +LevelManager::LevelManager() { + create_level(); +} + +LevelScaling LevelManager::scale_level() { + return { + INITIAL_MAP_W + int($current_level * 2), + INITIAL_MAP_H + int($current_level * 2) + }; +} + + +inline shared_ptr clone_load_world(shared_ptr prev_world) +{ + auto world = make_shared(); + + if(prev_world != nullptr) { + prev_world->clone_into(*world); + } else { + save::load_configs(*world); + } + + return world; +} + +shared_ptr LevelManager::create_bossfight(shared_ptr prev_world) { + dbc::check(prev_world != nullptr, "Starter world for boss fights can't be null."); + auto world = clone_load_world(prev_world); + auto& config = prev_world->get_the(); + + // BUG: the jank is too strong here + auto boss_names = config.bosses.keys(); + auto& level_name = boss_names[$current_level % boss_names.size()]; + auto& boss_data = config.bosses[level_name]; + + auto boss_id = world->entity(); + components::configure_entity(*world, boss_id, boss_data["components"]); + + return make_shared(world, boss_id); +} + +DinkyECS::Entity LevelManager::spawn_enemy(const std::string& named) { + (void)named; + dbc::log("THIS FUNCTION NEEDS A REWRITE"); + return 0; +} + +size_t LevelManager::create_level(shared_ptr prev_world) { + auto world = clone_load_world(prev_world); + + auto scaling = scale_level(); + + auto map = make_shared(scaling.map_width, scaling.map_height); + auto collision = std::make_shared(); + + WorldBuilder builder(*map, *collision); + builder.generate(*world); + + size_t index = $levels.size(); + + auto player = world->get_the(); + + $levels.emplace_back(index, player.entity, map, world, + make_shared(map->tiles()), collision); + + dbc::check(index == $levels.size() - 1, "Level index is not the same as $levels.size() - 1, off by one error"); + return 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); +} + +GameLevel &LevelManager::get(size_t index) { + return $levels.at(index); +} + namespace Game { using std::shared_ptr, std::string, std::make_shared; @@ -15,11 +138,6 @@ namespace Game { } } - 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; diff --git a/game_level.hpp b/game_level.hpp index d7743ba..944c838 100644 --- a/game_level.hpp +++ b/game_level.hpp @@ -2,19 +2,31 @@ #include "dinkyecs.hpp" #include "gui/boss_fight_ui.hpp" +#include "dinkyecs.hpp" +#include "lights.hpp" +#include "map.hpp" +#include +#include "spatialmap.hpp" -struct GameLevel; -struct LevelManager; namespace components { struct Position; } +struct GameLevel { + size_t index; + DinkyECS::Entity player; + std::shared_ptr map = nullptr; + std::shared_ptr world = nullptr; + std::shared_ptr lights = nullptr; + std::shared_ptr collision = nullptr; +}; + + namespace Game { std::shared_ptr create_bossfight(); GameLevel& create_level(); void init(); - LevelManager& get_the_manager(); GameLevel &next(); GameLevel &previous(); GameLevel ¤t(); diff --git a/gui/debug_ui.cpp b/gui/debug_ui.cpp index 6991c53..6380d8f 100644 --- a/gui/debug_ui.cpp +++ b/gui/debug_ui.cpp @@ -9,11 +9,6 @@ namespace gui { using namespace guecs; - DebugUI::DebugUI(LevelManager& level_mgr) : - $level_mgr(level_mgr) - { - } - void DebugUI::init(lel::Cell cell) { $gui.position(cell.x, cell.y, cell.w, cell.h); $gui.layout( @@ -51,7 +46,7 @@ namespace gui { void DebugUI::render(sf::RenderWindow& window) { if(active) { - auto& level = $level_mgr.current(); + auto& level = Game::current(); auto player = level.world->get_the(); auto player_combat = level.world->get(player.entity); auto map = level.map; @@ -81,7 +76,7 @@ namespace gui { active = !active; if(active) { - auto& level = $level_mgr.current(); + auto& level = Game::current(); // it's on now, enable things auto player = level.world->get_the(); auto& player_combat = level.world->get(player.entity); diff --git a/gui/debug_ui.hpp b/gui/debug_ui.hpp index e7465eb..7760acb 100644 --- a/gui/debug_ui.hpp +++ b/gui/debug_ui.hpp @@ -1,5 +1,5 @@ #pragma once -#include "levelmanager.hpp" +#include "game_level.hpp" #include #include #include @@ -10,11 +10,8 @@ namespace gui { public: Stats $stats; guecs::UI $gui; - LevelManager& $level_mgr; bool active = false; - DebugUI(LevelManager& level_mgr); - void init(lel::Cell cell); void render(sf::RenderWindow& window); bool mouse(float x, float y, guecs::Modifiers mods); diff --git a/gui/fsm.cpp b/gui/fsm.cpp index f64ad3c..f6dd6fa 100644 --- a/gui/fsm.cpp +++ b/gui/fsm.cpp @@ -19,7 +19,6 @@ namespace gui { FSM::FSM() : $window(sf::VideoMode({SCREEN_WIDTH, SCREEN_HEIGHT}), "Zed's Raycaster Thing"), - $debug_ui(Game::get_the_manager()), $main_ui($window), $font{FONT_FILE_NAME}, $dnd_loot($status_ui, $loot_ui, $window, $router) diff --git a/gui/fsm.hpp b/gui/fsm.hpp index 0d082f7..93efaa6 100644 --- a/gui/fsm.hpp +++ b/gui/fsm.hpp @@ -36,7 +36,7 @@ namespace gui { int $temp_attack_id = 0; DebugUI $debug_ui; MainUI $main_ui; - shared_ptr $boss_fight_ui = nullptr; + std::shared_ptr $boss_fight_ui = nullptr; CombatUI $combat_ui; StatusUI $status_ui; MapViewUI $map_ui; diff --git a/gui/status_ui.cpp b/gui/status_ui.cpp index 55590f4..54b6cb5 100644 --- a/gui/status_ui.cpp +++ b/gui/status_ui.cpp @@ -7,7 +7,6 @@ #include "systems.hpp" #include "inventory.hpp" #include "game_level.hpp" -#include "levelmanager.hpp" namespace gui { using namespace guecs; diff --git a/levelmanager.cpp b/levelmanager.cpp deleted file mode 100644 index a5e7a4f..0000000 --- a/levelmanager.cpp +++ /dev/null @@ -1,100 +0,0 @@ -#include "levelmanager.hpp" -#include "worldbuilder.hpp" -#include "constants.hpp" -#include "save.hpp" -#include "systems.hpp" -#include "components.hpp" -#include "rituals.hpp" - -using lighting::LightRender; -using std::shared_ptr, std::make_shared; -using namespace components; - -LevelManager::LevelManager() { - create_level(); -} - -LevelScaling LevelManager::scale_level() { - return { - INITIAL_MAP_W + int($current_level * 2), - INITIAL_MAP_H + int($current_level * 2) - }; -} - - -inline shared_ptr clone_load_world(shared_ptr prev_world) -{ - auto world = make_shared(); - - if(prev_world != nullptr) { - prev_world->clone_into(*world); - } else { - save::load_configs(*world); - } - - return world; -} - -shared_ptr LevelManager::create_bossfight(shared_ptr prev_world) { - dbc::check(prev_world != nullptr, "Starter world for boss fights can't be null."); - auto world = clone_load_world(prev_world); - auto& config = prev_world->get_the(); - - // BUG: the jank is too strong here - auto boss_names = config.bosses.keys(); - auto& level_name = boss_names[$current_level % boss_names.size()]; - auto& boss_data = config.bosses[level_name]; - - auto boss_id = world->entity(); - components::configure_entity(*world, boss_id, boss_data["components"]); - - return make_shared(world, boss_id); -} - -DinkyECS::Entity LevelManager::spawn_enemy(const std::string& named) { - (void)named; - dbc::log("THIS FUNCTION NEEDS A REWRITE"); - return 0; -} - -size_t LevelManager::create_level(shared_ptr prev_world) { - auto world = clone_load_world(prev_world); - - auto scaling = scale_level(); - - auto map = make_shared(scaling.map_width, scaling.map_height); - auto collision = std::make_shared(); - - WorldBuilder builder(*map, *collision); - builder.generate(*world); - - size_t index = $levels.size(); - - auto player = world->get_the(); - - $levels.emplace_back(index, player.entity, map, world, - make_shared(map->tiles()), collision); - - dbc::check(index == $levels.size() - 1, "Level index is not the same as $levels.size() - 1, off by one error"); - return 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); -} - -GameLevel &LevelManager::get(size_t index) { - return $levels.at(index); -} diff --git a/levelmanager.hpp b/levelmanager.hpp deleted file mode 100644 index e5befa3..0000000 --- a/levelmanager.hpp +++ /dev/null @@ -1,45 +0,0 @@ -#pragma once - -#include "dinkyecs.hpp" -#include "lights.hpp" -#include "map.hpp" -#include -#include -#include "spatialmap.hpp" -#include "components.hpp" -#include "gui/boss_fight_ui.hpp" - -using std::shared_ptr; - -struct GameLevel { - size_t index; - DinkyECS::Entity player; - shared_ptr map = nullptr; - shared_ptr world = nullptr; - shared_ptr lights = nullptr; - shared_ptr collision = nullptr; -}; - -struct LevelScaling { - int map_width=20; - int map_height=20; -}; - -class LevelManager { - public: - std::vector $levels; - size_t $current_level = 0; - - LevelManager(); - - shared_ptr create_bossfight(shared_ptr prev_world); - size_t create_level(shared_ptr prev_world = nullptr); - GameLevel &next(); - GameLevel &previous(); - GameLevel ¤t(); - size_t current_index() { return $current_level; } - GameLevel &get(size_t index); - LevelScaling scale_level(); - - DinkyECS::Entity spawn_enemy(const std::string& named); -}; diff --git a/meson.build b/meson.build index c610312..e7aaceb 100644 --- a/meson.build +++ b/meson.build @@ -107,7 +107,6 @@ sources = [ 'gui/ritual_ui.cpp', 'gui/status_ui.cpp', 'inventory.cpp', - 'levelmanager.cpp', 'lights.cpp', 'map.cpp', 'matrix.cpp', diff --git a/raycaster.cpp b/raycaster.cpp index c652763..582ca14 100644 --- a/raycaster.cpp +++ b/raycaster.cpp @@ -13,7 +13,7 @@ #include "shaders.hpp" using namespace fmt; -using std::make_unique; +using std::make_unique, std::shared_ptr; union ColorConv { struct { diff --git a/raycaster.hpp b/raycaster.hpp index 4ec2d1e..6a73f86 100644 --- a/raycaster.hpp +++ b/raycaster.hpp @@ -3,7 +3,7 @@ #include #include #include "spatialmap.hpp" -#include "levelmanager.hpp" +#include "game_level.hpp" #include "textures.hpp" #include "camera.hpp" @@ -62,7 +62,7 @@ struct Raycaster { void update_level(GameLevel level); void update_sprite(DinkyECS::Entity ent, components::Sprite& sprite); void init_shaders(); - void apply_sprite_effect(shared_ptr effect, float width, float height); + void apply_sprite_effect(std::shared_ptr effect, float width, float height); // camera things? void position_camera(float player_x, float player_y); diff --git a/systems.cpp b/systems.cpp index 86b3780..59fb222 100644 --- a/systems.cpp +++ b/systems.cpp @@ -17,7 +17,6 @@ #include "shaders.hpp" #include "inventory.hpp" #include "game_level.hpp" -#include "levelmanager.hpp" using std::string; using namespace fmt; diff --git a/tests/lighting.cpp b/tests/lighting.cpp index 57e0f81..a30e3d2 100644 --- a/tests/lighting.cpp +++ b/tests/lighting.cpp @@ -3,7 +3,6 @@ #include #include #include "map.hpp" -#include "levelmanager.hpp" #include "game_level.hpp" #include "lights.hpp" #include "point.hpp" diff --git a/tests/map.cpp b/tests/map.cpp index febf9e0..898f1f6 100644 --- a/tests/map.cpp +++ b/tests/map.cpp @@ -3,7 +3,6 @@ #include #include #include "map.hpp" -#include "levelmanager.hpp" #include "game_level.hpp" #include "systems.hpp." diff --git a/tests/matrix.cpp b/tests/matrix.cpp index a166687..4b00f2f 100644 --- a/tests/matrix.cpp +++ b/tests/matrix.cpp @@ -9,11 +9,10 @@ #include #include "map.hpp" #include -#include "levelmanager.hpp" using namespace nlohmann; using namespace fmt; -using std::string; +using std::string, std::shared_ptr; using matrix::Matrix; std::shared_ptr make_map() { diff --git a/tools/arena_fsm.hpp b/tools/arena_fsm.hpp index 9cce3e1..1f6acce 100644 --- a/tools/arena_fsm.hpp +++ b/tools/arena_fsm.hpp @@ -1,7 +1,6 @@ #pragma once #include "constants.hpp" #include "stats.hpp" -#include "levelmanager.hpp" #include "fsm.hpp" #include "main_ui.hpp" #include "combat_ui.hpp"