From 62562faad32f65555193df7781360406c2381cf4 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Sat, 26 Oct 2024 19:47:40 -0400 Subject: [PATCH] Give the player first move advantage, but maybe this should be a setting on motion so that some enemies can beat the player. --- systems.cpp | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/systems.cpp b/systems.cpp index f5a45e6..b8e503e 100644 --- a/systems.cpp +++ b/systems.cpp @@ -33,25 +33,33 @@ void System::init_positions(DinkyECS::World &world) { }); } +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) && + !collider.occupied(move_to)) + { + collider.move(position.location, move_to, ent); + position.location = move_to; + } +} + void System::motion(DinkyECS::World &world, Map &game_map) { auto &collider = world.get(); + auto &player = world.get(); + auto &player_position = world.component(player.entity); + auto &player_motion = world.component(player.entity); + move_entity(collider, game_map, player_position, player_motion, player.entity); world.system([&](const auto &ent, auto &position, auto &motion) { // don't process entities that don't move if(motion.dx != 0 || motion.dy != 0) { - 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) && - !collider.occupied(move_to)) - { - collider.move(position.location, move_to, ent); - position.location = move_to; - } + move_entity(collider, game_map, position, motion, ent); } }); }