summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2022-11-12 14:28:39 +0100
committerAki <please@ignore.pl>2022-11-12 14:28:39 +0100
commitc6d17ef545978bec555c8af8ef5eaef82c44e5f1 (patch)
tree461fff66de35a9f26aebde8cffc6386efa8e104f
parent0e57ea98049658b4d82488c43d71b4ced534c2af (diff)
downloadkurator-c6d17ef545978bec555c8af8ef5eaef82c44e5f1.zip
kurator-c6d17ef545978bec555c8af8ef5eaef82c44e5f1.tar.gz
kurator-c6d17ef545978bec555c8af8ef5eaef82c44e5f1.tar.bz2
Implemented very basic linear move-to-dest component
-rw-r--r--battles/CMakeLists.txt1
-rw-r--r--battles/include/kurator/battles/Point.h7
-rw-r--r--battles/include/kurator/battles/components.h13
-rw-r--r--battles/src/BaseBattle.cpp15
-rw-r--r--battles/src/Point.cpp65
5 files changed, 97 insertions, 4 deletions
diff --git a/battles/CMakeLists.txt b/battles/CMakeLists.txt
index 37d1903..261edd7 100644
--- a/battles/CMakeLists.txt
+++ b/battles/CMakeLists.txt
@@ -3,6 +3,7 @@ add_library(
${PROJECT_NAME}
src/BaseBattle.cpp
src/Battle.cpp
+ src/Point.cpp
src/RandomSpawner.cpp
src/scenarios.cpp
)
diff --git a/battles/include/kurator/battles/Point.h b/battles/include/kurator/battles/Point.h
index 71a6993..b6be651 100644
--- a/battles/include/kurator/battles/Point.h
+++ b/battles/include/kurator/battles/Point.h
@@ -11,6 +11,13 @@ struct Point
{
double x;
double y;
+ double magnitude() const;
+ double distance(const Point& other) const;
+ Point rotate(double angle) const;
+ Point scale(double _scale) const;
+ Point normalized() const;
+ Point operator-(const Point& other) const;
+ Point operator+(const Point& other) const;
};
diff --git a/battles/include/kurator/battles/components.h b/battles/include/kurator/battles/components.h
index 307acc3..0c1e280 100644
--- a/battles/include/kurator/battles/components.h
+++ b/battles/include/kurator/battles/components.h
@@ -25,5 +25,18 @@ struct Team
};
+struct AIState
+{
+ Point destination;
+ entt::entity target = entt::null;
+};
+
+
+struct FloatingMovement
+{
+ double speed; // linear and instant angular for now
+};
+
+
} // namespace battles
} // namespace kurator
diff --git a/battles/src/BaseBattle.cpp b/battles/src/BaseBattle.cpp
index a9ac45c..8527a56 100644
--- a/battles/src/BaseBattle.cpp
+++ b/battles/src/BaseBattle.cpp
@@ -28,6 +28,8 @@ BaseBattle::BaseBattle(const Scenario& scenario) :
_registry.emplace<universe::ShipType>(entity, ship.type);
_registry.emplace<Team>(entity, ship.team);
_registry.emplace<Transform>(entity, spawner.get(ship.team));
+ _registry.emplace<FloatingMovement>(entity, 0.4);
+ _registry.emplace<AIState>(entity, Point{0.0, 0.0});
}
}
@@ -42,10 +44,15 @@ BaseBattle::registry()
void
BaseBattle::update(const float dt)
{
- auto view = _registry.view<Transform>();
- for (auto&& [entity, transform] : view.each()) {
- transform.position.x += 0.1 * dt * std::cos(transform.angle);
- transform.position.y += 0.1 * dt * std::sin(transform.angle);
+ auto view = _registry.view<Transform, AIState, FloatingMovement>();
+ for (auto&& [entity, transform, state, movement] : view.each()) {
+ const double speed = movement.speed * dt;
+ const Point diff = (state.destination - transform.position);
+ if (diff.magnitude() > speed) {
+ const Point eff = diff.normalized().scale(speed);
+ transform.position.x += eff.x;
+ transform.position.y += eff.y;
+ }
}
}
diff --git a/battles/src/Point.cpp b/battles/src/Point.cpp
new file mode 100644
index 0000000..9ffa81c
--- /dev/null
+++ b/battles/src/Point.cpp
@@ -0,0 +1,65 @@
+#include <kurator/battles/Point.h>
+
+#include <cmath>
+
+
+namespace kurator
+{
+namespace battles
+{
+
+
+double
+Point::magnitude() const
+{
+ return std::sqrt(std::pow(x, 2) + std::pow(y, 2));
+}
+
+
+double
+Point::distance(const Point& other) const
+{
+ return std::sqrt(std::pow(other.x - x, 2) + std::pow(other.y - y, 2));
+}
+
+
+Point
+Point::rotate(const double angle) const
+{
+ return {
+ x * std::cos(angle) - y * std::sin(angle),
+ x * std::sin(angle) + y * std::cos(angle),
+ };
+}
+
+
+Point
+Point::scale(const double _scale) const
+{
+ return {x * _scale, y * _scale};
+}
+
+
+Point
+Point::normalized() const
+{
+ return scale(1.0 / magnitude());
+}
+
+
+Point
+Point::operator-(const Point& other) const
+{
+ return {x - other.x, y - other.y};
+}
+
+
+Point
+Point::operator+(const Point& other) const
+{
+ return {x + other.x, y + other.y};
+}
+
+
+} // namespace battles
+} // namespace kurator