Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
ConditionRef.cpp
Go to the documentation of this file.
1/**
2 * @file ConditionRef.cpp
3 * @brief Implementation of OperandRef and ConditionRef serialization helpers (Phase 24 Milestone 2).
4 * @author Olympe Engine
5 * @date 2026-03-18
6 *
7 * C++14 compliant — no std::optional, structured bindings, std::filesystem.
8 */
9
10#include "ConditionRef.h"
11
12#include <string>
13
14namespace Olympe {
15
16// ============================================================================
17// OperandRef serialization
18// ============================================================================
19
21{
22 nlohmann::json j = nlohmann::json::object();
23
24 std::string modeStr;
25 switch (mode)
26 {
27 case Mode::Variable: modeStr = "Variable"; break;
28 case Mode::Const: modeStr = "Const"; break;
29 case Mode::Pin: modeStr = "Pin"; break;
30 default: modeStr = "Const"; break;
31 }
32 j["mode"] = modeStr;
33 j["variableName"] = variableName;
34 j["constValue"] = constValue;
35 j["dynamicPinID"] = dynamicPinID;
36 return j;
37}
38
39/*static*/
41{
43 if (!data.is_object())
44 return ref;
45
46 if (data.contains("mode") && data["mode"].is_string())
47 {
48 const std::string& modeStr = data["mode"].get<std::string>();
49 if (modeStr == "Variable")
51 else if (modeStr == "Pin")
52 ref.mode = Mode::Pin;
53 else
54 ref.mode = Mode::Const;
55 }
56
57 if (data.contains("variableName") && data["variableName"].is_string())
58 ref.variableName = data["variableName"].get<std::string>();
59
60 if (data.contains("constValue") && data["constValue"].is_string())
61 ref.constValue = data["constValue"].get<std::string>();
62
63 if (data.contains("dynamicPinID") && data["dynamicPinID"].is_string())
64 ref.dynamicPinID = data["dynamicPinID"].get<std::string>();
65
66 return ref;
67}
68
69// ============================================================================
70// ConditionRef serialization
71// ============================================================================
72
74{
75 nlohmann::json j = nlohmann::json::object();
76 j["conditionIndex"] = conditionIndex;
77 j["leftOperand"] = leftOperand.ToJson();
78 j["operator"] = operatorStr;
79 j["rightOperand"] = rightOperand.ToJson();
80
81 // Serialize compareType as a string for readability.
82 std::string typeStr;
83 switch (compareType)
84 {
85 case VariableType::Int: typeStr = "Int"; break;
86 case VariableType::Float: typeStr = "Float"; break;
87 case VariableType::Bool: typeStr = "Bool"; break;
88 case VariableType::String: typeStr = "String"; break;
89 default: typeStr = "Float"; break;
90 }
91 j["compareType"] = typeStr;
92 return j;
93}
94
95/*static*/
97{
99 if (!data.is_object())
100 return ref;
101
102 if (data.contains("conditionIndex") && data["conditionIndex"].is_number_integer())
103 ref.conditionIndex = data["conditionIndex"].get<int>();
104
105 if (data.contains("leftOperand") && data["leftOperand"].is_object())
106 ref.leftOperand = OperandRef::FromJson(data["leftOperand"]);
107
108 if (data.contains("operator") && data["operator"].is_string())
109 ref.operatorStr = data["operator"].get<std::string>();
110
111 if (data.contains("rightOperand") && data["rightOperand"].is_object())
112 ref.rightOperand = OperandRef::FromJson(data["rightOperand"]);
113
114 if (data.contains("compareType") && data["compareType"].is_string())
115 {
116 const std::string& typeStr = data["compareType"].get<std::string>();
117 if (typeStr == "Int")
118 ref.compareType = VariableType::Int;
119 else if (typeStr == "Bool")
120 ref.compareType = VariableType::Bool;
121 else if (typeStr == "String")
122 ref.compareType = VariableType::String;
123 else
124 ref.compareType = VariableType::Float;
125 }
126
127 return ref;
128}
129
130} // namespace Olympe
Defines OperandRef and ConditionRef — standalone operand-to-pin mapping structures.
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
< Provides AssetID and INVALID_ASSET_ID
@ Int
32-bit signed integer
@ Float
Single-precision float.
@ String
std::string
nlohmann::json json
Stores the complete reference for one condition including operand-to-DynamicDataPin mapping.
OperandRef rightOperand
Right-hand side of the comparison.
OperandRef leftOperand
Left-hand side of the comparison.
std::string operatorStr
Comparison operator: "==", "<=", ">=", "!=", "<", ">".
VariableType compareType
Expected value type of both operands.
int conditionIndex
Index in the Branch/While node's conditions[] array.
static ConditionRef FromJson(const nlohmann::json &data)
Deserializes a ConditionRef from a JSON object.
nlohmann::json ToJson() const
Serializes this ConditionRef to a JSON object.
References one operand of a condition (left or right).
static OperandRef FromJson(const nlohmann::json &data)
Deserializes an OperandRef from a JSON object.
@ Variable
References a blackboard variable by name.
@ Const
Literal constant value.
@ Pin
External data-input pin on the owning node.
std::string constValue
Literal string (mode == Const), e.g. "100.0".
std::string variableName
Blackboard key (mode == Variable), e.g. "mHealth".
nlohmann::json ToJson() const
Serializes this OperandRef to a JSON object.
Mode mode
Active mode.
std::string dynamicPinID
UUID of the DynamicDataPin that supplies this operand's value.