Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
GameObject.cpp
Go to the documentation of this file.
1#include "GameObject.h"
2#include "ObjectFactory.h"
4#include <sstream>
5#include <iomanip>
7
8//for tests
9//#include <SDL3/SDL_render.h>
10#include "GameEngine.h"
11//extern SDL_Renderer* renderer;
12
13bool GameObject::FactoryRegistered = ObjectFactory::Get().Register("GameObject", Create); // []() { return new GameObject(); });
14
16{
17 return new GameObject();
18}
19//-------------------------------------------------------------
21{
22 // Default GameObject OnEvent - override in derived classes for specific handling
23}
24//-------------------------------------------------------------
25std::string GameObject::Save() const
26{
27 std::ostringstream ss;
28 ss << std::fixed << std::setprecision(6);
29 ss << "{\n";
30 ss << " \"uid\": " << GetUID() << ",\n";
31 ss << " \"name\": \"" << escape_json_string(name) << "\",\n";
32 ss << " \"className\": \"GameObject\",\n";
33 ss << " \"entityType\": " << static_cast<int>(GetEntityType()) << ",\n";
34 ss << " \"position\": { \"x\": " << position.x << ", \"y\": " << position.y << " },\n";
35 ss << " \"width\": " << width << ",\n";
36 ss << " \"height\": " << height << ",\n";
37 ss << " \"isDynamic\": " << (isDynamic ? "true" : "false") << "\n";
38 ss << "}";
39 return ss.str();
40}
41
42bool GameObject::Load(const std::string& json)
43{
44 std::string s;
45 if (extract_json_string(json, "name", s)) name = s;
46
47 double px=0, py=0;
48 // Optimized: search for position block once
49 size_t pos = json.find("\"position\"");
50 if (pos != std::string::npos)
51 {
52 size_t brace = json.find('{', pos);
53 if (brace != std::string::npos)
54 {
55 size_t endb = json.find('}', brace);
56 if (endb != std::string::npos)
57 {
58 // Use string_view-like approach to avoid substring allocation
59 std::string block = json.substr(brace, endb - brace + 1);
60 double x=0,y=0;
61 if (extract_json_double(block, "x", x)) position.x = static_cast<float>(x);
62 if (extract_json_double(block, "y", y)) position.y = static_cast<float>(y);
63 }
64 }
65 }
66
67 // width / height - parse both on same line for efficiency
68 double w=0,h=0;
69 if (extract_json_double(json, "width", w)) width = static_cast<float>(w);
70 if (extract_json_double(json, "height", h)) height = static_cast<float>(h);
71
72 // isDynamic
73 bool b=false;
74 if (extract_json_bool(json, "isDynamic", b)) isDynamic = b;
75
76 // entity type
77 int et=0;
78 if (extract_json_int(json, "entityType", et)) {
79 // best-effort mapping
80 if (et >= 0 && et < static_cast<int>(EntityType::Count)) {
81 // we ignore setting specific derived type - keep as base GameObject
82 }
83 }
84
85 return true;
86}
nlohmann::json json
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
Core game engine class.
float height
Definition GameObject.h:84
static bool FactoryRegistered
Definition GameObject.h:35
std::string Save() const
bool isDynamic
Definition GameObject.h:88
Vector position
Definition GameObject.h:81
bool Load(const std::string &json)
virtual void OnEvent(const Message &msg) override
static Object * Create()
virtual EntityType GetEntityType() const
Definition GameObject.h:43
float width
Definition GameObject.h:83
bool Register(const std::string &className, CreatorFunction creator)
Enregistre une fonction de cr�ation pour un nom de classe donn�.
static ObjectFactory & Get()
std::string name
Definition Object.h:45
uint64_t GetUID() const
Definition Object.h:52
float y
Definition vector.h:27
float x
Definition vector.h:27
static std::string escape_json_string(const std::string &s)
static bool extract_json_double(const std::string &json, const std::string &key, double &out)
static bool extract_json_bool(const std::string &json, const std::string &key, bool &out)
static bool extract_json_int(const std::string &json, const std::string &key, int &out)
static bool extract_json_string(const std::string &json, const std::string &key, std::string &out)