A little bit nicer but ultimately the way the system talks to the GUI needs a redesign to be more 'coarse grain'

main
Zed A. Shaw 2 weeks ago
parent 0a268591c2
commit 011fee4872
  1. 2
      events.hpp
  2. 16
      gui.cpp
  3. 6
      map.hpp
  4. 40
      systems.cpp

@ -2,6 +2,6 @@
namespace Events { namespace Events {
enum GUI { enum GUI {
START, HIT, MISS, DEAD START, HIT, MISS, DEAD, LOOT
}; };
} }

@ -97,12 +97,13 @@ void GUI::create_renderer() {
} }
void GUI::handle_world_events() { void GUI::handle_world_events() {
using eGUI = Events::GUI;
auto player = $world.get_the<Player>(); auto player = $world.get_the<Player>();
while($world.has_event<Events::GUI>()) { while($world.has_event<eGUI>()) {
auto [evt, entity] = $world.recv<Events::GUI>(); auto [evt, entity] = $world.recv<eGUI>();
switch(evt) { switch(evt) {
case Events::GUI::HIT: { case eGUI::HIT: {
auto combat = $world.get<Combat>(entity); auto combat = $world.get<Combat>(entity);
if(entity == player.entity) { if(entity == player.entity) {
@ -113,16 +114,21 @@ void GUI::handle_world_events() {
$sounds.play("hit"); $sounds.play("hit");
} }
} break; } break;
case Events::GUI::MISS: case eGUI::MISS:
if(entity == player.entity) { if(entity == player.entity) {
$log.log("You MISSED the enemy."); $log.log("You MISSED the enemy.");
} else { } else {
$log.log("Enemy MISSED YOU."); $log.log("Enemy MISSED YOU.");
} }
break; break;
case Events::GUI::DEAD: case eGUI::DEAD:
$log.log("--- ENEMY DEAD!"); $log.log("--- ENEMY DEAD!");
break; break;
case eGUI::LOOT: {
auto loot = $world.get<Loot>(entity);
$log.log(format("You found {} gold.", loot.amount));
}
break;
default: default:
$log.log(format("INVALID EVENT! {},{}", evt, entity)); $log.log(format("INVALID EVENT! {},{}", evt, entity));
} }

@ -73,6 +73,12 @@ public:
void add_door(Room &room); void add_door(Room &room);
bool inmap(size_t x, size_t y); bool inmap(size_t x, size_t y);
bool iswall(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); bool neighbors(Point &out, bool up);
void generate(); void generate();
void place_rooms(Room &root); void place_rooms(Room &root);

@ -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 = { Point move_to = {
position.location.x + motion.dx, position.location.x + motion.dx,
position.location.y + motion.dy position.location.y + motion.dy
}; };
motion = {0,0}; // clear it after getting it motion = {0,0}; // clear it after getting it
if(game_map.inmap(move_to.x, move_to.y) && // it's a wall, skip
!game_map.iswall(move_to.x, move_to.y)) if(!game_map.can_move(move_to)) return;
{ // there's collision skip
if(collider.occupied(move_to)) { if(collider.occupied(move_to)) return;
return true;
} else { // all good, do the move
collider.move(position.location, move_to, ent); collider.move(position.location, move_to, ent);
position.location = move_to; position.location = move_to;
return false;
}
} else {
return false;
}
} }
void System::motion(DinkyECS::World &world, Map &game_map) { void System::motion(DinkyECS::World &world, Map &game_map) {
@ -75,11 +70,7 @@ void System::motion(DinkyECS::World &world, Map &game_map) {
world.query<Position, Motion>([&](const auto &ent, auto &position, auto &motion) { world.query<Position, Motion>([&](const auto &ent, auto &position, auto &motion) {
// don't process entities that don't move // don't process entities that don't move
if(motion.dx != 0 || motion.dy != 0) { if(motion.dx != 0 || motion.dy != 0) {
// if there's a collision move_entity(collider, game_map, position, motion, ent);
if(move_entity(collider, game_map, position, motion, ent)) {
// send a collision event?
println("hit it!");
}
} }
}); });
} }
@ -109,30 +100,33 @@ void System::combat(DinkyECS::World &world, Player &player) {
auto [found, nearby] = collider.neighbors(player_position.location); auto [found, nearby] = collider.neighbors(player_position.location);
if(found) { if(found) {
// save some keystrokes
using eGUI = Events::GUI;
for(auto entity : nearby) { for(auto entity : nearby) {
if(world.has<Combat>(entity)) { if(world.has<Combat>(entity)) {
auto& enemy_combat = world.get<Combat>(entity); auto& enemy_combat = world.get<Combat>(entity);
int player_dmg = player_combat.attack(enemy_combat); int player_dmg = player_combat.attack(enemy_combat);
if(player_dmg > 0) { if(player_dmg > 0) {
world.send<Events::GUI>(Events::GUI::HIT, entity); world.send<eGUI>(eGUI::HIT, entity);
} else { } else {
world.send<Events::GUI>(Events::GUI::MISS, entity); world.send<eGUI>(eGUI::MISS, entity);
} }
if(enemy_combat.hp > 0) { if(enemy_combat.hp > 0) {
int enemy_dmg = enemy_combat.attack(player_combat); int enemy_dmg = enemy_combat.attack(player_combat);
if(enemy_dmg > 0) { if(enemy_dmg > 0) {
world.send<Events::GUI>(Events::GUI::HIT, player.entity); world.send<eGUI>(eGUI::HIT, player.entity);
} else { } else {
world.send<Events::GUI>(Events::GUI::MISS, player.entity); world.send<eGUI>(eGUI::MISS, player.entity);
} }
} else { } else {
world.send<Events::GUI>(Events::GUI::DEAD, entity); world.send<eGUI>(eGUI::DEAD, entity);
} }
} else if(world.has<Loot>(entity)) { } else if(world.has<Loot>(entity)) {
println("YOU FOUND LOOT"); world.send<eGUI>(eGUI::LOOT, entity);
} else { } else {
println("UNKNOWN COLLISION TYPE {}", entity); println("UNKNOWN COLLISION TYPE {}", entity);
} }

Loading…
Cancel
Save