Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
Task_Flee.cpp
Go to the documentation of this file.
1/**
2 * @file Task_Flee.cpp
3 * @brief Atomic task: compute flee direction away from a threat.
4 * @author Olympe Engine
5 * @date 2026-03-08
6 *
7 * C++14 compliant - no C++17/20 features.
8 */
9
10#include "Task_Flee.h"
11#include "../../AtomicTaskRegistry.h"
12#include "../../LocalBlackboard.h"
13#include "../../../system/system_utils.h"
14
15#include <cmath>
16
17namespace Olympe {
18
19static const float DEFAULT_FLEE_DISTANCE = 200.0f;
20
22
24{
25 SYSTEM_LOG << "[Task_Flee] Abort()\n";
26}
27
28TaskStatus Task_Flee::Execute(const ParameterMap& /*params*/)
29{
30 return TaskStatus::Failure; // requires context; use ExecuteWithContext
31}
32
33TaskStatus Task_Flee::ExecuteWithContext(const AtomicTaskContext& ctx,
34 const ParameterMap& params)
35{
36 if (!ctx.LocalBB)
37 {
38 SYSTEM_LOG << "[Task_Flee] No LocalBlackboard in context\n";
40 }
41
42 // --- Resolve Distance parameter (optional) ---
44 {
45 auto it = params.find("Distance");
46 if (it != params.end() && it->second.GetType() == VariableType::Float)
47 {
48 distance = it->second.AsFloat();
50 }
51 }
52
53 // --- Resolve threat position ---
54 ::Vector threatPos(0.0f, 0.0f, 0.0f);
55 {
56 auto it = params.find("Target");
57 if (it != params.end() && it->second.GetType() == VariableType::Vector)
58 {
59 threatPos = it->second.AsVector();
60 }
61 // If no explicit Target parameter, use the entity's own position as a
62 // safe fallback (results in a zero flee vector, which is benign).
63 }
64
65 // --- Read entity's current position ---
66 ::Vector entityPos(0.0f, 0.0f, 0.0f);
67 {
68 const TaskValue posVal = ctx.LocalBB->GetValueScoped("local:Position");
69 if (posVal.GetType() == VariableType::Vector)
70 {
71 entityPos = posVal.AsVector();
72 }
73 }
74
75 // --- Compute flee direction (opposite of threat direction) ---
77 const float norm = delta.Norm();
78
80 if (norm > 1e-5f)
81 {
82 ::Vector dir = delta * (1.0f / norm);
84 }
85 else
86 {
87 // Entity is on top of the threat: flee along +X as a safe default.
88 fleeTarget = entityPos + ::Vector(distance, 0.0f, 0.0f);
89 }
90
91 SYSTEM_LOG << "[Task_Flee] Entity " << ctx.Entity
92 << " fleeing from (" << threatPos.x << "," << threatPos.y << ")"
93 << " to (" << fleeTarget.x << "," << fleeTarget.y << ")"
94 << " distance=" << distance << "\n";
95
96 ctx.LocalBB->SetValueScoped("local:FleeTarget", TaskValue(fleeTarget));
97
99}
100
101REGISTER_ATOMIC_TASK(Task_Flee, "Task_Flee")
102
103} // namespace Olympe
#define REGISTER_ATOMIC_TASK(ClassName, Id)
Registers a factory for ClassName under Id at static init time.
Atomic task that computes a flee direction away from a target.
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
void Abort() override
Aborts the task, releasing any in-progress state.
Definition Task_Flee.cpp:25
TaskStatus Execute(const ParameterMap &params) override
Executes the atomic task for one frame.
Definition Task_Flee.cpp:30
TaskStatus ExecuteWithContext(const AtomicTaskContext &ctx, const ParameterMap &params) override
Executes the atomic task for one frame with full runtime context.
Definition Task_Flee.cpp:35
float Norm() const
Definition vector.h:71
< Provides AssetID and INVALID_ASSET_ID
@ Float
Single-precision float.
@ Vector
3-component vector (Vector from vector.h)
TaskStatus
Result code returned by IAtomicTask::Execute().
Definition IAtomicTask.h:38
@ Success
Task completed successfully.
@ Failure
Task failed.
static const float DEFAULT_FLEE_DISTANCE
Definition Task_Flee.cpp:20
#define SYSTEM_LOG