summaryrefslogtreecommitdiff
path: root/sim/src
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2022-12-31 18:12:24 +0100
committerAki <please@ignore.pl>2022-12-31 18:12:24 +0100
commit087803532fd7dfa04bc9a39fd2886f572002d0b1 (patch)
tree808d6e8734065eddff69793d395deed0c86773f7 /sim/src
parent56ecf8834768fa61c299c07094c8760209f5bb45 (diff)
downloadkurator-087803532fd7dfa04bc9a39fd2886f572002d0b1.zip
kurator-087803532fd7dfa04bc9a39fd2886f572002d0b1.tar.gz
kurator-087803532fd7dfa04bc9a39fd2886f572002d0b1.tar.bz2
Extracted TurretControl to own unit
Diffstat (limited to 'sim/src')
-rw-r--r--sim/src/BaseBattle.cpp40
-rw-r--r--sim/src/Builder.cpp1
-rw-r--r--sim/src/TurretControl.cpp55
3 files changed, 58 insertions, 38 deletions
diff --git a/sim/src/BaseBattle.cpp b/sim/src/BaseBattle.cpp
index 1b27c2e..8ec603a 100644
--- a/sim/src/BaseBattle.cpp
+++ b/sim/src/BaseBattle.cpp
@@ -10,6 +10,7 @@
#include <kurator/sim/components.h>
#include <kurator/sim/events.h>
#include <kurator/sim/FloatingMovement.h>
+#include <kurator/sim/TurretControl.h>
#include <kurator/stats/events.h>
#include <kurator/universe.h>
@@ -64,7 +65,7 @@ BaseBattle::update(const float dt)
pick_random_targets();
keep_at_range();
FloatingMovement::update(_registry, _dispatcher, dt);
- turrets(dt);
+ TurretControl::update(_registry, _dispatcher, dt);
kill_off_dead();
manager.clear(_registry); // registry supports on destructions events
manager.update(_dispatcher);
@@ -97,43 +98,6 @@ BaseBattle::keep_at_range()
void
-BaseBattle::turrets(const float dt)
-{
- auto view = _registry.view<TurretControl, universe::TurretType>();
- for (auto&& [entity, control, def] : view.each()) {
- if (!_registry.valid(control.owner)) {
- _registry.destroy(entity);
- continue;
- }
- const auto& [state, transform] = _registry.get<AIState, Transform>(control.owner); // no checks
- if (!_registry.valid(state.target))
- continue;
- if (control.reload > 0.0)
- control.reload -= dt;
- if (control.rounds < 1 && control.reload <= 0.0)
- control.rounds = def.rounds;
- if (control.delay > 0.0)
- control.delay -= dt;
- if (control.rounds > 0 && control.delay <= 0.0) {
- auto& target_points = _registry.get<HitPoints>(state.target);
- const auto& target = _registry.get<Transform>(state.target);
- const auto distance = transform.position.distance(target.position);
- if (distance > def.optimal_range * 2.5)
- continue;
- const auto damage = def.effective_damage(distance);
- if (damage > 0.0) {
- target_points.health -= damage;
- _dispatcher.trigger(Hit{damage, control.owner, state.target});
- }
- control.delay = def.rate_of_fire;
- if (--control.rounds < 1)
- control.reload = def.reload;
- }
- }
-}
-
-
-void
BaseBattle::kill_off_dead()
{
auto view = _registry.view<HitPoints>();
diff --git a/sim/src/Builder.cpp b/sim/src/Builder.cpp
index 068d012..11e0f1e 100644
--- a/sim/src/Builder.cpp
+++ b/sim/src/Builder.cpp
@@ -4,6 +4,7 @@
#include <kurator/sim/components.h>
#include <kurator/sim/FloatingMovement.h>
+#include <kurator/sim/TurretControl.h>
#include <kurator/sim/Point.h>
#include <kurator/universe/ShipType.h>
#include <kurator/universe/TurretType.h>
diff --git a/sim/src/TurretControl.cpp b/sim/src/TurretControl.cpp
new file mode 100644
index 0000000..b5527fc
--- /dev/null
+++ b/sim/src/TurretControl.cpp
@@ -0,0 +1,55 @@
+#include <kurator/sim/TurretControl.h>
+
+#include <entt/entity/registry.hpp>
+#include <entt/signal/dispatcher.hpp>
+
+#include <kurator/sim/components.h>
+#include <kurator/sim/events.h>
+#include <kurator/universe/TurretType.h>
+
+
+namespace kurator
+{
+namespace sim
+{
+
+
+void
+TurretControl::update(entt::registry& registry, entt::dispatcher& dispatcher, const float dt)
+{
+ auto view = registry.view<TurretControl, universe::TurretType>();
+ for (auto&& [entity, control, def] : view.each()) {
+ if (!registry.valid(control.owner)) {
+ registry.destroy(entity);
+ continue;
+ }
+ const auto& [state, transform] = registry.get<AIState, Transform>(control.owner); // no checks
+ if (!registry.valid(state.target))
+ continue;
+ if (control.reload > 0.0)
+ control.reload -= dt;
+ if (control.rounds < 1 && control.reload <= 0.0)
+ control.rounds = def.rounds;
+ if (control.delay > 0.0)
+ control.delay -= dt;
+ if (control.rounds > 0 && control.delay <= 0.0) {
+ auto& target_points = registry.get<HitPoints>(state.target);
+ const auto& target = registry.get<Transform>(state.target);
+ const auto distance = transform.position.distance(target.position);
+ if (distance > def.optimal_range * 2.5)
+ continue;
+ const auto damage = def.effective_damage(distance);
+ if (damage > 0.0) {
+ target_points.health -= damage;
+ dispatcher.trigger(Hit{damage, control.owner, state.target});
+ }
+ control.delay = def.rate_of_fire;
+ if (--control.rounds < 1)
+ control.reload = def.reload;
+ }
+ }
+}
+
+
+} // namespace sim
+} // namespace kurator