From 9b453277059fd015703873172d0dc87b4a29cb55 Mon Sep 17 00:00:00 2001 From: Aki Date: Fri, 3 Feb 2023 22:00:28 +0100 Subject: Created engine module right now containing only Point This might be a bit too generic of a name, but the intent is to get the main shared abstracts for gameplay loop and/or simulation outside of the game executable implementation to redirect dependencies. --- CMakeLists.txt | 1 + engine/CMakeLists.txt | 9 ++++ engine/include/kurator/engine/Point.h | 26 +++++++++++ engine/src/Point.cpp | 72 ++++++++++++++++++++++++++++++ kurator/src/Battle.cpp | 1 - kurator/src/Camera.h | 4 +- kurator/src/PopupEmitter.cpp | 4 +- kurator/src/components.h | 10 ++--- sim/CMakeLists.txt | 2 +- sim/include/kurator/sim/FloatingMovement.h | 4 +- sim/include/kurator/sim/Point.h | 26 ----------- sim/include/kurator/sim/components.h | 6 +-- sim/src/BaseBattle.cpp | 2 +- sim/src/Builder.cpp | 5 ++- sim/src/Point.cpp | 72 ------------------------------ sim/src/RandomSpawner.cpp | 4 +- 16 files changed, 130 insertions(+), 118 deletions(-) create mode 100644 engine/CMakeLists.txt create mode 100644 engine/include/kurator/engine/Point.h create mode 100644 engine/src/Point.cpp delete mode 100644 sim/include/kurator/sim/Point.h delete mode 100644 sim/src/Point.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 79eae9f..ae2a9b8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,7 @@ include(AddResources) include(AddVersionFile) add_subdirectory(campaign) add_subdirectory(contrib) +add_subdirectory(engine) add_subdirectory(kurator) add_subdirectory(sim) add_subdirectory(stats) diff --git a/engine/CMakeLists.txt b/engine/CMakeLists.txt new file mode 100644 index 0000000..041965a --- /dev/null +++ b/engine/CMakeLists.txt @@ -0,0 +1,9 @@ +project(engine) +add_library( + ${PROJECT_NAME} STATIC + src/Point.cpp +) +target_include_directories( + ${PROJECT_NAME} + PUBLIC include +) diff --git a/engine/include/kurator/engine/Point.h b/engine/include/kurator/engine/Point.h new file mode 100644 index 0000000..0e9f0ab --- /dev/null +++ b/engine/include/kurator/engine/Point.h @@ -0,0 +1,26 @@ +#pragma once + + +namespace kurator +{ +namespace engine +{ + + +struct Point +{ + double x; + double y; + double magnitude() const; + double distance(const Point& other) const; + double angle() 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; +}; + + +} // namespace engine +} // namespace kurator diff --git a/engine/src/Point.cpp b/engine/src/Point.cpp new file mode 100644 index 0000000..b5ce4eb --- /dev/null +++ b/engine/src/Point.cpp @@ -0,0 +1,72 @@ +#include + +#include + + +namespace kurator +{ +namespace engine +{ + + +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)); +} + + +double +Point::angle() const +{ + return std::atan2(y, x); // (+x, _) is 0 +} + + +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 engine +} // namespace kurator diff --git a/kurator/src/Battle.cpp b/kurator/src/Battle.cpp index d6df0b1..d300011 100644 --- a/kurator/src/Battle.cpp +++ b/kurator/src/Battle.cpp @@ -14,7 +14,6 @@ #include #include #include -#include #include #include #include diff --git a/kurator/src/Camera.h b/kurator/src/Camera.h index b6ca7d1..6056c7a 100644 --- a/kurator/src/Camera.h +++ b/kurator/src/Camera.h @@ -1,6 +1,6 @@ #pragma once -#include +#include namespace kurator @@ -9,7 +9,7 @@ namespace kurator struct Camera { - sim::Point offset = {}; + engine::Point offset = {}; double scale = 1.0; }; diff --git a/kurator/src/PopupEmitter.cpp b/kurator/src/PopupEmitter.cpp index c382ef1..0605da2 100644 --- a/kurator/src/PopupEmitter.cpp +++ b/kurator/src/PopupEmitter.cpp @@ -5,8 +5,8 @@ #include #include +#include #include -#include #include "components.h" @@ -26,7 +26,7 @@ PopupEmitter::emit(entt::registry& registry, const sim::Transform& transform, co registry.emplace(popup, transform.position, 0.0); registry.emplace(popup, std::string{}, 0, size, RED); registry.emplace(popup, 0, 0); - registry.emplace(popup, sim::Point{0, -20}, 0.9998); + registry.emplace(popup, engine::Point{0, -20}, 0.9998); } total += damage; auto& text = registry.get(popup); diff --git a/kurator/src/components.h b/kurator/src/components.h index bcd34e6..ff52bfe 100644 --- a/kurator/src/components.h +++ b/kurator/src/components.h @@ -4,7 +4,7 @@ #include -#include +#include namespace kurator @@ -27,14 +27,14 @@ struct CenteredText }; -struct UIOffset : public sim::Point +struct UIOffset : public engine::Point { }; struct PopMove { - sim::Point speed; + engine::Point speed; double damp; }; @@ -50,8 +50,8 @@ struct Marker struct Line { Color color; - sim::Point start; - sim::Point end; + engine::Point start; + engine::Point end; double hlength; double duration; double position = 0.0; diff --git a/sim/CMakeLists.txt b/sim/CMakeLists.txt index 1cae7b0..ef80c77 100644 --- a/sim/CMakeLists.txt +++ b/sim/CMakeLists.txt @@ -6,7 +6,6 @@ add_library( src/Builder.cpp src/FloatingMovement.cpp src/HitPoints.cpp - src/Point.cpp src/RandomSpawner.cpp src/TeamManager.cpp src/TurretControl.cpp @@ -19,6 +18,7 @@ target_link_libraries( ${PROJECT_NAME} PUBLIC EnTT::EnTT PUBLIC campaign + PUBLIC engine PRIVATE stats PUBLIC universe ) diff --git a/sim/include/kurator/sim/FloatingMovement.h b/sim/include/kurator/sim/FloatingMovement.h index 352377d..d7e8479 100644 --- a/sim/include/kurator/sim/FloatingMovement.h +++ b/sim/include/kurator/sim/FloatingMovement.h @@ -3,7 +3,7 @@ #include #include -#include "Point.h" +#include namespace kurator @@ -18,7 +18,7 @@ struct FloatingMovement double acceleration; double deceleration; double destination_boundary = 100.0; - Point speed = {0.0, 0.0}; + engine::Point speed = {0.0, 0.0}; static void update(entt::registry& registry, entt::dispatcher& dispatcher, float dt); }; diff --git a/sim/include/kurator/sim/Point.h b/sim/include/kurator/sim/Point.h deleted file mode 100644 index afd30ba..0000000 --- a/sim/include/kurator/sim/Point.h +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once - - -namespace kurator -{ -namespace sim -{ - - -struct Point -{ - double x; - double y; - double magnitude() const; - double distance(const Point& other) const; - double angle() 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; -}; - - -} // namespace sim -} // namespace kurator diff --git a/sim/include/kurator/sim/components.h b/sim/include/kurator/sim/components.h index f4f1799..e8b2591 100644 --- a/sim/include/kurator/sim/components.h +++ b/sim/include/kurator/sim/components.h @@ -2,7 +2,7 @@ #include -#include "Point.h" +#include namespace kurator @@ -13,7 +13,7 @@ namespace sim struct Transform { - Point position; + engine::Point position; double angle; entt::entity reference_frame = entt::null; }; @@ -28,7 +28,7 @@ struct Team struct AIState { double keep_at_range; - Point destination; + engine::Point destination; entt::entity target = entt::null; }; diff --git a/sim/src/BaseBattle.cpp b/sim/src/BaseBattle.cpp index 99a0199..3e3815c 100644 --- a/sim/src/BaseBattle.cpp +++ b/sim/src/BaseBattle.cpp @@ -89,7 +89,7 @@ BaseBattle::keep_at_range() if (!_registry.valid(ai.target)) continue; const auto target = _registry.get(ai.target); - const Point offset = target.position - self.position; + const auto offset = target.position - self.position; ai.destination = target.position - offset.normalized().scale(ai.keep_at_range); } } diff --git a/sim/src/Builder.cpp b/sim/src/Builder.cpp index b5762b3..279cd8b 100644 --- a/sim/src/Builder.cpp +++ b/sim/src/Builder.cpp @@ -2,10 +2,10 @@ #include +#include #include #include #include -#include #include #include #include @@ -19,6 +19,9 @@ namespace sim { +using engine::Point; + + Builder::Builder(entt::registry& _registry, Spawner& _spawner) : registry {_registry}, spawner {_spawner} diff --git a/sim/src/Point.cpp b/sim/src/Point.cpp deleted file mode 100644 index 1f49774..0000000 --- a/sim/src/Point.cpp +++ /dev/null @@ -1,72 +0,0 @@ -#include - -#include - - -namespace kurator -{ -namespace sim -{ - - -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)); -} - - -double -Point::angle() const -{ - return std::atan2(y, x); // (+x, _) is 0 -} - - -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 sim -} // namespace kurator diff --git a/sim/src/RandomSpawner.cpp b/sim/src/RandomSpawner.cpp index 7b85f21..a0b0f78 100644 --- a/sim/src/RandomSpawner.cpp +++ b/sim/src/RandomSpawner.cpp @@ -2,8 +2,8 @@ #include +#include #include -#include namespace kurator @@ -30,7 +30,7 @@ RandomSpawner::get(const int team) double facing = clean_angle + M_PI; if (facing > 2 * M_PI) facing -= 2 * M_PI; - const Point position { + const engine::Point position { distance * std::cos(angle), distance * std::sin(angle), }; -- cgit v1.1