aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/system/AISystem.cpp
diff options
context:
space:
mode:
authormax-001 <maxsmits21@kpnmail.nl>2024-12-09 13:49:34 +0100
committermax-001 <maxsmits21@kpnmail.nl>2024-12-09 13:49:34 +0100
commitcdaf587006053874c2e286a7541e6a2b246ce2b3 (patch)
treeba58085b181873a803e1009e64efe22314e75947 /src/crepe/system/AISystem.cpp
parenta12986c0855e68ac027c2686223813c112960165 (diff)
Added comments
Diffstat (limited to 'src/crepe/system/AISystem.cpp')
-rw-r--r--src/crepe/system/AISystem.cpp23
1 files changed, 22 insertions, 1 deletions
diff --git a/src/crepe/system/AISystem.cpp b/src/crepe/system/AISystem.cpp
index 72f3d9b..8be5ddc 100644
--- a/src/crepe/system/AISystem.cpp
+++ b/src/crepe/system/AISystem.cpp
@@ -20,13 +20,21 @@ void AISystem::update() {
//TODO: Use fixed loop dt (this is not available at master at the moment)
double dt = LoopTimer::get_instance().get_delta_time();
+ // Loop through all AI components
for (AI & ai : ai_components) {
+ if (!ai.active) {
+ continue;
+ }
+
RefVector<Rigidbody> rigidbodies
= mgr.get_components_by_id<Rigidbody>(ai.game_object_id);
Rigidbody & rigidbody = rigidbodies.front().get();
+ // Calculate the force to apply to the entity
vec2 force = this->calculate(ai);
+ // Calculate the acceleration (using the above calculated force)
vec2 acceleration = force / rigidbody.data.mass;
+ // Finally, update Rigidbody's velocity
rigidbody.data.linear_velocity += acceleration * dt;
}
}
@@ -34,6 +42,7 @@ void AISystem::update() {
vec2 AISystem::calculate(AI & ai) {
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);
@@ -71,13 +80,16 @@ bool AISystem::accumulate_force(AI & ai, vec2 & running_total, vec2 force_to_add
float magnitude_remaining = ai.max_force - magnitude;
if (magnitude_remaining <= 0.0f) {
+ // If the force is already at/above the max force, return false
return false;
}
float magnitude_to_add = force_to_add.length();
if (magnitude_to_add < magnitude_remaining) {
+ // If the force to add is less than the remaining force, add it
running_total += force_to_add;
} else {
+ // If the force to add is greater than the remaining force, add a fraction of it
force_to_add.normalize();
running_total += force_to_add * magnitude_remaining;
}
@@ -93,6 +105,7 @@ vec2 AISystem::seek(const AI & ai) {
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;
desired_velocity.normalize();
desired_velocity *= rigidbody.data.max_linear_velocity;
@@ -108,11 +121,11 @@ vec2 AISystem::flee(const AI & ai) {
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;
if (desired_velocity.length_squared() > ai.square_flee_panic_distance) {
return vec2{0, 0};
}
-
desired_velocity.normalize();
desired_velocity *= rigidbody.data.max_linear_velocity;
@@ -127,6 +140,7 @@ vec2 AISystem::arrive(const AI & ai) {
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) {
@@ -152,21 +166,28 @@ vec2 AISystem::path_follow(AI & ai) {
return vec2{0, 0};
}
+ // Get the target node
vec2 target = ai.path.at(ai.path_index);
+ // Calculate the force to apply to the entity
vec2 to_target = target - transform.position;
if (to_target.length_squared() > ai.path_node_distance * ai.path_node_distance) {
+ // If the entity is not close enough to the target node, seek it
ai.seek_target = target;
} else {
+ // If the entity is close enough to the target node, move to the next node
ai.path_index++;
if (ai.path_index >= ai.path.size()) {
if (ai.path_loop) {
+ // If the path is looping, reset the path index
ai.path_index = 0;
} else {
+ // If the path is not looping, arrive at the last node
ai.path_index = ai.path.size() - 1;
return this->arrive(ai);
}
}
}
+ // Seek the target node
return this->seek(ai);
}