From 011fee4872f04d6a8724036f29733b916e708819 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Thu, 7 Nov 2024 01:00:17 -0500 Subject: [PATCH] A little bit nicer but ultimately the way the system talks to the GUI needs a redesign to be more 'coarse grain' --- events.hpp | 2 +- gui.cpp | 16 +++++++++++----- map.hpp | 6 ++++++ systems.cpp | 44 +++++++++++++++++++------------------------- 4 files changed, 37 insertions(+), 31 deletions(-) diff --git a/events.hpp b/events.hpp index 185225a..2079474 100644 --- a/events.hpp +++ b/events.hpp @@ -2,6 +2,6 @@ namespace Events { enum GUI { - START, HIT, MISS, DEAD + START, HIT, MISS, DEAD, LOOT }; } diff --git a/gui.cpp b/gui.cpp index 057a4a6..2d1c91a 100644 --- a/gui.cpp +++ b/gui.cpp @@ -97,12 +97,13 @@ void GUI::create_renderer() { } void GUI::handle_world_events() { + using eGUI = Events::GUI; 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 Events::GUI::HIT: { + case eGUI::HIT: { auto combat = $world.get(entity); if(entity == player.entity) { @@ -113,16 +114,21 @@ void GUI::handle_world_events() { $sounds.play("hit"); } } break; - case Events::GUI::MISS: + case eGUI::MISS: if(entity == player.entity) { $log.log("You MISSED the enemy."); } else { $log.log("Enemy MISSED YOU."); } break; - case Events::GUI::DEAD: + case eGUI::DEAD: $log.log("--- ENEMY DEAD!"); break; + case eGUI::LOOT: { + auto loot = $world.get(entity); + $log.log(format("You found {} gold.", loot.amount)); + } + break; default: $log.log(format("INVALID EVENT! {},{}", evt, entity)); } diff --git a/map.hpp b/map.hpp index b1ce269..f36b930 100644 --- a/map.hpp +++ b/map.hpp @@ -73,6 +73,12 @@ public: void add_door(Room &room); bool inmap(size_t x, size_t y); bool iswall(size_t x, size_t y); + + bool can_move(Point move_to) { + return inmap(move_to.x, move_to.y) && + !iswall(move_to.x, move_to.y); + } + bool neighbors(Point &out, bool up); void generate(); void place_rooms(Room &root); diff --git a/systems.cpp b/systems.cpp index a18b5ae..7239872 100644 --- a/systems.cpp +++ b/systems.cpp @@ -47,26 +47,21 @@ void System::init_positions(DinkyECS::World &world) { }); } -inline bool move_entity(spatial_map &collider, Map &game_map, Position &position, Motion &motion, DinkyECS::Entity ent) { +inline void move_entity(spatial_map &collider, Map &game_map, Position &position, Motion &motion, DinkyECS::Entity ent) { Point move_to = { position.location.x + motion.dx, position.location.y + motion.dy }; motion = {0,0}; // clear it after getting it - if(game_map.inmap(move_to.x, move_to.y) && - !game_map.iswall(move_to.x, move_to.y)) - { - if(collider.occupied(move_to)) { - return true; - } else { - collider.move(position.location, move_to, ent); - position.location = move_to; - return false; - } - } else { - return false; - } + // it's a wall, skip + if(!game_map.can_move(move_to)) return; + // there's collision skip + if(collider.occupied(move_to)) return; + + // all good, do the move + collider.move(position.location, move_to, ent); + position.location = move_to; } void System::motion(DinkyECS::World &world, Map &game_map) { @@ -75,11 +70,7 @@ void System::motion(DinkyECS::World &world, Map &game_map) { world.query([&](const auto &ent, auto &position, auto &motion) { // don't process entities that don't move if(motion.dx != 0 || motion.dy != 0) { - // if there's a collision - if(move_entity(collider, game_map, position, motion, ent)) { - // send a collision event? - println("hit it!"); - } + move_entity(collider, game_map, position, motion, ent); } }); } @@ -109,30 +100,33 @@ void System::combat(DinkyECS::World &world, Player &player) { auto [found, nearby] = collider.neighbors(player_position.location); if(found) { + // save some keystrokes + using eGUI = Events::GUI; + for(auto entity : nearby) { if(world.has(entity)) { auto& enemy_combat = world.get(entity); int player_dmg = player_combat.attack(enemy_combat); if(player_dmg > 0) { - world.send(Events::GUI::HIT, entity); + world.send(eGUI::HIT, entity); } else { - world.send(Events::GUI::MISS, entity); + world.send(eGUI::MISS, entity); } if(enemy_combat.hp > 0) { int enemy_dmg = enemy_combat.attack(player_combat); if(enemy_dmg > 0) { - world.send(Events::GUI::HIT, player.entity); + world.send(eGUI::HIT, player.entity); } else { - world.send(Events::GUI::MISS, player.entity); + world.send(eGUI::MISS, player.entity); } } else { - world.send(Events::GUI::DEAD, entity); + world.send(eGUI::DEAD, entity); } } else if(world.has(entity)) { - println("YOU FOUND LOOT"); + world.send(eGUI::LOOT, entity); } else { println("UNKNOWN COLLISION TYPE {}", entity); }