Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
EntityBlueprint.cpp
Go to the documentation of this file.
1/*
2 * Olympe Blueprint Editor - Entity Blueprint Implementation
3 */
4
5#include "EntityBlueprint.h"
6#include "../json_helper.h"
7#include <algorithm>
8
9namespace Olympe {
10namespace Blueprint {
11
12// EntityBlueprint methods
13
14void EntityBlueprint::AddComponent(const std::string& type, const json& properties)
15{
16 // Check if component already exists, if so, update it
17 for (auto& comp : components)
18 {
19 if (comp.type == type)
20 {
21 comp.properties = properties;
22 return;
23 }
24 }
25
26 // Add new component
27 components.push_back(ComponentData(type, properties));
28}
29
31{
32 for (auto& comp : components)
33 {
34 if (comp.type == type)
35 return &comp;
36 }
37 return nullptr;
38}
39
40const ComponentData* EntityBlueprint::GetComponent(const std::string& type) const
41{
42 for (const auto& comp : components)
43 {
44 if (comp.type == type)
45 return &comp;
46 }
47 return nullptr;
48}
49
50bool EntityBlueprint::HasComponent(const std::string& type) const
51{
52 return GetComponent(type) != nullptr;
53}
54
55bool EntityBlueprint::RemoveComponent(const std::string& type)
56{
57 auto it = std::remove_if(components.begin(), components.end(),
58 [&type](const ComponentData& comp) { return comp.type == type; });
59
60 if (it != components.end())
61 {
62 components.erase(it, components.end());
63 return true;
64 }
65 return false;
66}
67
69{
70 json j;
71 j["schema_version"] = 1;
72 j["type"] = "EntityBlueprint";
73 j["name"] = name;
74 j["description"] = description;
75
76 j["components"] = json::array();
77 for (const auto& comp : components)
78 {
80 compJson["type"] = comp.type;
81 compJson["properties"] = comp.properties;
82 j["components"].push_back(compJson);
83 }
84
85 return j;
86}
87
89{
91
93 blueprint.description = JsonHelper::GetString(j, "description", "");
94
95 if (JsonHelper::IsArray(j, "components"))
96 {
97 JsonHelper::ForEachInArray(j, "components", [&blueprint](const json& compJson, size_t i)
98 {
101
102 if (compJson.contains("properties"))
103 comp.properties = compJson["properties"];
104
105 blueprint.components.push_back(comp);
106 });
107 }
108
109 return blueprint;
110}
111
112bool EntityBlueprint::SaveToFile(const std::string& filepath) const
113{
114 return JsonHelper::SaveJsonToFile(filepath, ToJson(), 4);
115}
116
118{
119 json j;
120 if (!JsonHelper::LoadJsonFromFile(filepath, j))
121 {
122 return EntityBlueprint();
123 }
124
125 return FromJson(j);
126}
127
128// Helper functions for creating component data
129
131{
132 json props = json::object();
133 json position = json::object();
134 position["x"] = x;
135 position["y"] = y;
136 position["z"] = 0.0f;
137 props["position"] = position;
138 return ComponentData("Position", props);
139}
140
141ComponentData CreateBoundingBoxComponent(float x, float y, float width, float height)
142{
143 json props = json::object();
144 json boundingBox = json::object();
145 boundingBox["x"] = x;
146 boundingBox["y"] = y;
147 boundingBox["w"] = width;
148 boundingBox["h"] = height;
149 props["boundingBox"] = boundingBox;
150 return ComponentData("BoundingBox", props);
151}
152
154 const std::string& spritePath,
155 float srcX, float srcY, float srcWidth, float srcHeight,
156 float hotSpotX, float hotSpotY)
157{
158 json props = json::object();
159 props["spritePath"] = spritePath;
160
161 json srcRect = json::object();
162 srcRect["x"] = srcX;
163 srcRect["y"] = srcY;
164 srcRect["w"] = srcWidth;
165 srcRect["h"] = srcHeight;
166 props["srcRect"] = srcRect;
167
168 json hotSpot = json::object();
169 hotSpot["x"] = hotSpotX;
170 hotSpot["y"] = hotSpotY;
171 props["hotSpot"] = hotSpot;
172
173 return ComponentData("VisualSprite", props);
174}
175
177{
178 json props = json::object();
179
180 json direction = json::object();
181 direction["x"] = dirX;
182 direction["y"] = dirY;
183 props["direction"] = direction;
184
185 json velocity = json::object();
186 velocity["x"] = velX;
187 velocity["y"] = velY;
188 props["velocity"] = velocity;
189
190 return ComponentData("Movement", props);
191}
192
194{
195 json props = json::object();
196 props["mass"] = mass;
197 props["speed"] = speed;
198 return ComponentData("PhysicsBody", props);
199}
200
202{
203 json props = json::object();
204 props["currentHealth"] = current;
205 props["maxHealth"] = max;
206 return ComponentData("Health", props);
207}
208
209ComponentData CreateAIBehaviorComponent(const std::string& behaviorType)
210{
211 json props = json::object();
212 props["behaviorType"] = behaviorType;
213 return ComponentData("AIBehavior", props);
214}
215
216} // namespace Blueprint
217} // namespace Olympe
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
std::string GetString(const json &j, const std::string &key, const std::string &defaultValue="")
Safely get a string value from JSON.
bool LoadJsonFromFile(const std::string &filepath, json &j)
Load and parse a JSON file.
Definition json_helper.h:42
void ForEachInArray(const json &j, const std::string &key, std::function< void(const json &, size_t)> callback)
Iterate over an array with a callback function.
bool SaveJsonToFile(const std::string &filepath, const json &j, int indent=4)
Save a JSON object to a file with formatting.
Definition json_helper.h:73
bool IsArray(const json &j, const std::string &key)
Check if a key contains an array.
ComponentData CreateVisualSpriteComponent(const std::string &spritePath, float srcX, float srcY, float srcWidth, float srcHeight, float hotSpotX, float hotSpotY)
ComponentData CreateBoundingBoxComponent(float x, float y, float width, float height)
ComponentData CreateMovementComponent(float dirX, float dirY, float velX, float velY)
ComponentData CreateAIBehaviorComponent(const std::string &behaviorType)
nlohmann::json json
ComponentData CreatePositionComponent(float x, float y)
ComponentData CreatePhysicsBodyComponent(float mass, float speed)
ComponentData CreateHealthComponent(int current, int max)
< Provides AssetID and INVALID_ASSET_ID
@ Blueprint
.ats files (SubGraph/VisualScript)
void AddComponent(const std::string &type, const json &properties)
static EntityBlueprint LoadFromFile(const std::string &filepath)
std::vector< ComponentData > components
bool SaveToFile(const std::string &filepath) const
static EntityBlueprint FromJson(const json &j)
bool RemoveComponent(const std::string &type)
ComponentData * GetComponent(const std::string &type)
bool HasComponent(const std::string &type) const