diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/crepe/system/AISystem.cpp | 21 | ||||
-rw-r--r-- | src/crepe/system/AISystem.h | 5 |
2 files changed, 18 insertions, 8 deletions
diff --git a/src/crepe/system/AISystem.cpp b/src/crepe/system/AISystem.cpp index d496e12..4858000 100644 --- a/src/crepe/system/AISystem.cpp +++ b/src/crepe/system/AISystem.cpp @@ -33,7 +33,7 @@ vec2 AISystem::calculate(AI & ai) { if (ai.on(AI::BehaviorType::SEEK)) { vec2 force_to_add = this->seek(ai); - if (!this->accumulate_force(force, force_to_add)) { + if (!this->accumulate_force(ai, force, force_to_add)) { return force; } } @@ -50,16 +50,23 @@ vec2 AISystem::calculate(AI & ai) { return force; } -bool AISystem::accumulate_force(vec2 & running_total, vec2 force_to_add) { - double magnitude_remaining = running_total.length(); - double magnitude_to_add = force_to_add.length(); +bool AISystem::accumulate_force(AI & ai, vec2 & running_total, vec2 force_to_add) { + float magnitude = running_total.length(); + float magnitude_remaining = ai.max_force - magnitude; - if (magnitude_remaining + magnitude_to_add > 0) { + if (magnitude_remaining <= 0.0f) { + return false; + } + + float magnitude_to_add = force_to_add.length(); + if (magnitude_to_add < magnitude_remaining) { running_total += force_to_add; - return true; + } else { + force_to_add.normalize(); + running_total += force_to_add * magnitude_remaining; } - return false; + return true; } vec2 AISystem::seek(const AI & ai) { diff --git a/src/crepe/system/AISystem.h b/src/crepe/system/AISystem.h index 5e94ccb..18f1c61 100644 --- a/src/crepe/system/AISystem.h +++ b/src/crepe/system/AISystem.h @@ -15,9 +15,12 @@ public: private: vec2 calculate(AI & ai); - bool accumulate_force(vec2 & running_total, vec2 force_to_add); + bool accumulate_force(AI & ai, vec2 & running_total, vec2 force_to_add); vec2 seek(const AI & ai); + vec2 flee(const AI & ai); + vec2 arrive(const AI & ai); + vec2 path_follow(const AI & ai); }; } // namespace crepe |