Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions ECS/src/systems/basics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,16 @@ void logging_system(registry const &,
void clock_system(registry &r,
sparse_array<comp::clock> &clocks,
sparse_array<comp::enabled> 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<size_t> 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);
}
}
}
72 changes: 55 additions & 17 deletions server/src/systems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ void kill_enemy_system(registry &reg,
sparse_array<killable> const &killables,
sparse_array<monster> const &monsters,
sparse_array<comp::enabled> const &enabled) {
std::vector<size_t> 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) {
Expand All @@ -57,9 +58,12 @@ void kill_enemy_system(registry &reg,
}
}
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 &,
Expand Down Expand Up @@ -103,64 +107,86 @@ void kill_monster_shoot_system(registry &r,
sparse_array<comp::position> const &positions,
sparse_array<monster_shoot> const &shoots,
sparse_array<comp::enabled> const &enabled) {
std::vector<size_t> 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<comp::position> const &positions,
sparse_array<shoot> const &shoots,
sparse_array<comp::enabled> const &enabled) {
std::vector<size_t> 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,
sparse_array<comp::position> const &positions,
sparse_array<killable> const &killables,
sparse_array<player> const &players,
sparse_array<comp::enabled> const &enabled) {
std::vector<size_t> 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,
sparse_array<player> &players,
sparse_array<comp::clock> &clocks,
sparse_array<comp::position> const &positions,
sparse_array<comp::enabled> const &enabled) {
struct MissileToSpawn {
size_t player_idx;
comp::position pos;
sendFct const *pushToSendQueue;
};
std::vector<MissileToSpawn> 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<comp::position>()[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<comp::position>()[ent]);
(*missile_data.pushToSendQueue)(ent, SAFE, NEW_MISSILE, sizeof(*missile_pos), &*missile_pos);
}
}

void monster_shoot_collision_system(registry &r,
Expand All @@ -172,16 +198,22 @@ void monster_shoot_collision_system(registry &r,
sparse_array<update_location> &update_clds,
sparse_array<killable> &killables,
sparse_array<comp::enabled> const &enabled) {
std::vector<size_t> 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};
Expand All @@ -201,16 +233,22 @@ void player_shoot_collision_system(registry &r,
sparse_array<update_location> &update_clds,
sparse_array<killable> &killables,
sparse_array<comp::enabled> const &enabled) {
std::vector<size_t> 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};
Expand Down