From 009b1e63a79a686a334517036f9d4362e3117b95 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Wed, 30 Oct 2024 02:13:31 -0400 Subject: [PATCH] More refactoring to get the GUI dumber. --- components.hpp | 11 ---------- events.hpp | 8 ++++--- gui.cpp | 59 ++++++++------------------------------------------ gui.hpp | 20 ++++++++++++----- main.cpp | 53 ++++++++++++++++++++++++++++++++++++++++++++- systems.cpp | 10 ++++----- 6 files changed, 86 insertions(+), 75 deletions(-) diff --git a/components.hpp b/components.hpp index 047941c..7e46b51 100644 --- a/components.hpp +++ b/components.hpp @@ -25,15 +25,4 @@ namespace Components { struct Tile { std::string chr = "!"; }; - - struct ActionLog { - std::deque messages; - - void log(std::string msg) { - messages.push_front(msg); - if(messages.size() > 20) { - messages.pop_back(); - } - } - }; } diff --git a/events.hpp b/events.hpp index b3bcecd..185225a 100644 --- a/events.hpp +++ b/events.hpp @@ -1,5 +1,7 @@ #pragma once -enum GUIEvent { - START, HIT, MISS, DEAD -}; +namespace Events { + enum GUI { + START, HIT, MISS, DEAD + }; +} diff --git a/gui.cpp b/gui.cpp index 46fda91..b8534a1 100644 --- a/gui.cpp +++ b/gui.cpp @@ -51,8 +51,7 @@ sf::Color GUI::color(Value val) { return VALUES[size_t(val)]; } -GUI::GUI() : - $game_map(GAME_MAP_X, GAME_MAP_Y), +GUI::GUI(DinkyECS::World &world, Map& game_map) : $window(sf::VideoMode(VIDEO_X,VIDEO_Y), "Roguish"), $screen(SCREEN_X, SCREEN_Y), $map_screen(0,0), @@ -60,7 +59,9 @@ GUI::GUI() : $map_font_size(BASE_MAP_FONT_SIZE), $line_spacing(0), $sounds("./assets"), - $log({{"Welcome to the game!"}}) + $log({{"Welcome to the game!"}}), + $world(world), + $game_map(game_map) { // this needs a config file soon $font.loadFromFile("./assets/text.otf"); @@ -72,8 +73,6 @@ GUI::GUI() : $ui_text.setPosition(0,0); $ui_text.setCharacterSize(UI_FONT_SIZE); $ui_text.setFillColor(color(Value::LIGHT_LIGHT)); - - $game_map.generate(); } void GUI::create_renderer() { @@ -112,30 +111,28 @@ void GUI::create_renderer() { void GUI::handle_world_events() { auto player = $world.get_the(); - while($world.has_event()) { - auto [evt, entity] = $world.recv(); + while($world.has_event()) { + auto [evt, entity] = $world.recv(); switch(evt) { - case GUIEvent::HIT: { + case Events::GUI::HIT: { auto combat = $world.get(entity); if(entity == player.entity) { $log.log(format("Enemy HIT YOU, you have {} HP!", combat.hp)); $sounds.play("hit"); - shake(); } else { $log.log(format("You HIT enemy, they have {} HP!", combat.hp)); $sounds.play("hit"); - shake(); } } break; - case GUIEvent::MISS: + case Events::GUI::MISS: if(entity == player.entity) { $log.log("You MISSED the enemy."); } else { $log.log("Enemy MISSED YOU."); } break; - case GUIEvent::DEAD: + case Events::GUI::DEAD: $log.log("--- ENEMY DEAD!"); break; default: @@ -305,43 +302,6 @@ void GUI::shake() { } } -void GUI::configure_world() { - // this sets up the gui event system - $world.set_the(GUIEvent::START); - - dbc::check($game_map.room_count() > 1, "not enough rooms in map."); - // configure a player as a fact of the world - Player player{$world.entity()}; - $world.set_the(player); - - spatial_map collider; - $world.set_the(collider); - - $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, {PLAYER_TILE}); - - auto enemy = $world.entity(); - $world.set(enemy, {$game_map.place_entity(1)}); - $world.set(enemy, {0,0}); - $world.set(enemy, {20, 10}); - $world.set(enemy, {ENEMY_TILE}); - - auto enemy2 = $world.entity(); - $world.set(enemy2, {$game_map.place_entity(2)}); - $world.set(enemy2, {0,0}); - $world.set(enemy2, {20, 10}); - $world.set(enemy2, {"*"}); - - auto gold = $world.entity(); - $world.set(gold, {$game_map.place_entity($game_map.room_count() - 1)}); - $world.set(gold, {100}); - $world.set(gold, {"$"}); - - System::init_positions($world); -} - void GUI::render_scene() { $screen.Clear(); $map_screen.Clear(); @@ -352,7 +312,6 @@ void GUI::render_scene() { } int GUI::main() { - configure_world(); create_renderer(); run_systems(); diff --git a/gui.hpp b/gui.hpp index 3510f53..f233d70 100644 --- a/gui.hpp +++ b/gui.hpp @@ -36,8 +36,18 @@ enum class Value { LIGHT_LIGHT, WHITE, TRANSPARENT }; +struct ActionLog { + std::deque messages; + + void log(std::string msg) { + messages.push_front(msg); + if(messages.size() > 20) { + messages.pop_back(); + } + } +}; + class GUI { - Map $game_map; string $status_text = "NOT DEAD"; Component $document; Component $map_view; @@ -48,7 +58,6 @@ class GUI { sf::RenderWindow $window; Screen $screen; Screen $map_screen; - DinkyECS::World $world; sf::Texture $font_texture; std::unordered_map $sprites; Point $view_port; @@ -56,10 +65,12 @@ class GUI { sf::Glyph $base_glyph; float $line_spacing; SoundManager $sounds; - Components::ActionLog $log; + ActionLog $log; + DinkyECS::World& $world; + Map& $game_map; public: - GUI(); + GUI(DinkyECS::World& world, Map& game_map); // disable copying GUI(GUI &gui) = delete; @@ -71,7 +82,6 @@ public: void handle_world_events(); void draw_screen(bool clear=true, float map_off_x=0.0f, float map_off_y=0.0f); void shake(); - void configure_world(); void run_systems(); void resize_map(int new_size); sf::Sprite &get_text_sprite(wchar_t tile); diff --git a/main.cpp b/main.cpp index 871694e..9d836e0 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,57 @@ #include "gui.hpp" +#include "dinkyecs.hpp" +#include "systems.hpp" +#include "events.hpp" +#include "components.hpp" +#include "dbc.hpp" +#include "collider.hpp" + +/* + * This needs to be turned into a real world generator + * system. + */ +void configure_world(DinkyECS::World &world, Map &game_map) { + // this sets up the gui event system + world.set_the(Events::GUI::START); + + // configure a player as a fact of the world + Player player{world.entity()}; + world.set_the(player); + + spatial_map collider; + world.set_the(collider); + + 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, {PLAYER_TILE}); + + auto enemy = world.entity(); + world.set(enemy, {game_map.place_entity(1)}); + world.set(enemy, {0,0}); + world.set(enemy, {20, 10}); + world.set(enemy, {ENEMY_TILE}); + + auto enemy2 = world.entity(); + world.set(enemy2, {game_map.place_entity(2)}); + world.set(enemy2, {0,0}); + world.set(enemy2, {20, 10}); + world.set(enemy2, {"*"}); + + auto gold = world.entity(); + world.set(gold, {game_map.place_entity(game_map.room_count() - 1)}); + world.set(gold, {100}); + world.set(gold, {"$"}); +} + int main() { - GUI gui; + DinkyECS::World world; + Map game_map(GAME_MAP_X, GAME_MAP_Y); + game_map.generate(); + + configure_world(world, game_map); + System::init_positions(world); + GUI gui(world, game_map); return gui.main(); } diff --git a/systems.cpp b/systems.cpp index b50efdc..01efa1a 100644 --- a/systems.cpp +++ b/systems.cpp @@ -98,21 +98,21 @@ void System::combat(DinkyECS::World &world, Player &player) { int player_dmg = player_combat.attack(enemy_combat); if(player_dmg > 0) { - world.send(GUIEvent::HIT, entity); + world.send(Events::GUI::HIT, entity); } else { - world.send(GUIEvent::MISS, entity); + world.send(Events::GUI::MISS, entity); } if(enemy_combat.hp > 0) { int enemy_dmg = enemy_combat.attack(player_combat); if(enemy_dmg > 0) { - world.send(GUIEvent::HIT, player.entity); + world.send(Events::GUI::HIT, player.entity); } else { - world.send(GUIEvent::MISS, player.entity); + world.send(Events::GUI::MISS, player.entity); } } else { - world.send(GUIEvent::DEAD, entity); + world.send(Events::GUI::DEAD, entity); } } }