Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
MathOpOperand.cpp
Go to the documentation of this file.
1/**
2 * @file MathOpOperand.cpp
3 * @brief Implementation of MathOpOperand serialization and helpers.
4 */
5
6#include "MathOpOperand.h"
7
8namespace Olympe {
9
10// ============================================================================
11// MathOpOperand
12// ============================================================================
13
15{
17
18 // Mode (0 = Variable, 1 = Const, 2 = Pin)
19 int modeValue = static_cast<int>(mode);
20 j["mode"] = modeValue;
21
22 // Variable / Const data
23 j["variableName"] = variableName;
24 j["constValue"] = constValue;
25
26 // Pin mode data
27 j["dynamicPinID"] = dynamicPinID;
28
29 return j;
30}
31
33{
35
36 if (data.contains("mode")) {
37 int modeValue = data["mode"].get<int>();
38 operand.mode = static_cast<Mode>(modeValue);
39 }
40
41 if (data.contains("variableName")) {
42 operand.variableName = data["variableName"].get<std::string>();
43 }
44
45 if (data.contains("constValue")) {
46 operand.constValue = data["constValue"].get<std::string>();
47 }
48
49 if (data.contains("dynamicPinID")) {
50 operand.dynamicPinID = data["dynamicPinID"].get<std::string>();
51 }
52
53 return operand;
54}
55
56// ============================================================================
57// MathOpRef
58// ============================================================================
59
60std::string MathOpRef::GetDisplayString() const
61{
62 std::string result = "[";
63
64 // Left operand
65 switch (leftOperand.mode) {
67 result += leftOperand.variableName;
68 break;
70 result += leftOperand.constValue;
71 break;
73 result += "Pin:" + leftOperand.dynamicPinID;
74 break;
75 }
76
77 result += "] " + mathOperator + " [";
78
79 // Right operand
80 switch (rightOperand.mode) {
82 result += rightOperand.variableName;
83 break;
85 result += rightOperand.constValue;
86 break;
88 result += "Pin:" + rightOperand.dynamicPinID;
89 break;
90 }
91
92 result += "]";
93
94 return result;
95}
96
98{
100
101 j["leftOperand"] = leftOperand.ToJson();
102 j["mathOperator"] = mathOperator;
103 j["rightOperand"] = rightOperand.ToJson();
104
105 return j;
106}
107
109{
111
112 if (data.contains("leftOperand")) {
113 ref.leftOperand = MathOpOperand::FromJson(data["leftOperand"]);
114 }
115
116 if (data.contains("mathOperator")) {
117 ref.mathOperator = data["mathOperator"].get<std::string>();
118 }
119
120 if (data.contains("rightOperand")) {
121 ref.rightOperand = MathOpOperand::FromJson(data["rightOperand"]);
122 }
123
124 return ref;
125}
126
127} // namespace Olympe
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
Defines MathOpOperand — operand references for MathOp nodes.
< Provides AssetID and INVALID_ASSET_ID
nlohmann::json json
Represents one arithmetic operand (left A, right B) in a MathOp node.
Mode
Discriminates the data source of this operand.
@ Variable
References a blackboard variable by name.
@ Const
Literal constant value.
@ Pin
External data-input pin on the owning node.
std::string variableName
Blackboard key (mode == Variable), e.g. "mMoveSpeed".
std::string dynamicPinID
UUID of the DynamicDataPin that supplies this operand's value.
std::string constValue
Literal string (mode == Const), e.g. "5.0".
Mode mode
Active mode.
nlohmann::json ToJson() const
Serializes this MathOpOperand to a JSON object.
static MathOpOperand FromJson(const nlohmann::json &data)
Deserializes a MathOpOperand from a JSON object.
Complete reference for a MathOp node: left operand, operator, right operand.
static MathOpRef FromJson(const nlohmann::json &data)
Deserializes a MathOpRef from a JSON object.
MathOpOperand leftOperand
Left-hand side operand (A)
std::string mathOperator
Arithmetic operator: "+", "-", "*", "/", "%", "^".
nlohmann::json ToJson() const
Serializes this MathOpRef to a JSON object.
std::string GetDisplayString() const
Returns a human-readable display string for the operation.
MathOpOperand rightOperand
Right-hand side operand (B)