aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormax-001 <maxsmits21@kpnmail.nl>2024-12-04 17:27:51 +0100
committermax-001 <maxsmits21@kpnmail.nl>2024-12-04 17:27:51 +0100
commit121387ba92a23d6f17b36331d25757abc899f7d2 (patch)
tree7d335c9b0ae3449733ffe1b05ceda8a26a6706af
parente303bd4c54b0edbcd1819a47a5d8aaef88211a09 (diff)
Implemeted seek behavior
-rw-r--r--src/crepe/api/AI.h3
-rw-r--r--src/crepe/system/AISystem.cpp45
-rw-r--r--src/crepe/system/AISystem.h3
-rw-r--r--src/example/AITest.cpp4
4 files changed, 51 insertions, 4 deletions
diff --git a/src/crepe/api/AI.h b/src/crepe/api/AI.h
index faeeba5..242ff89 100644
--- a/src/crepe/api/AI.h
+++ b/src/crepe/api/AI.h
@@ -51,6 +51,9 @@ public:
float arrive_deceleration = 2.0f;
private:
+ vec2 velocity;
+ friend class AISystem;
+
int flags = 0;
};
diff --git a/src/crepe/system/AISystem.cpp b/src/crepe/system/AISystem.cpp
index 12da3d9..9029f32 100644
--- a/src/crepe/system/AISystem.cpp
+++ b/src/crepe/system/AISystem.cpp
@@ -1,4 +1,7 @@
#include "../ComponentManager.h"
+#include "api/LoopTimer.h"
+#include "api/Transform.h"
+#include "types.h"
#include "AISystem.h"
@@ -8,9 +11,19 @@ void AISystem::update() {
ComponentManager & mgr = this->component_manager;
RefVector<AI> ai_components = mgr.get_components_by_type<AI>();
+ double dt = LoopTimer::get_instance().get_delta_time();
+
for (AI & ai : ai_components) {
vec2 force = this->calculate(ai);
- //...
+ vec2 acceleration = force / ai.mass;
+ ai.velocity += acceleration * dt;
+ ai.velocity.truncate(ai.max_speed);
+
+ // Update the position
+ RefVector<Transform> transforms
+ = mgr.get_components_by_id<Transform>(ai.game_object_id);
+ Transform & transform = transforms.front().get();
+ transform.position += ai.velocity * dt;
}
}
@@ -18,7 +31,11 @@ vec2 AISystem::calculate(AI & ai) {
vec2 force;
if (ai.on(AI::BehaviorType::SEEK)) {
- // Seek the target
+ vec2 force_to_add = this->seek(ai);
+
+ if (!this->accumulate_force(force, force_to_add)) {
+ return force;
+ }
}
if (ai.on(AI::BehaviorType::FLEE)) {
// Flee from the target
@@ -32,3 +49,27 @@ 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();
+
+ if (magnitude_remaining + magnitude_to_add > 0) {
+ running_total += force_to_add;
+ return true;
+ }
+
+ return false;
+}
+
+vec2 AISystem::seek(const AI & ai) {
+ ComponentManager & mgr = this->component_manager;
+ RefVector<Transform> transforms = mgr.get_components_by_id<Transform>(ai.game_object_id);
+ Transform & transform = transforms.front().get();
+
+ vec2 desired_velocity = ai.seek_target - transform.position;
+ desired_velocity.normalize();
+ desired_velocity *= ai.max_speed;
+
+ return desired_velocity - ai.velocity;
+}
diff --git a/src/crepe/system/AISystem.h b/src/crepe/system/AISystem.h
index eb8d08c..5e94ccb 100644
--- a/src/crepe/system/AISystem.h
+++ b/src/crepe/system/AISystem.h
@@ -15,6 +15,9 @@ public:
private:
vec2 calculate(AI & ai);
+ bool accumulate_force(vec2 & running_total, vec2 force_to_add);
+
+ vec2 seek(const AI & ai);
};
} // namespace crepe
diff --git a/src/example/AITest.cpp b/src/example/AITest.cpp
index 1c4633f..341e1de 100644
--- a/src/example/AITest.cpp
+++ b/src/example/AITest.cpp
@@ -18,13 +18,13 @@ public:
void load_scene() override {
ComponentManager & mgr = this->component_manager;
- GameObject game_object1 = mgr.new_object("", "", vec2{0, 0}, 0, 1);
+ GameObject game_object1 = mgr.new_object("", "", vec2{250, 250}, 0, 1);
GameObject game_object2 = mgr.new_object("", "", vec2{0, 0}, 0, 1);
Texture img = Texture("asset/texture/test_ap43.png");
game_object1.add_component<Sprite>(img, Color::MAGENTA,
Sprite::FlipSettings{false, false}, 1, 1, 195);
- game_object1.add_component<AI>(1, 1, 1);
+ game_object1.add_component<AI>(1, 200, 200).seek_on();
game_object2.add_component<Camera>(Color::WHITE, ivec2{1080, 720}, vec2{1036, 780},
1.0f);