From 4f3de714cd04ee5d99167f415843573d6bbd9f62 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 14 Nov 2022 00:32:08 +0100 Subject: Implemented naive turrets and targeting mechanics Coupling between components, implementation of systems and behaviour of some state-related methods should be refactored, but the overall look is nice. --- battles/include/kurator/battles/components.h | 7 ++++++ battles/src/BaseBattle.cpp | 26 ++++++++++++++++++++- battles/src/TeamManager.cpp | 2 ++ battles/src/scenarios.cpp | 35 ++++++++++++++-------------- 4 files changed, 52 insertions(+), 18 deletions(-) diff --git a/battles/include/kurator/battles/components.h b/battles/include/kurator/battles/components.h index 35a9251..8f5813b 100644 --- a/battles/include/kurator/battles/components.h +++ b/battles/include/kurator/battles/components.h @@ -44,5 +44,12 @@ struct HitPoints }; +struct TurretControl +{ + double reload; + entt::entity owner; +}; + + } // namespace battles } // namespace kurator diff --git a/battles/src/BaseBattle.cpp b/battles/src/BaseBattle.cpp index 4d80671..815c683 100644 --- a/battles/src/BaseBattle.cpp +++ b/battles/src/BaseBattle.cpp @@ -31,6 +31,12 @@ BaseBattle::BaseBattle(const Scenario& scenario) : _registry.emplace(entity, 0.4); _registry.emplace(entity, Point{0.0, 0.0}); _registry.emplace(entity, ship.type.base_health_points); + for (const auto& turret_def : ship.turrets) { + const auto turret = _registry.create(); + _registry.emplace(turret, turret_def); + _registry.emplace(turret, 0.0, entity); + _registry.emplace(turret, Point{0.0, 0.0}, 0.0, entity); + } manager.add(ship.team, entity); // registry supports on construction events } } @@ -50,6 +56,8 @@ BaseBattle::update(const float dt) for (auto&& [entity, team, transform, state, movement] : view.each()) { if (!_registry.valid(state.target)) state.target = manager.random((team.id + 1) % 2); + if (!_registry.valid(state.target)) + continue; const auto target = _registry.get(state.target); const double speed = movement.speed * dt; const Point diff = target.position - transform.position; @@ -63,10 +71,26 @@ BaseBattle::update(const float dt) } auto view2 = _registry.view(); for (auto&& [entity, points] : view2.each()) { - points.health -= dt; if (points.health <= 0.0) _registry.destroy(entity); } + auto view3 = _registry.view(); + for (auto&& [entity, control, def] : view3.each()) { // split into systems! + if (!_registry.valid(control.owner)) { + _registry.destroy(entity); + continue; + } + const auto& state = _registry.get(control.owner); // no checks + if (!_registry.valid(state.target)) + continue; + if (control.reload > 0.0) + control.reload -= dt; + if (control.reload <= 0.0) { + auto& target_points = _registry.get(state.target); + target_points.health -= def.base_damage; + control.reload = def.rate_of_fire; + } + } manager.clear(_registry); // registry supports on destructions events } diff --git a/battles/src/TeamManager.cpp b/battles/src/TeamManager.cpp index e4afeb3..9dc90e3 100644 --- a/battles/src/TeamManager.cpp +++ b/battles/src/TeamManager.cpp @@ -41,6 +41,8 @@ entt::entity TeamManager::random(int team) { auto& members = teams.at(team); + if (members.size() == 0) + return entt::null; std::uniform_int_distribution uniform{0, members.size() - 1}; return members.at(uniform(generator)); } diff --git a/battles/src/scenarios.cpp b/battles/src/scenarios.cpp index cf348ca..3b35f44 100644 --- a/battles/src/scenarios.cpp +++ b/battles/src/scenarios.cpp @@ -17,26 +17,27 @@ example() { const universe::ShipType halo {"halo", 6.0}; const universe::ShipType cube {"cube", 12.0}; - const universe::ShipType bell {"bell", 20.0}; + const universe::ShipType bell {"bell", 30.0}; + const universe::TurretType cannon {"cannon", 1.0, 1.0, 0.5}; return { "example", { - {0, halo, {}}, - {0, halo, {}}, - {0, cube, {}}, - {0, cube, {}}, - {0, cube, {}}, - {0, bell, {}}, - {0, bell, {}}, - {0, bell, {}}, - {1, halo, {}}, - {1, halo, {}}, - {1, bell, {}}, - {1, cube, {}}, - {1, cube, {}}, - {1, cube, {}}, - {1, bell, {}}, - {1, bell, {}}, + {0, halo, {cannon}}, + {0, halo, {cannon}}, + {0, cube, {cannon, cannon}}, + {0, cube, {cannon, cannon}}, + {0, cube, {cannon, cannon}}, + {0, bell, {cannon}}, + {0, bell, {cannon}}, + {0, bell, {cannon}}, + {1, halo, {cannon}}, + {1, halo, {cannon}}, + {1, bell, {cannon}}, + {1, cube, {cannon, cannon}}, + {1, cube, {cannon, cannon}}, + {1, cube, {cannon, cannon}}, + {1, bell, {cannon}}, + {1, bell, {cannon}}, }, }; } -- cgit v1.1