GigaPasjans
Loading...
Searching...
No Matches
GameObject.h
1// Wykonane przez Piotra ChudziƄskiego w dniu 13.04.2025
2#pragma once
3
4#include <foundation/Common.h>
5#include <foundation/Random.h>
6#include <iostream>
7#include <nlohmann/json.hpp>
8#include <ranges>
9#include <utility>
10
11#include "StreamUtils.h"
12
13
14// stream.write(reinterpret_cast<const char *>(&m_Value), sizeof(type));
15// stream.read(reinterpret_cast<char *>(&m_Value), sizeof(type));
16#define INT_BASED_PARAMETER(name, type) \
17 class name : public IParameter<type> \
18 { \
19 public: \
20 name(const String &paramName, const type &defaultValue) : IParameter<type>(paramName, defaultValue) {} \
21 \
22 int32 Write(nlohmann::json &target) const override \
23 { \
24 target["value"] = m_Value; \
25 return sizeof(type); \
26 } \
27 \
28 int32 Read(nlohmann::json &target) override \
29 { \
30 m_Value = target["value"].get<type>(); \
31 return sizeof(type); \
32 } \
33 \
34 type IncrementAndGet() \
35 { \
36 SetValue(GetValue() + 1); \
37 return GetValue(); \
38 }; \
39 \
40 type GetAndIncrement() \
41 { \
42 type value = GetValue(); \
43 SetValue(GetValue() + 1); \
44 return value; \
45 }; \
46 \
47 type DecrementAndGet() \
48 { \
49 SetValue(GetValue() - 1); \
50 return GetValue(); \
51 }; \
52 \
53 type GetAndDecrement() \
54 { \
55 type value = GetValue(); \
56 SetValue(GetValue() - 1); \
57 return value; \
58 }; \
59 };
60
61
62namespace Phosphorus
63{
69 {
70 public:
71 virtual ~IBaseParameter() = default;
72
79 virtual int32 Write(nlohmann::json &target) const = 0;
80
87 virtual int32 Read(nlohmann::json &target) = 0;
88
89 virtual void SetDisplayName(const String &name) = 0;
90
94 [[nodiscard]] virtual String GetName() const = 0;
95
99 [[nodiscard]] virtual String GetDisplayName() const = 0;
100 };
101
107 template<typename T>
108 class IParameter : public IBaseParameter
109 {
110 protected:
111 T m_Value;
112 String m_Name;
113 String m_DisplayName;
114
115 public:
116 IParameter(const String &name, const T &defaultValue) : m_Value(defaultValue), m_Name(name), m_DisplayName(name)
117 {
118 }
119
120 ~IParameter() override = default;
121
127 void SetValue(const T &value) { m_Value = value; }
128
134 [[nodiscard]] T GetValue() const { return m_Value; }
135
136 void SetDisplayName(const String &name) override { m_DisplayName = name; }
137
143 [[nodiscard]] String GetName() const override { return m_Name; }
144
150 [[nodiscard]] String GetDisplayName() const override { return m_DisplayName; }
151 };
152
153 INT_BASED_PARAMETER(Int8Parameter, int8);
154 INT_BASED_PARAMETER(IntParameter, int32);
155 INT_BASED_PARAMETER(Int64Parameter, int64);
156 INT_BASED_PARAMETER(BoolParameter, bool);
157
158
159#define WIDEN2(x) L##x
160#define WIDEN(x) WIDEN2(x)
161#define DEFINE_ENUM_VALUE(parameter, Tenum, value, ...) parameter.Bind(Tenum::value, WIDEN(#value));
162
167 class BaseEnumParameter : public IParameter<int32>
168 {
169 protected:
174 std::unordered_map<int32, WideString> m_Bindings;
175
176 BaseEnumParameter(const String &name, int32 defaultValue) : IParameter(name, defaultValue) {}
177
179 int32 Write(nlohmann::json &target) const override
180 {
181 target["value"] = m_Value;
182 return sizeof(int32);
183 }
184
186 int32 Read(nlohmann::json &target) override
187 {
188 m_Value = target["value"];
189 return sizeof(int32);
190 }
191
192 public:
199 WideString GetBinding(int32 index) const { return m_Bindings.at(index); }
200
206 int32 GetBindingCount() const { return m_Bindings.size(); }
207 };
208
214 template<typename T>
215 class EnumParameter : public BaseEnumParameter
216 {
217 public:
218 EnumParameter(const String &name, const T &defaultValue) :
219 BaseEnumParameter(name, static_cast<int32>(defaultValue))
220 {
221 }
222
229 void Bind(const T &value, const WideString &text) { m_Bindings.emplace(static_cast<int32>(value), text); }
230
236 void SetEnum(const T &value) { m_Value = static_cast<int32>(value); }
237
242 T GetEnum() const { return static_cast<T>(m_Value); }
243 };
244
249 class StringParameter : public IParameter<String>
250 {
251 public:
252 StringParameter(const String &name, const String &defaultValue) : IParameter(name, defaultValue) {}
253
254 // @copydoc IBaseParameter::Write
255 int32 Write(nlohmann::json &target) const override
256 {
257 target["value"] = m_Value.Get();
258 return m_Value.GetLength();
259 }
260
261 // @copydoc IBaseParameter::Read
262 int32 Read(nlohmann::json &target) override
263 {
264 m_Value = target["value"].get<std::string>();
265 return m_Value.GetLength();
266 }
267 };
268
273 class GameObject
274 {
275 public:
276 virtual ~GameObject() = default;
277
278 private:
279 friend class GameSerializer;
280 friend class GlobalGameObjectRepository;
281
286 std::vector<IBaseParameter *> m_Parameters;
287
288 protected:
294
295 GameObject() : m_Identifier("Identifier", Random::RandString(16)) { addParameter(&m_Identifier); }
296
297 GameObject(const String &identifier) : m_Identifier("Identifier", identifier) { addParameter(&m_Identifier); }
298
304 void addParameter(IBaseParameter *parameter) { m_Parameters.push_back(parameter); };
305
313 {
314 const auto &iterator = std::ranges::find_if(m_Parameters, [&name](IBaseParameter *parameter)
315 { return parameter->GetName() == name; });
316
317 if (iterator == m_Parameters.end())
318 {
319 ERR("GameObject::findParameter: Couldn't find parameter with name '{}'", name);
320 return nullptr;
321 }
322 return iterator[0];
323 }
324
330 virtual GameObject *clone() = 0;
331
337 [[nodiscard]] virtual String GetIdentifer() const = 0;
338
343 virtual void onPostLoad() {};
344
349 virtual void onPreSerialize() {};
350
351 public:
357 std::vector<IBaseParameter *> GetParameters() const { return m_Parameters; }
358
364 [[nodiscard]] String GetUniqueId() const { return m_Identifier.GetValue(); }
365 };
366
372 {
373 friend class GameSerializer;
374
375 protected:
376 std::unordered_map<String, GameObject *> m_GameObjects;
377
378 std::unordered_map<String, GameObject *> &getMap() { return m_GameObjects; }
379 const std::unordered_map<String, GameObject *> &getMap() const { return m_GameObjects; }
380
381 public:
382 virtual ~GameObjectRegistry() { Clear(); };
383
390 GameObject *GetGameObject(const String &identifier)
391 {
392 GameObject *object = m_GameObjects.at(identifier);
393 return object;
394 }
395
400 void Clear()
401 {
402 const int32 objects = static_cast<int32>(m_GameObjects.size());
403 for (const auto &[key, object]: m_GameObjects)
404 {
405 if (key.StartsWith("Global::"))
406 continue;
407
408 delete object;
409 }
410 m_GameObjects.clear();
411
412 INFO("[GameObjectRegistry::Clear] Purged {} items", objects);
413 }
414
422 {
423 auto it = std::find_if(m_GameObjects.begin(), m_GameObjects.end(),
424 [identifier](const auto &pair) { return pair.second->GetUniqueId() == identifier; });
425
426 if (it != m_GameObjects.end())
427 {
428 return it->second;
429 }
430
431 return nullptr;
432 }
433
438 void RegisterGameObject(const String &name, GameObject *object)
439 {
440 m_GameObjects.insert(std::make_pair(name, object));
441 }
442 };
443
448 class ParentGameObject : public GameObject, protected std::vector<GameObject *>
449 {
450 friend class GameSerializer;
451
452 public:
453 ParentGameObject() : GameObject() {}
454
455 explicit ParentGameObject(const String &identifier) : GameObject(identifier) {}
456
462 virtual void Add(GameObject *object) { this->push_back(object); }
463 };
464
470 {
471 private:
472 friend class GameSerializer;
473 inline static std::unordered_map<String, GameObject *> m_Registry;
474
475 public:
480 static void Clear()
481 {
482 for (auto &object: m_Registry | std::views::values)
483 delete object;
484 }
485
491 static void RegisterGameObject(GameObject *object)
492 {
493 m_Registry.insert(std::make_pair(object->GetIdentifer(), object));
494 }
495
501 {
502 if (m_Registry.find(name) == m_Registry.end())
503 {
504 ERR("GlobalGameObjectRepository::CreateGameObjectInstance: returned nullptr for name {}", name);
505 return nullptr;
506 }
507
508 return m_Registry.find(name)->second->clone();
509 }
510 };
511} // namespace Phosphorus
std::unordered_map< int32, WideString > m_Bindings
Enum bindings.
Definition GameObject.h:174
int32 GetBindingCount() const
Returns binding count.
Definition GameObject.h:206
int32 Read(nlohmann::json &target) override
Parses text representation of data from JSON object.
Definition GameObject.h:186
WideString GetBinding(int32 index) const
Returns bound name for enum value.
Definition GameObject.h:199
int32 Write(nlohmann::json &target) const override
Writes text representation of data to JSON object.
Definition GameObject.h:179
void SetEnum(const T &value)
Casts enum to integer and sets it.
Definition GameObject.h:236
void Bind(const T &value, const WideString &text)
Sets binding for given enum value.
Definition GameObject.h:229
T GetEnum() const
Casts integer to enum and returns.
Definition GameObject.h:242
Represents named set of parameters which can be serialized.
Definition GameObject.h:274
virtual void onPostLoad()
Event called after object load from file.
Definition GameObject.h:343
void addParameter(IBaseParameter *parameter)
Adds parameter to the list.
Definition GameObject.h:304
virtual String GetIdentifer() const =0
Returns common identifier of GameObject. To be filled by derived class.
virtual GameObject * clone()=0
Clones GameObject.
IBaseParameter * findParameter(const String &name)
Returns parameter with given name.
Definition GameObject.h:312
StringParameter m_Identifier
Random GameObject identifier.
Definition GameObject.h:293
virtual void onPreSerialize()
Event called before object gets serialized.
Definition GameObject.h:349
std::vector< IBaseParameter * > GetParameters() const
Returns parameters list.
Definition GameObject.h:357
String GetUniqueId() const
Returns GameObject unique id.
Definition GameObject.h:364
Storage of GameObjects (scene). GameObjectRegistry take over ownership of object.
Definition GameObject.h:372
void RegisterGameObject(const String &name, GameObject *object)
Registers given GameObject.
Definition GameObject.h:438
GameObject * GetGameObjectByUniqueId(const String &identifier)
Find GameObject with unique id.
Definition GameObject.h:421
GameObject * GetGameObject(const String &identifier)
Find GameObject with given name.
Definition GameObject.h:390
void Clear()
Deletes all GameObject from registry.
Definition GameObject.h:400
Contains all GameObjects associated with name. Used by serialization system.
Definition GameObject.h:470
static void Clear()
Deletes all objects in repository.
Definition GameObject.h:480
static GameObject * CreateGameObjectInstance(const String &name)
Creates a GameObject instance.
Definition GameObject.h:500
static void RegisterGameObject(GameObject *object)
Registers GameObject in repository.
Definition GameObject.h:491
Represents parameter which can be serializable. Used by IParameter.
Definition GameObject.h:69
virtual String GetDisplayName() const =0
Returns parameter display name.
virtual int32 Read(nlohmann::json &target)=0
Parses text representation of data from JSON object.
virtual String GetName() const =0
Returns parameter name.
virtual int32 Write(nlohmann::json &target) const =0
Writes text representation of data to JSON object.
T GetValue() const
Returns value.
Definition GameObject.h:134
String GetDisplayName() const override
Returns parameter display name.
Definition GameObject.h:150
String GetName() const override
Returns parameter name.
Definition GameObject.h:143
void SetValue(const T &value)
Sets value.
Definition GameObject.h:127
virtual void Add(GameObject *object)
Adds GameObject to parent.
Definition GameObject.h:462
String based parameter.
Definition GameObject.h:250
int32 Read(nlohmann::json &target) override
Parses text representation of data from JSON object.
Definition GameObject.h:262
int32 Write(nlohmann::json &target) const override
Writes text representation of data to JSON object.
Definition GameObject.h:255
A utility which contains essential function for pseudorandom generating.
Definition Random.h:16
Represents a mutable sequence of characters, providing various member functions for string manipulati...
Definition String.h:16
Represents a mutable sequence of utf-16 characters, providing various member functions for string man...
Definition WideString.h:14