summaryrefslogtreecommitdiff
path: root/sim
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
parent56ecf8834768fa61c299c07094c8760209f5bb45 (diff)
downloadkurator-087803532fd7dfa04bc9a39fd2886f572002d0b1.zip
kurator-087803532fd7dfa04bc9a39fd2886f572002d0b1.tar.gz
kurator-087803532fd7dfa04bc9a39fd2886f572002d0b1.tar.bz2
Extracted TurretControl to own unit
Diffstat (limited to 'sim')
-rw-r--r--sim/CMakeLists.txt1
-rw-r--r--sim/include/kurator/sim/TurretControl.h24
-rw-r--r--sim/include/kurator/sim/components.h9
-rw-r--r--sim/src/BaseBattle.cpp40
-rw-r--r--sim/src/Builder.cpp1
-rw-r--r--sim/src/TurretControl.cpp55
6 files changed, 83 insertions, 47 deletions
diff --git a/sim/CMakeLists.txt b/sim/CMakeLists.txt
index ff2621c..e10056a 100644
--- a/sim/CMakeLists.txt
+++ b/sim/CMakeLists.txt
@@ -8,6 +8,7 @@ add_library(
src/Point.cpp
src/RandomSpawner.cpp
src/TeamManager.cpp
+ src/TurretControl.cpp
)
target_include_directories(
${PROJECT_NAME}
diff --git a/sim/include/kurator/sim/TurretControl.h b/sim/include/kurator/sim/TurretControl.h
new file mode 100644
index 0000000..f6dc13a
--- /dev/null
+++ b/sim/include/kurator/sim/TurretControl.h
@@ -0,0 +1,24 @@
+#pragma once
+
+#include <entt/entity/entity.hpp>
+#include <entt/signal/dispatcher.hpp>
+
+
+namespace kurator
+{
+namespace sim
+{
+
+
+struct TurretControl
+{
+ double delay;
+ double reload;
+ int rounds;
+ entt::entity owner;
+ static void update(entt::registry& registry, entt::dispatcher& dispatcher, float dt);
+};
+
+
+} // namespace sim
+} // namespace kurator
diff --git a/sim/include/kurator/sim/components.h b/sim/include/kurator/sim/components.h
index 7190369..34b0f93 100644
--- a/sim/include/kurator/sim/components.h
+++ b/sim/include/kurator/sim/components.h
@@ -39,14 +39,5 @@ struct HitPoints
};
-struct TurretControl
-{
- double delay;
- double reload;
- int rounds;
- entt::entity owner;
-};
-
-
} // namespace sim
} // namespace kurator
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