diff --git a/ECS/src/systems/basics.cpp b/ECS/src/systems/basics.cpp index 67799e9..3c60715 100644 --- a/ECS/src/systems/basics.cpp +++ b/ECS/src/systems/basics.cpp @@ -31,7 +31,16 @@ void logging_system(registry const &, void clock_system(registry &r, sparse_array &clocks, sparse_array const &enabled) { - for (auto &&[clock, _]: zipper(clocks, enabled)) - if (not clock.paused() and clock.elapsed_time() > clock.time()) - clock.send_signal(comp::clock_signals::ended, r, clock); -} + std::vector clocks_to_signal; + for (auto &&[i, clock, _]: indexed_zipper(clocks, enabled)) { + if (not clock.paused() and clock.elapsed_time() > clock.time()) { + clocks_to_signal.push_back(i); + } + } + for (size_t i : clocks_to_signal) { + auto &clock = clocks[i]; + if (clock.has_value()) { + clock->send_signal(comp::clock_signals::ended, r, *clock); + } + } +} \ No newline at end of file diff --git a/server/src/systems.cpp b/server/src/systems.cpp index cc664e6..4fd22a1 100644 --- a/server/src/systems.cpp +++ b/server/src/systems.cpp @@ -46,6 +46,7 @@ void kill_enemy_system(registry ®, sparse_array const &killables, sparse_array const &monsters, sparse_array const &enabled) { + std::vector entities_to_kill; for (auto &&[i, pos, health, monster, _]: indexed_zipper(positions, killables, monsters, enabled)) { if (pos.x < -100 || health.health <= 0) { if (health.health <= 0 && health.lastDamager != -1) { @@ -57,9 +58,12 @@ void kill_enemy_system(registry ®, } } monster.pushToSendQueue(i, SAFE, KILL_ENTITY, 0, nullptr); - reg.kill_entity(i); + entities_to_kill.push_back(i); } } + for (size_t i : entities_to_kill) { + reg.kill_entity(i); + } } void enemy_pathfind_system(registry const &, @@ -103,24 +107,32 @@ void kill_monster_shoot_system(registry &r, sparse_array const &positions, sparse_array const &shoots, sparse_array const &enabled) { + std::vector entities_to_kill; for (auto &&[i, pos, shoot, _]: indexed_zipper(positions, shoots, enabled)) { if (pos.x < -30) { shoot.pushToSendQueue(i, SAFE, KILL_ENTITY, 0, nullptr); - r.kill_entity(i); + entities_to_kill.push_back(i); } } + for (size_t i : entities_to_kill) { + r.kill_entity(i); + } } void kill_shoot_system(registry &r, sparse_array const &positions, sparse_array const &shoots, sparse_array const &enabled) { + std::vector entities_to_kill; for (auto &&[i, pos, shoot, _]: indexed_zipper(positions, shoots, enabled)) { if (pos.x > 1920) { shoot.pushToSendQueue(i, SAFE, KILL_ENTITY, 0, nullptr); - r.kill_entity(i); + entities_to_kill.push_back(i); } } + for (size_t i : entities_to_kill) { + r.kill_entity(i); + } } void kill_player_system(registry &r, @@ -128,12 +140,16 @@ void kill_player_system(registry &r, sparse_array const &killables, sparse_array const &players, sparse_array const &enabled) { + std::vector entities_to_kill; for (auto &&[i, pos, health, play, _]: indexed_zipper(positions, killables, players, enabled)) { if (health.health <= 0) { play.pushToSendQueue(i, SAFE, KILL_ENTITY, 0, nullptr); - r.kill_entity(i); + entities_to_kill.push_back(i); } } + for (size_t i : entities_to_kill) { + r.kill_entity(i); + } } void shoot_system(registry &r, @@ -141,26 +157,36 @@ void shoot_system(registry &r, sparse_array &clocks, sparse_array const &positions, sparse_array const &enabled) { + struct MissileToSpawn { + size_t player_idx; + comp::position pos; + sendFct const *pushToSendQueue; + }; + std::vector missiles_to_spawn; + for (auto &&[i, player, clock, pos, _]: indexed_zipper(players, clocks, positions, enabled)) { for (uint16_t key: player.recvKeys) { if (key == SHOOT) { if (not clock.paused() and clock.elapsed_time() > clock.time()) { - entity ent(r.spawn_entity()); - clock.restart(); - r.add_components(ent, - comp::velocity(3.0f, .0f), - comp::position(pos.x + 32, pos.y + 12), - shoot(player.pushToSendQueue, i, 10), - update_location(600), - comp::hittable({25.0f, 30.0f}), - comp::enabled()); - auto &missile_pos(r.get_components()[ent]); - player.pushToSendQueue(ent, SAFE, NEW_MISSILE, sizeof(*missile_pos), &*missile_pos); + missiles_to_spawn.push_back({i, comp::position(pos.x + 32, pos.y + 12), &player.pushToSendQueue}); } } } } + + for (auto &missile_data : missiles_to_spawn) { + entity ent(r.spawn_entity()); + r.add_components(ent, + comp::velocity(3.0f, .0f), + missile_data.pos, + shoot(*missile_data.pushToSendQueue, missile_data.player_idx, 10), + update_location(600), + comp::hittable({25.0f, 30.0f}), + comp::enabled()); + auto &missile_pos(r.get_components()[ent]); + (*missile_data.pushToSendQueue)(ent, SAFE, NEW_MISSILE, sizeof(*missile_pos), &*missile_pos); + } } void monster_shoot_collision_system(registry &r, @@ -172,16 +198,22 @@ void monster_shoot_collision_system(registry &r, sparse_array &update_clds, sparse_array &killables, sparse_array const &enabled) { + std::vector entities_to_kill; + for (auto &&[i, player_pos, health, player_hit, player, __]: indexed_zipper(positions, killables, hittables, players, enabled)) { for (auto &&[j, shoot_pos, shoot_hit, shoot, __]: indexed_zipper(positions, hittables, shoots, enabled)) { if (player_hit.hit(shoot_hit, player_pos.x - shoot_pos.x, player_pos.y - shoot_pos.y)) { player.pushToSendQueue(j, SAFE, KILL_ENTITY, 0, nullptr); health.health -= shoot.damage; health.lastDamager = int(shoot.shooter); - r.kill_entity(j); + entities_to_kill.push_back(j); } } } + + for (size_t entity_id : entities_to_kill) { + r.kill_entity(entity_id); + } for (auto &&[i, pos, vel, shoot, cld, __]: indexed_zipper(positions, velocities, shoots, update_clds, enabled)) { if (cld.last_send >= cld.ticks) { cld.loc = {pos.x, pos.y, vel.x, vel.y}; @@ -201,16 +233,22 @@ void player_shoot_collision_system(registry &r, sparse_array &update_clds, sparse_array &killables, sparse_array const &enabled) { + std::vector entities_to_kill; + for (auto &&[i, monster_pos, health, monster_hit, monster, __]: indexed_zipper(positions, killables, hittables, monsters, enabled)) { for (auto &&[j, shoot_pos, shoot_hit, shoot, __]: indexed_zipper(positions, hittables, shoots, enabled)) { if (monster_hit.hit(shoot_hit, monster_pos.x - shoot_pos.x, monster_pos.y - shoot_pos.y)) { shoot.pushToSendQueue(j, SAFE, KILL_ENTITY, 0, nullptr); health.health -= shoot.damage; health.lastDamager = int(shoot.shooter); - r.kill_entity(j); + entities_to_kill.push_back(j); } } } + + for (size_t entity_id : entities_to_kill) { + r.kill_entity(entity_id); + } for (auto &&[i, pos, vel, shoot, cld, __]: indexed_zipper(positions, velocities, shoots, update_clds, enabled)) { if (cld.last_send >= cld.ticks) { cld.loc = {pos.x, pos.y, vel.x, vel.y};