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

This commit is contained in:
Zed A. Shaw 2024-11-07 01:00:17 -05:00
parent 0a268591c2
commit 011fee4872
4 changed files with 37 additions and 31 deletions

View file

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