Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
ComponentDefinition.h
Go to the documentation of this file.
1/*
2Olympe Engine V2 - 2025
3Nicolas Chereau
4nchereau@gmail.com
5
6This file is part of Olympe Engine V2.
7
8ComponentDefinition purpose: Provides dynamic component parameter storage and JSON parsing
9for entity components. Supports type-safe parameter access and conversions.
10*/
11
12#pragma once
13
14#include <string>
15#include <map>
16#include <memory>
17#include <SDL3/SDL.h>
18#include "vector.h"
19#include "ECS_Entity.h"
20
21// Forward declaration
22namespace nlohmann {
23 class json;
24}
25
27{
28 enum class Type : uint8_t
29 {
30 Unknown = 0,
31 Bool,
32 Int,
33 Float,
34 String,
35 Vector2,
36 Vector3,
37 Color,
38 Array,
40 };
41
43
44 // Value storage
45 bool boolValue = false;
46 int intValue = 0;
47 float floatValue = 0.0f;
48 std::string stringValue;
50 SDL_Color colorValue = { 255, 255, 255, 255 };
52
53 // Array storage (uses nlohmann::json for C++14 compatibility)
54 // Initialized to null by default, set to json::array() when Type::Array is used
55 std::shared_ptr<nlohmann::json> arrayValue;
56
57 // Factory methods
58 static ComponentParameter FromBool(bool value);
59 static ComponentParameter FromInt(int value);
60 static ComponentParameter FromFloat(float value);
61 static ComponentParameter FromString(const std::string& value);
62 static ComponentParameter FromVector2(float x, float y);
63 static ComponentParameter FromVector3(float x, float y, float z);
67
68 // Type conversion helpers with fallback logic
69 bool AsBool() const;
70 int AsInt() const;
71 float AsFloat() const;
72 std::string AsString() const;
73 Vector AsVector() const;
74 SDL_Color AsColor() const;
75 EntityID AsEntityRef() const;
76 const nlohmann::json& AsArray() const;
77};
78
80{
81 std::string componentType;
82 std::map<std::string, ComponentParameter> parameters;
83
84 // JSON parsing with type detection heuristics
86
87 // Parameter access
88 bool HasParameter(const std::string& name) const;
89 const ComponentParameter* GetParameter(const std::string& name) const;
90};
91
92// Helper function to convert ParameterType enum to string
94
95// Schema-aware parameter parsing
97 const std::string& componentType,
98 const std::string& paramName,
100);
nlohmann::json json
std::string ParameterTypeToString(ComponentParameter::Type type)
ComponentParameter ParseParameterWithSchema(const std::string &componentType, const std::string &paramName, const nlohmann::json &jsonValue)
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
std::uint64_t EntityID
Definition ECS_Entity.h:21
const EntityID INVALID_ENTITY_ID
Definition ECS_Entity.h:23
nlohmann::json json
const ComponentParameter * GetParameter(const std::string &name) const
std::map< std::string, ComponentParameter > parameters
bool HasParameter(const std::string &name) const
static ComponentDefinition FromJSON(const nlohmann::json &jsonObj)
std::shared_ptr< nlohmann::json > arrayValue
EntityID AsEntityRef() const
static ComponentParameter FromBool(bool value)
static ComponentParameter FromFloat(float value)
static ComponentParameter FromVector2(float x, float y)
static ComponentParameter FromEntityRef(EntityID entityId)
static ComponentParameter FromVector3(float x, float y, float z)
static ComponentParameter FromColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
const nlohmann::json & AsArray() const
std::string AsString() const
static ComponentParameter FromInt(int value)
static ComponentParameter FromArray(const nlohmann::json &arrayData)
SDL_Color AsColor() const
static ComponentParameter FromString(const std::string &value)