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 months ago
parent 0a268591c2
commit 011fee4872
  1. 2
      events.hpp
  2. 16
      gui.cpp
  3. 6
      map.hpp
  4. 44
      systems.cpp

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

@ -97,12 +97,13 @@ void GUI::create_renderer() {
}
void GUI::handle_world_events() {
using eGUI = Events::GUI;
auto player = $world.get_the<Player>();
while($world.has_event<Events::GUI>()) {
auto [evt, entity] = $world.recv<Events::GUI>();
while($world.has_event<eGUI>()) {
auto [evt, entity] = $world.recv<eGUI>();
switch(evt) {
case Events::GUI::HIT: {
case eGUI::HIT: {
auto combat = $world.get<Combat>(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<Loot>(entity);
$log.log(format("You found {} gold.", loot.amount));
}
break;
default:
$log.log(format("INVALID EVENT! {},{}", evt, entity));
}

@ -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);

@ -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<Position, Motion>([&](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<Combat>(entity)) {
auto& enemy_combat = world.get<Combat>(entity);
int player_dmg = player_combat.attack(enemy_combat);
if(player_dmg > 0) {
world.send<Events::GUI>(Events::GUI::HIT, entity);
world.send<eGUI>(eGUI::HIT, entity);
} else {
world.send<Events::GUI>(Events::GUI::MISS, entity);
world.send<eGUI>(eGUI::MISS, entity);
}
if(enemy_combat.hp > 0) {
int enemy_dmg = enemy_combat.attack(player_combat);
if(enemy_dmg > 0) {
world.send<Events::GUI>(Events::GUI::HIT, player.entity);
world.send<eGUI>(eGUI::HIT, player.entity);
} else {
world.send<Events::GUI>(Events::GUI::MISS, player.entity);
world.send<eGUI>(eGUI::MISS, player.entity);
}
} else {
world.send<Events::GUI>(Events::GUI::DEAD, entity);
world.send<eGUI>(eGUI::DEAD, entity);
}
} else if(world.has<Loot>(entity)) {
println("YOU FOUND LOOT");
world.send<eGUI>(eGUI::LOOT, entity);
} else {
println("UNKNOWN COLLISION TYPE {}", entity);
}

Loading…
Cancel
Save