aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authormax-001 <maxsmits21@kpnmail.nl>2024-12-04 13:46:41 +0100
committermax-001 <maxsmits21@kpnmail.nl>2024-12-04 13:46:41 +0100
commitf9f5600b60d6944dc9a7dd502988703d59d0cd62 (patch)
tree4f6d7cfe7e15bf0fd3b838769cb2a093de487ba6 /src
parentac87bfad20e1bcf1fd066a4eda231608fe12f504 (diff)
Setup some behaviors
Diffstat (limited to 'src')
-rw-r--r--src/crepe/api/AI.cpp2
-rw-r--r--src/crepe/api/AI.h47
-rw-r--r--src/crepe/system/AISystem.cpp32
-rw-r--r--src/crepe/system/AISystem.h6
4 files changed, 80 insertions, 7 deletions
diff --git a/src/crepe/api/AI.cpp b/src/crepe/api/AI.cpp
index 6b63216..7f820a8 100644
--- a/src/crepe/api/AI.cpp
+++ b/src/crepe/api/AI.cpp
@@ -2,7 +2,7 @@
namespace crepe {
-AI::AI(game_object_id_t id, double mass, double max_speed, double max_force)
+AI::AI(game_object_id_t id, float mass, float max_speed, float max_force)
: Component(id),
mass(mass),
max_speed(max_speed),
diff --git a/src/crepe/api/AI.h b/src/crepe/api/AI.h
index b755439..faeeba5 100644
--- a/src/crepe/api/AI.h
+++ b/src/crepe/api/AI.h
@@ -7,12 +7,51 @@ namespace crepe {
class AI : public Component {
public:
- AI(game_object_id_t id, double mass, double max_speed, double max_force);
+ enum BehaviorType {
+ NONE = 0x00000,
+ SEEK = 0x00002,
+ FLEE = 0x00004,
+ ARRIVE = 0x00008,
+ PATH_FOLLOW = 0x00010,
+ };
public:
- double mass;
- double max_speed;
- double max_force;
+ AI(game_object_id_t id, float mass, float max_speed, float max_force);
+
+ bool on(BehaviorType behavior) const { return (flags & behavior) == behavior; }
+ void seek_on() { flags |= SEEK; }
+ void seek_off() {
+ if (on(SEEK)) flags ^= SEEK;
+ }
+ void flee_on() { flags |= FLEE; }
+ void flee_off() {
+ if (on(FLEE)) flags ^= FLEE;
+ }
+ void arrive_on() { flags |= ARRIVE; }
+ void arrive_off() {
+ if (on(ARRIVE)) flags ^= ARRIVE;
+ }
+ void path_follow_on() { flags |= PATH_FOLLOW; }
+ void path_follow_off() {
+ if (on(PATH_FOLLOW)) flags ^= PATH_FOLLOW;
+ }
+
+public:
+ float mass;
+ float max_speed;
+ float max_force;
+
+ // The target to seek or arrive at
+ vec2 seek_target;
+ // The target to flee from
+ vec2 flee_target;
+ // The distance at which the entity will start to flee from the target
+ float square_flee_panic_distance = 100.0f * 100.0f;
+ // The deceleration rate for the arrive behavior (higher values will make the entity decelerate faster (less overshoot))
+ float arrive_deceleration = 2.0f;
+
+private:
+ int flags = 0;
};
} // namespace crepe
diff --git a/src/crepe/system/AISystem.cpp b/src/crepe/system/AISystem.cpp
index 012f4fa..12da3d9 100644
--- a/src/crepe/system/AISystem.cpp
+++ b/src/crepe/system/AISystem.cpp
@@ -1,6 +1,34 @@
+#include "../ComponentManager.h"
+
#include "AISystem.h"
-#include <iostream>
using namespace crepe;
-void AISystem::update() { std::cout << "AI System update" << std::endl; }
+void AISystem::update() {
+ ComponentManager & mgr = this->component_manager;
+ RefVector<AI> ai_components = mgr.get_components_by_type<AI>();
+
+ for (AI & ai : ai_components) {
+ vec2 force = this->calculate(ai);
+ //...
+ }
+}
+
+vec2 AISystem::calculate(AI & ai) {
+ vec2 force;
+
+ if (ai.on(AI::BehaviorType::SEEK)) {
+ // Seek the target
+ }
+ if (ai.on(AI::BehaviorType::FLEE)) {
+ // Flee from the target
+ }
+ if (ai.on(AI::BehaviorType::ARRIVE)) {
+ // Arrive at the target
+ }
+ if (ai.on(AI::BehaviorType::PATH_FOLLOW)) {
+ // Follow the path
+ }
+
+ return force;
+}
diff --git a/src/crepe/system/AISystem.h b/src/crepe/system/AISystem.h
index 4138e01..eb8d08c 100644
--- a/src/crepe/system/AISystem.h
+++ b/src/crepe/system/AISystem.h
@@ -1,6 +1,9 @@
#pragma once
+#include "api/AI.h"
+
#include "System.h"
+#include "types.h"
namespace crepe {
@@ -9,6 +12,9 @@ public:
using System::System;
void update() override;
+
+private:
+ vec2 calculate(AI & ai);
};
} // namespace crepe