aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/system/AISystem.cpp
diff options
context:
space:
mode:
authormax-001 <maxsmits21@kpnmail.nl>2024-12-10 08:48:06 +0100
committermax-001 <maxsmits21@kpnmail.nl>2024-12-10 08:48:06 +0100
commitc8d05f759bb1be0ec99e8f23eaa65c80c36ce03d (patch)
tree8d176b831b9619cffc953f368be2eefc9e52c8ca /src/crepe/system/AISystem.cpp
parente5e2eefc7f0f6199fe2b36688b2ad64a97784717 (diff)
Implemented feedback
Diffstat (limited to 'src/crepe/system/AISystem.cpp')
-rw-r--r--src/crepe/system/AISystem.cpp46
1 files changed, 24 insertions, 22 deletions
diff --git a/src/crepe/system/AISystem.cpp b/src/crepe/system/AISystem.cpp
index 62fa553..4d5039b 100644
--- a/src/crepe/system/AISystem.cpp
+++ b/src/crepe/system/AISystem.cpp
@@ -2,7 +2,6 @@
#include <cmath>
#include "api/LoopTimer.h"
-#include "api/Rigidbody.h"
#include "api/Transform.h"
#include "manager/ComponentManager.h"
#include "manager/Mediator.h"
@@ -27,10 +26,17 @@ void AISystem::update() {
RefVector<Rigidbody> rigidbodies
= mgr.get_components_by_id<Rigidbody>(ai.game_object_id);
+ if (rigidbodies.empty()) {
+ throw std::runtime_error(
+ "AI component must be attached to a GameObject with a Rigidbody component");
+ }
Rigidbody & rigidbody = rigidbodies.front().get();
+ if (rigidbody.data.mass <= 0) {
+ throw std::runtime_error("Mass must be greater than 0");
+ }
// Calculate the force to apply to the entity
- vec2 force = this->calculate(ai);
+ vec2 force = this->calculate(ai, rigidbody);
// Calculate the acceleration (using the above calculated force)
vec2 acceleration = force / rigidbody.data.mass;
// Finally, update Rigidbody's velocity
@@ -38,33 +44,33 @@ void AISystem::update() {
}
}
-vec2 AISystem::calculate(AI & ai) {
+vec2 AISystem::calculate(AI & ai, const Rigidbody & rigidbody) {
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);
+ vec2 force_to_add = this->flee(ai, rigidbody);
if (!this->accumulate_force(ai, force, force_to_add)) {
return force;
}
}
if (ai.on(AI::BehaviorType::ARRIVE)) {
- vec2 force_to_add = this->arrive(ai);
+ vec2 force_to_add = this->arrive(ai, rigidbody);
if (!this->accumulate_force(ai, force, force_to_add)) {
return force;
}
}
if (ai.on(AI::BehaviorType::SEEK)) {
- vec2 force_to_add = this->seek(ai);
+ vec2 force_to_add = this->seek(ai, rigidbody);
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);
+ vec2 force_to_add = this->path_follow(ai, rigidbody);
if (!this->accumulate_force(ai, force, force_to_add)) {
return force;
@@ -74,7 +80,7 @@ vec2 AISystem::calculate(AI & ai) {
return force;
}
-bool AISystem::accumulate_force(AI & ai, vec2 & running_total, vec2 force_to_add) {
+bool AISystem::accumulate_force(const AI & ai, vec2 & running_total, vec2 force_to_add) {
float magnitude = running_total.length();
float magnitude_remaining = ai.max_force - magnitude;
@@ -96,13 +102,11 @@ bool AISystem::accumulate_force(AI & ai, vec2 & running_total, vec2 force_to_add
return true;
}
-vec2 AISystem::seek(const AI & ai) {
+vec2 AISystem::seek(const AI & ai, const Rigidbody & rigidbody) const {
const Mediator & mediator = this->mediator;
ComponentManager & mgr = mediator.component_manager;
RefVector<Transform> transforms = mgr.get_components_by_id<Transform>(ai.game_object_id);
Transform & transform = transforms.front().get();
- RefVector<Rigidbody> rigidbodies = mgr.get_components_by_id<Rigidbody>(ai.game_object_id);
- Rigidbody & rigidbody = rigidbodies.front().get();
// Calculate the desired velocity
vec2 desired_velocity = ai.seek_target - transform.position;
@@ -112,13 +116,11 @@ vec2 AISystem::seek(const AI & ai) {
return desired_velocity - rigidbody.data.linear_velocity;
}
-vec2 AISystem::flee(const AI & ai) {
+vec2 AISystem::flee(const AI & ai, const Rigidbody & rigidbody) const {
const Mediator & mediator = this->mediator;
ComponentManager & mgr = mediator.component_manager;
RefVector<Transform> transforms = mgr.get_components_by_id<Transform>(ai.game_object_id);
Transform & transform = transforms.front().get();
- RefVector<Rigidbody> rigidbodies = mgr.get_components_by_id<Rigidbody>(ai.game_object_id);
- Rigidbody & rigidbody = rigidbodies.front().get();
// Calculate the desired velocity if the entity is within the panic distance
vec2 desired_velocity = transform.position - ai.flee_target;
@@ -131,18 +133,20 @@ vec2 AISystem::flee(const AI & ai) {
return desired_velocity - rigidbody.data.linear_velocity;
}
-vec2 AISystem::arrive(const AI & ai) {
+vec2 AISystem::arrive(const AI & ai, const Rigidbody & rigidbody) const {
const Mediator & mediator = this->mediator;
ComponentManager & mgr = mediator.component_manager;
RefVector<Transform> transforms = mgr.get_components_by_id<Transform>(ai.game_object_id);
Transform & transform = transforms.front().get();
- RefVector<Rigidbody> rigidbodies = mgr.get_components_by_id<Rigidbody>(ai.game_object_id);
- Rigidbody & rigidbody = rigidbodies.front().get();
// Calculate the desired velocity (taking into account the deceleration rate)
vec2 to_target = ai.seek_target - transform.position;
float distance = to_target.length();
if (distance > 0.0f) {
+ if (ai.arrive_deceleration <= 0.0f) {
+ throw std::runtime_error("Deceleration rate must be greater than 0");
+ }
+
float speed = distance / ai.arrive_deceleration;
speed = std::min(speed, rigidbody.data.max_linear_velocity.length());
vec2 desired_velocity = to_target * (speed / distance);
@@ -153,13 +157,11 @@ vec2 AISystem::arrive(const AI & ai) {
return vec2{0, 0};
}
-vec2 AISystem::path_follow(AI & ai) {
+vec2 AISystem::path_follow(AI & ai, const Rigidbody & rigidbody) {
const Mediator & mediator = this->mediator;
ComponentManager & mgr = mediator.component_manager;
RefVector<Transform> transforms = mgr.get_components_by_id<Transform>(ai.game_object_id);
Transform & transform = transforms.front().get();
- RefVector<Rigidbody> rigidbodies = mgr.get_components_by_id<Rigidbody>(ai.game_object_id);
- Rigidbody & rigidbody = rigidbodies.front().get();
if (ai.path.empty()) {
return vec2{0, 0};
@@ -182,11 +184,11 @@ vec2 AISystem::path_follow(AI & ai) {
} else {
// If the path is not looping, arrive at the last node
ai.path_index = ai.path.size() - 1;
- return this->arrive(ai);
+ return this->arrive(ai, rigidbody);
}
}
}
// Seek the target node
- return this->seek(ai);
+ return this->seek(ai, rigidbody);
}