From 6fc38e15e4b88d480d5fcb69eec36f7d8685e853 Mon Sep 17 00:00:00 2001 From: max-001 Date: Tue, 10 Dec 2024 09:21:58 +0100 Subject: Passing transform by reference --- src/crepe/system/AISystem.cpp | 49 ++++++++++++++++--------------------------- src/crepe/system/AISystem.h | 13 ++++++++---- 2 files changed, 27 insertions(+), 35 deletions(-) (limited to 'src') diff --git a/src/crepe/system/AISystem.cpp b/src/crepe/system/AISystem.cpp index b42cb11..d1ebeb5 100644 --- a/src/crepe/system/AISystem.cpp +++ b/src/crepe/system/AISystem.cpp @@ -2,7 +2,6 @@ #include #include "api/LoopTimer.h" -#include "api/Transform.h" #include "manager/ComponentManager.h" #include "manager/Mediator.h" @@ -45,32 +44,37 @@ void AISystem::update() { } vec2 AISystem::calculate(AI & ai, const Rigidbody & rigidbody) { + const Mediator & mediator = this->mediator; + ComponentManager & mgr = mediator.component_manager; + RefVector transforms = mgr.get_components_by_id(ai.game_object_id); + Transform & transform = transforms.front().get(); + vec2 force; // Run all the behaviors that are on, and stop if the force gets too high if (ai.on(AI::BehaviorType::FLEE)) { - vec2 force_to_add = this->flee(ai, rigidbody); + vec2 force_to_add = this->flee(ai, rigidbody, transform); if (!this->accumulate_force(ai, force, force_to_add)) { return force; } } if (ai.on(AI::BehaviorType::ARRIVE)) { - vec2 force_to_add = this->arrive(ai, rigidbody); + vec2 force_to_add = this->arrive(ai, rigidbody, transform); if (!this->accumulate_force(ai, force, force_to_add)) { return force; } } if (ai.on(AI::BehaviorType::SEEK)) { - vec2 force_to_add = this->seek(ai, rigidbody); + vec2 force_to_add = this->seek(ai, rigidbody, transform); if (!this->accumulate_force(ai, force, force_to_add)) { return force; } } if (ai.on(AI::BehaviorType::PATH_FOLLOW)) { - vec2 force_to_add = this->path_follow(ai, rigidbody); + vec2 force_to_add = this->path_follow(ai, rigidbody, transform); if (!this->accumulate_force(ai, force, force_to_add)) { return force; @@ -102,12 +106,8 @@ bool AISystem::accumulate_force(const AI & ai, vec2 & running_total, vec2 & forc return true; } -vec2 AISystem::seek(const AI & ai, const Rigidbody & rigidbody) const { - const Mediator & mediator = this->mediator; - ComponentManager & mgr = mediator.component_manager; - RefVector transforms = mgr.get_components_by_id(ai.game_object_id); - Transform & transform = transforms.front().get(); - +vec2 AISystem::seek(const AI & ai, const Rigidbody & rigidbody, + const Transform & transform) const { // Calculate the desired velocity vec2 desired_velocity = ai.seek_target - transform.position; desired_velocity.normalize(); @@ -116,12 +116,8 @@ vec2 AISystem::seek(const AI & ai, const Rigidbody & rigidbody) const { return desired_velocity - rigidbody.data.linear_velocity; } -vec2 AISystem::flee(const AI & ai, const Rigidbody & rigidbody) const { - const Mediator & mediator = this->mediator; - ComponentManager & mgr = mediator.component_manager; - RefVector transforms = mgr.get_components_by_id(ai.game_object_id); - Transform & transform = transforms.front().get(); - +vec2 AISystem::flee(const AI & ai, const Rigidbody & rigidbody, + const Transform & transform) const { // Calculate the desired velocity if the entity is within the panic distance vec2 desired_velocity = transform.position - ai.flee_target; if (desired_velocity.length_squared() > ai.square_flee_panic_distance) { @@ -133,12 +129,8 @@ vec2 AISystem::flee(const AI & ai, const Rigidbody & rigidbody) const { return desired_velocity - rigidbody.data.linear_velocity; } -vec2 AISystem::arrive(const AI & ai, const Rigidbody & rigidbody) const { - const Mediator & mediator = this->mediator; - ComponentManager & mgr = mediator.component_manager; - RefVector transforms = mgr.get_components_by_id(ai.game_object_id); - Transform & transform = transforms.front().get(); - +vec2 AISystem::arrive(const AI & ai, const Rigidbody & rigidbody, + const Transform & transform) const { // Calculate the desired velocity (taking into account the deceleration rate) vec2 to_target = ai.seek_target - transform.position; float distance = to_target.length(); @@ -157,12 +149,7 @@ vec2 AISystem::arrive(const AI & ai, const Rigidbody & rigidbody) const { return vec2{0, 0}; } -vec2 AISystem::path_follow(AI & ai, const Rigidbody & rigidbody) { - const Mediator & mediator = this->mediator; - ComponentManager & mgr = mediator.component_manager; - RefVector transforms = mgr.get_components_by_id(ai.game_object_id); - Transform & transform = transforms.front().get(); - +vec2 AISystem::path_follow(AI & ai, const Rigidbody & rigidbody, const Transform & transform) { if (ai.path.empty()) { return vec2{0, 0}; } @@ -184,11 +171,11 @@ vec2 AISystem::path_follow(AI & ai, const Rigidbody & rigidbody) { } else { // If the path is not looping, arrive at the last node ai.path_index = ai.path.size() - 1; - return this->arrive(ai, rigidbody); + return this->arrive(ai, rigidbody, transform); } } } // Seek the target node - return this->seek(ai, rigidbody); + return this->seek(ai, rigidbody, transform); } diff --git a/src/crepe/system/AISystem.h b/src/crepe/system/AISystem.h index 9a937d2..d5f8a8e 100644 --- a/src/crepe/system/AISystem.h +++ b/src/crepe/system/AISystem.h @@ -4,6 +4,7 @@ #include "api/Rigidbody.h" #include "System.h" +#include "api/Transform.h" #include "types.h" namespace crepe { @@ -44,33 +45,37 @@ private: * * \param ai The AI component * \param rigidbody The Rigidbody component + * \param transform The Transform component * \return The seek force */ - vec2 seek(const AI & ai, const Rigidbody & rigidbody) const; + vec2 seek(const AI & ai, const Rigidbody & rigidbody, const Transform & transform) const; /** * \brief Calculate the flee force * * \param ai The AI component * \param rigidbody The Rigidbody component + * \param transform The Transform component * \return The flee force */ - vec2 flee(const AI & ai, const Rigidbody & rigidbody) const; + vec2 flee(const AI & ai, const Rigidbody & rigidbody, const Transform & transform) const; /** * \brief Calculate the arrive force * * \param ai The AI component * \param rigidbody The Rigidbody component + * \param transform The Transform component * \return The arrive force */ - vec2 arrive(const AI & ai, const Rigidbody & rigidbody) const; + vec2 arrive(const AI & ai, const Rigidbody & rigidbody, const Transform & transform) const; /** * \brief Calculate the path follow force * * \param ai The AI component * \param rigidbody The Rigidbody component + * \param transform The Transform component * \return The path follow force */ - vec2 path_follow(AI & ai, const Rigidbody & rigidbody); + vec2 path_follow(AI & ai, const Rigidbody & rigidbody, const Transform & transform); }; } // namespace crepe -- cgit v1.2.3