Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
MathOpOperand.h
Go to the documentation of this file.
1/**
2 * @file MathOpOperand.h
3 * @brief Defines MathOpOperand — operand references for MathOp nodes.
4 * @author Olympe Engine
5 * @date 2026-03-18
6 *
7 * @details
8 * MathOpOperand is a self-contained representation of one arithmetic operand
9 * for a MathOp node. It mirrors the OperandRef/ConditionRef pattern, supporting
10 * three modes: Variable (blackboard reference), Const (literal value), or
11 * Pin (data input pin).
12 *
13 * A MathOp has three operands: left (A), operator, right (B).
14 * Each operand independently supports Variable/Const/Pin modes with optional
15 * dynamic pin generation for Pin-mode operands.
16 *
17 * C++14 compliant.
18 */
19
20#pragma once
21
22#include <string>
23#include "../third_party/nlohmann/json.hpp"
24
25namespace Olympe {
26
27/**
28 * @struct MathOpOperand
29 * @brief Represents one arithmetic operand (left A, right B) in a MathOp node.
30 *
31 * @details
32 * Similar to OperandRef from ConditionRef.h, but adapted for arithmetic operations.
33 * If mode is `Pin`, `dynamicPinID` stores the UUID of the DynamicDataPin.
34 * If mode is `Variable` or `Const`, no pin is generated and `dynamicPinID` is empty.
35 */
37{
38 /** @brief Discriminates the data source of this operand. */
39 enum class Mode
40 {
41 Variable, ///< References a blackboard variable by name
42 Const, ///< Literal constant value
43 Pin ///< External data-input pin on the owning node
44 };
45
46 Mode mode = Mode::Const; ///< Active mode
47
48 // Variable / Const modes
49 std::string variableName; ///< Blackboard key (mode == Variable), e.g. "mMoveSpeed"
50 std::string constValue; ///< Literal string (mode == Const), e.g. "5.0"
51
52 // Pin mode only
53 /**
54 * @brief UUID of the DynamicDataPin that supplies this operand's value.
55 *
56 * Populated by DynamicDataPinManager when the parent MathOpOperand
57 * is registered with a Pin-mode operand.
58 * Format: "pin_inst_<random>", e.g. "pin_inst_abc123def456".
59 * Empty string means no pin has been assigned yet.
60 */
61 std::string dynamicPinID;
62
63 // -----------------------------------------------------------------------
64 // Serialization
65 // -----------------------------------------------------------------------
66
67 /// @brief Serializes this MathOpOperand to a JSON object.
68 nlohmann::json ToJson() const;
69
70 /// @brief Deserializes a MathOpOperand from a JSON object.
71 static MathOpOperand FromJson(const nlohmann::json& data);
72};
73
74/**
75 * @struct MathOpRef
76 * @brief Complete reference for a MathOp node: left operand, operator, right operand.
77 *
78 * @details
79 * Stores the full operand configuration for one MathOp node.
80 * The result is computed as: leftOperand [operator] rightOperand.
81 * Operators supported: "+", "-", "*", "/", "%", "^" (power).
82 *
83 * Operands whose mode is Pin automatically have DynamicDataPin objects
84 * generated for them by DynamicDataPinManager.
85 */
87{
88 // Operands
89 MathOpOperand leftOperand; ///< Left-hand side operand (A)
90 std::string mathOperator = "+"; ///< Arithmetic operator: "+", "-", "*", "/", "%", "^"
91 MathOpOperand rightOperand; ///< Right-hand side operand (B)
92
93 // -----------------------------------------------------------------------
94 // Helpers
95 // -----------------------------------------------------------------------
96
97 /// @brief Returns a human-readable display string for the operation.
98 /// Example: "[mSpeed] + [5.0]" or "[Pin:0] * [mFactor]"
99 std::string GetDisplayString() const;
100
101 // -----------------------------------------------------------------------
102 // Serialization
103 // -----------------------------------------------------------------------
104
105 /// @brief Serializes this MathOpRef to a JSON object.
106 nlohmann::json ToJson() const;
107
108 /// @brief Deserializes a MathOpRef from a JSON object.
109 static MathOpRef FromJson(const nlohmann::json& data);
110};
111
112} // namespace Olympe
< 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)