Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
Task_ChangeState.cpp
Go to the documentation of this file.
1/**
2 * @file Task_ChangeState.cpp
3 * @brief Atomic AI task: change the entity's current state in the LocalBlackboard.
4 * @author Olympe Engine
5 * @date 2026-03-08
6 *
7 * C++14 compliant - no C++17/20 features.
8 */
9
10#include "Task_ChangeState.h"
11#include "../../AtomicTaskRegistry.h"
12#include "../../LocalBlackboard.h"
13#include "../../../system/system_utils.h"
14
15namespace Olympe {
16
17static const char* BB_KEY_CURRENT_STATE = "local:current_state";
18
20
22{
23 SYSTEM_LOG << "[Task_ChangeState(AI)] Abort()\n";
24}
25
27{
28 return TaskStatus::Failure; // requires context; use ExecuteWithContext
29}
30
32 const ParameterMap& params)
33{
34 if (!ctx.LocalBB)
35 {
36 SYSTEM_LOG << "[Task_ChangeState(AI)] No LocalBlackboard in context\n";
38 }
39
40 // --- Resolve StateName parameter ---
41 std::string stateName;
42 {
43 auto it = params.find("StateName");
44 if (it == params.end() || it->second.GetType() != VariableType::String)
45 {
46 SYSTEM_LOG << "[Task_ChangeState(AI)] Missing or invalid 'StateName' parameter\n";
48 }
49 stateName = it->second.AsString();
50 }
51
52 // Log the state transition.
53 const TaskValue currentVal = ctx.LocalBB->GetValueScoped(BB_KEY_CURRENT_STATE);
54 if (currentVal.GetType() == VariableType::String)
55 {
56 SYSTEM_LOG << "[Task_ChangeState(AI)] Entity " << ctx.Entity
57 << " state: '" << currentVal.AsString()
58 << "' -> '" << stateName << "'\n";
59 }
60 else
61 {
62 SYSTEM_LOG << "[Task_ChangeState(AI)] Entity " << ctx.Entity
63 << " setting state -> '" << stateName << "'\n";
64 }
65
66 ctx.LocalBB->SetValueScoped(BB_KEY_CURRENT_STATE, TaskValue(stateName));
67
69}
70
71REGISTER_ATOMIC_TASK(Task_ChangeState, "Task_ChangeState")
72
73} // namespace Olympe
#define REGISTER_ATOMIC_TASK(ClassName, Id)
Registers a factory for ClassName under Id at static init time.
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
Atomic task that changes the AI state stored in the LocalBlackboard.
std::unordered_map< std::string, TaskValue > ParameterMap
Convenience alias for the parameter map passed to Execute().
Definition IAtomicTask.h:67
C++14-compliant type-safe value container for task parameters.
std::string AsString() const
Returns the string value.
Writes a new AI state name to "local:current_state" in the LocalBlackboard.
TaskStatus Execute(const ParameterMap &params) override
Executes the atomic task for one frame.
TaskStatus ExecuteWithContext(const AtomicTaskContext &ctx, const ParameterMap &params) override
Executes the atomic task for one frame with full runtime context.
void Abort() override
Aborts the task, releasing any in-progress state.
< Provides AssetID and INVALID_ASSET_ID
@ String
std::string
TaskStatus
Result code returned by IAtomicTask::Execute().
Definition IAtomicTask.h:38
@ Success
Task completed successfully.
@ Failure
Task failed.
static const char * BB_KEY_CURRENT_STATE
Lightweight context bundle passed to IAtomicTask::ExecuteWithContext().
#define SYSTEM_LOG