aboutsummaryrefslogtreecommitdiff
path: root/src/crepe
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe')
-rw-r--r--src/crepe/api/AI.h14
-rw-r--r--src/crepe/system/AISystem.cpp34
-rw-r--r--src/crepe/system/AISystem.h2
3 files changed, 47 insertions, 3 deletions
diff --git a/src/crepe/api/AI.h b/src/crepe/api/AI.h
index d4bd9d3..35b8998 100644
--- a/src/crepe/api/AI.h
+++ b/src/crepe/api/AI.h
@@ -36,6 +36,8 @@ public:
if (on(PATH_FOLLOW)) flags ^= PATH_FOLLOW;
}
+ void add_path_node(vec2 node) { path.push_back(node); }
+
public:
float max_force;
@@ -47,9 +49,21 @@ public:
float square_flee_panic_distance = 200.0f * 200.0f;
// The deceleration rate for the arrive behavior (higher values will make the entity decelerate faster (less overshoot))
float arrive_deceleration = 40.0f;
+ // The path to follow
+ std::vector<vec2> path;
+ // The distance from the path node at which the entity will move to the next node
+ float path_node_distance = 400.0f;
+ // Looping behavior for the path
+ bool path_loop = true;
private:
+ // The flags for the behaviors
int flags = 0;
+ // The current path index
+ size_t path_index = 0;
+
+ // The AISystem is the only class that can access the private members of AI
+ friend class AISystem;
};
} // namespace crepe
diff --git a/src/crepe/system/AISystem.cpp b/src/crepe/system/AISystem.cpp
index ce3147f..55dc14c 100644
--- a/src/crepe/system/AISystem.cpp
+++ b/src/crepe/system/AISystem.cpp
@@ -55,11 +55,11 @@ vec2 AISystem::calculate(AI & ai) {
}
}
if (ai.on(AI::BehaviorType::PATH_FOLLOW)) {
- /*vec2 force_to_add = this->path_follow(ai);
+ vec2 force_to_add = this->path_follow(ai);
if (!this->accumulate_force(ai, force, force_to_add)) {
return force;
- }*/
+ }
}
return force;
@@ -138,3 +138,33 @@ vec2 AISystem::arrive(const AI & ai) {
return vec2{0, 0};
}
+
+vec2 AISystem::path_follow(AI & ai) {
+ 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};
+ }
+
+ vec2 to_target = ai.path.at(ai.path_index) - transform.position;
+ if (to_target.length_squared() > ai.path_node_distance * ai.path_node_distance) {
+ ai.seek_target = ai.path.at(ai.path_index);
+ } else {
+ ai.path_index++;
+ if (ai.path_index >= ai.path.size()) {
+ if (ai.path_loop) {
+ ai.path_index = 0;
+ } else {
+ ai.path_index = ai.path.size() - 1;
+ return this->arrive(ai);
+ }
+ }
+ }
+
+ return this->seek(ai);
+}
diff --git a/src/crepe/system/AISystem.h b/src/crepe/system/AISystem.h
index 18f1c61..27861d9 100644
--- a/src/crepe/system/AISystem.h
+++ b/src/crepe/system/AISystem.h
@@ -20,7 +20,7 @@ private:
vec2 seek(const AI & ai);
vec2 flee(const AI & ai);
vec2 arrive(const AI & ai);
- vec2 path_follow(const AI & ai);
+ vec2 path_follow(AI & ai);
};
} // namespace crepe