From 1bb89996100f5eb578f7aca7cf16fbb0b0fb7c9a Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Wed, 16 Oct 2024 23:15:56 -0400 Subject: [PATCH] A bit more cleanup, but still looking for more organization. --- components.hpp | 42 ++++++++++++++++++++++-------------------- gui.cpp | 28 ++++++++++++++++++++-------- gui.hpp | 5 ++--- scripts/reset_build.sh | 3 ++- systems.cpp | 2 ++ systems.hpp | 4 ++-- 6 files changed, 50 insertions(+), 34 deletions(-) diff --git a/components.hpp b/components.hpp index 725555f..f5c6a3d 100644 --- a/components.hpp +++ b/components.hpp @@ -1,28 +1,30 @@ #pragma once #include "dinkyecs.hpp" -struct Player { - DinkyECS::Entity entity; -}; +namespace Components { + struct Player { + DinkyECS::Entity entity; + }; -struct Position { - Point location; -}; + struct Position { + Point location; + }; -struct Motion { - int dx; - int dy; -}; + struct Motion { + int dx; + int dy; + }; -struct Combat { - int hp; - int damage; -}; + struct Combat { + int hp; + int damage; + }; -struct Treasure { - int amount; -}; + struct Treasure { + int amount; + }; -struct Tile { - std::string chr = "!"; -}; + struct Tile { + std::string chr = "!"; + }; +} diff --git a/gui.cpp b/gui.cpp index 7f3049b..3868b8e 100644 --- a/gui.cpp +++ b/gui.cpp @@ -26,6 +26,7 @@ using std::string; using namespace fmt; using namespace std::chrono_literals; using namespace ftxui; +using namespace Components; std::array VALUES{ sf::Color{1, 4, 2}, // black @@ -116,8 +117,9 @@ void GUI::create_renderer() { }); } -void GUI::handle_events() { +bool GUI::handle_events() { sf::Event event; + bool event_happened = false; auto player = $world.get(); auto& player_motion = $world.component(player.entity); @@ -127,21 +129,28 @@ void GUI::handle_events() { } else if(event.type == sf::Event::KeyPressed) { if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { player_motion.dx = -1; + event_happened = true; } else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { player_motion.dx = 1; + event_happened = true; } else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) { player_motion.dy = -1; + event_happened = true; } else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) { player_motion.dy = 1; + event_happened = true; } - - // COMPOSE system? You create a bunch of callbacks and then combine them into - // a single run over the data? - System::enemy_pathing($world, $game_map, player); - System::motion($world, $game_map); - System::combat($world, player); } } + + return event_happened; +} + +void GUI::run_systems() { + auto player = $world.get(); + System::enemy_pathing($world, $game_map, player); + System::motion($world, $game_map); + System::combat($world, player); } void GUI::burn() { @@ -225,7 +234,10 @@ int GUI::main() { while($window.isOpen()) { render_scene(); - handle_events(); + + if(handle_events()) { + run_systems(); + } std::this_thread::sleep_for(10ms); } diff --git a/gui.hpp b/gui.hpp index 3cf8fab..07b6a6b 100644 --- a/gui.hpp +++ b/gui.hpp @@ -48,8 +48,6 @@ class GUI { sf::Font $font; sf::Text $ui_text; sf::Text $map_text; - bool $shake_it = false; - bool $burn_baby_burn = false; std::wstring_convert> $converter; sf::RenderWindow $window; Screen $screen; @@ -65,11 +63,12 @@ public: sf::Color color(int val); void create_renderer(); void render_scene(); - void handle_events(); + bool handle_events(); void draw_screen(bool clear=true, float map_off_x=0.0f, float map_off_y=0.0f); void shake(); void burn(); void configure_world(); + void run_systems(); int main(); }; diff --git a/scripts/reset_build.sh b/scripts/reset_build.sh index 11e8d63..9d56a7f 100755 --- a/scripts/reset_build.sh +++ b/scripts/reset_build.sh @@ -7,4 +7,5 @@ mkdir subprojects mv packagecache ./subprojects/ mkdir builddir cp wraps/*.wrap subprojects/ -meson setup --default-library=static --prefer-static builddir +# on OSX you can't do this with static +meson setup builddir diff --git a/systems.cpp b/systems.cpp index 2a9b30e..46f01c0 100644 --- a/systems.cpp +++ b/systems.cpp @@ -1,5 +1,7 @@ #include "systems.hpp" +using namespace Components; + void System::enemy_pathing(DinkyECS::World &world, Map &game_map, Player &player) { // move enemies system world.system([&](const auto &ent, auto &position, auto &motion) { diff --git a/systems.hpp b/systems.hpp index 9a7a19a..97e69d0 100644 --- a/systems.hpp +++ b/systems.hpp @@ -6,7 +6,7 @@ namespace System { void motion(DinkyECS::World &world, Map &game_map); - void combat(DinkyECS::World &world, Player &player); + void combat(DinkyECS::World &world, Components::Player &player); void draw_entities(DinkyECS::World &world, ftxui::Canvas &canvas); - void enemy_pathing(DinkyECS::World &world, Map &game_map, Player &player); + void enemy_pathing(DinkyECS::World &world, Map &game_map, Components::Player &player); }