Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
ObjectFactory.h
Go to the documentation of this file.
1/*
2Olympe Engine V2 2025
3Nicolas Chereau
4nchereau@gmail.com
5
6Purpose:
7- Class that enables the creation of various game objects.
8
9*/
10#pragma once
11#include "Object.h"
12#include <map>
13#include <string>
14#include <functional>
15#include <memory>
16#include <stdexcept>
17#include <iostream>
18#include "World.h"
19#include "system/system_utils.h"
20#include "system/EventManager.h"
21#include "ObjectComponent.h"
22
23//DEPRECATED: use PrefabFactory instead for more advanced prefab management
24
25class ObjectFactory: public Object
26{
27public:
28 using CreatorFunction = std::function<Object* ()>;
29 std::map<std::string, CreatorFunction> m_registeredCreators;
30
32 {
33 //name = "ObjectFactory";
34 SYSTEM_LOG << "ObjectFactory created and Initialized\n";
35 // register to event manager to receive object events
40 };
49
51
52 // Per-class singleton accessors
54 {
56 return instance;
57 }
58 static ObjectFactory& Get() { return GetInstance(); }
59
60 /**
61 * @brief Enregistre une fonction de cr�ation pour un nom de classe donn�.
62 * @param className Le nom de la classe (cl�).
63 * @param creator La fonction de cr�ation (std::function retournant BaseObject*).
64 * @return true si l'enregistrement a r�ussi, false sinon (d�j� enregistr�).
65 */
66 bool Register(const std::string& className, CreatorFunction creator)
67 {
69 if (it != m_registeredCreators.end())
70 {
71 SYSTEM_LOG << "Warning: Class '" << className << "' already registered." << std::endl;
72 return false;
73 }
74 else
75 {
77 SYSTEM_LOG << "Class '" << className << "' registered." << std::endl;
78 return true;
79 }
80 }
81 //-------------------------------------------------------------
82 // @brief Check if a class is registered in the factory
83 // @param className The name of the class to check
84 // @return true if the class is registered, false otherwise
85 bool IsRegistered(const std::string& className) const
86 {
88 }
89 //-------------------------------------------------------------
90 /**
91 * @brief Cr�e une nouvelle instance de l'objet sp�cifi� par son nom de classe.
92 * @param className Le nom de la classe � cr�er.
93 * @return Un pointeur vers le nouvel objet BaseObject, ou nullptr si non trouv�.
94 */
95 Object* CreateObject(const std::string& className)
96 {
98 if (it == m_registeredCreators.end())
99 {
100 SYSTEM_LOG << "Error: ObjectFactory::CreateObject: Class '" << className << "' not found/registered in factory." << std::endl;
101 return nullptr;
102 }
103 Object* o = it->second(); // Cann the Create of the Object
104 World::Get().StoreObject(o);
105 return o;
106 }
107
108 ObjectComponent* AddComponent(const std::string& className, Object* owner)
109 {
110 auto it = m_registeredCreators.find(className);
111 if (it == m_registeredCreators.end())
112 {
113 SYSTEM_LOG << "Error: Class ObjectFactory::AddComponent: '" << className << "' not found/registered in factory." << std::endl;
114 return nullptr;
115 }
116
117 ObjectComponent* component = (ObjectComponent*) it->second(); // call the Create of the ObjectComponent
118 component->SetOwner(owner); // set the owner to the component
119 component->Initialize(); // initialize the component
120
121 World::Get().StoreComponent(component);
122
123 return component;
124 }
125
126 // Event handling: respond to create/destroy/property messages
127 virtual void OnEvent(const Message& msg) override
128 {
129 // All factory events are now Olympe events
130 switch (msg.msg_type)
131 {
133 {
134 /* if (!msg.className.empty())
135 {
136 Object* o = CreateObject(msg.className);
137 if (o)
138 {
139 if (!msg.objectName.empty()) o->name = msg.objectName;
140 // return the created object's UID via payload pointer (not ideal but works)
141 // we can't push directly a message back synchronously here; instead post a system message
142 Message res;
143 res.msg_type = EventType::Olympe_EventType_Default;
144 res.sender = this;
145 res.targetUid = o->GetUID();
146 res.objectName = o->name;
147 EventManager::Get().AddMessage(res);
148 SYSTEM_LOG << "Factory created object '" << o->name << "' uid=" << o->GetUID() << "\n";
149 }
150 }/**/
151 break;
152 }
154 {
155 uint64_t uid = msg.targetUid;
156 if (uid !=0)
157 {
158 // find and destroy object in world
159 // simple linear search - World could provide a helper
160 // we directly access World::objectlist isn't public; use World API if available
161 // For now iterate and delete
162 auto &list = GetWorldObjectList();
163 for (auto it = list.begin(); it != list.end(); ++it)
164 {
165 if ((*it)->GetUID() == uid)
166 {
167 delete *it;
168 list.erase(it);
169 SYSTEM_LOG << "Factory destroyed object uid=" << uid << "\n";
170 break;
171 }
172 }
173 }
174 break;
175 }
177 {
178 uint64_t uid = msg.targetUid;
179 /* if (uid != 0 && !msg.ComponentType.empty())
180 {
181 Object* o = FindObjectByUID(uid);
182 if (o)
183 {
184 SYSTEM_LOG << "Factory added property '" << msg.ComponentType << "' to object uid=" << uid << "\n";
185 }
186 }/**/
187 break;
188 }
190 {
191 // Not implemented: requires property type lookup and removal API in World
192 SYSTEM_LOG << "Factory received Property_Remove for uid=" << msg.targetUid << " (not implemented)\n";
193 break;
194 }
195 default:
196 break;
197 }
198 }
199
200private:
201 // helper to find object by uid in world
203 {
204 // access world's object list via reflection - World doesn't expose, so we add a helper local
205 auto &list = GetWorldObjectList();
206 for (auto obj : list) if (obj && obj->GetUID() == uid) return obj;
207 return nullptr;
208 }
209
210 // Access internal world object list via friend function (quick hack) - implement as wrapper
211 static std::vector<Object*>& GetWorldObjectList()
212 {
213 // World doesn't expose object list; we rely on a private static accessor in World (to be added)
214 return World::Get().GetObjectList();
215 }
216};
217
218
219// --- M�canisme d'Enregistrement Automatique ---
220
221// Fonction g�n�rique pour cr�er une instance de classe T
222template <typename T>
224{
225 // C++ moderne: retourne T* cast� en BaseObject*
226 return new T();
227}
228
229/**
230 * @brief Classe utilitaire qui enregistre la classe T dans la fabrique
231 * lors de son initialisation statique.
232 * @note Le m�canisme d'enregistrement se fait dans le constructeur de ce helper,
233 * qui est appel� statiquement au d�marrage du programme.
234 */
235template <typename T>
237{
238public:
243};
244
245/**
246 * @brief Macro pour enregistrer automatiquement une classe d�riv�e.
247 * Doit �tre plac� dans le fichier .cpp de la classe.
248 */
249#define REGISTER_OBJECT(ClassName) \
250 namespace { \
251 AutoRegister<ClassName> RegisterHelper_##ClassName(#ClassName); \
252 }
253
254
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
Object * createT()
ObjectType
Definition Object.h:19
World and ECS Manager for Olympe Engine.
Classe utilitaire qui enregistre la classe T dans la fabrique lors de son initialisation statique.
AutoRegister(const std::string &className)
void Unregister(void *owner, EventType type)
void Register(void *owner, EventType type, Listener callback)
static EventManager & Get()
virtual void SetOwner(Object *_owner)
bool Register(const std::string &className, CreatorFunction creator)
Enregistre une fonction de cr�ation pour un nom de classe donn�.
virtual ~ObjectFactory()
virtual void OnEvent(const Message &msg) override
Object * FindObjectByUID(uint64_t uid)
std::function< Object *()> CreatorFunction
virtual ObjectType GetObjectType() const
static ObjectFactory & Get()
bool IsRegistered(const std::string &className) const
ObjectComponent * AddComponent(const std::string &className, Object *owner)
static std::vector< Object * > & GetWorldObjectList()
Object * CreateObject(const std::string &className)
Cr�e une nouvelle instance de l'objet sp�cifi� par son nom de classe.
std::map< std::string, CreatorFunction > m_registeredCreators
static ObjectFactory & GetInstance()
uint64_t uid
Definition Object.h:56
static World & Get()
Get singleton instance (short form)
Definition World.h:232
@ Olympe_EventType_Property_Add
@ Olympe_EventType_Object_Destroy
@ Olympe_EventType_Property_Remove
@ Olympe_EventType_Object_Create
#define SYSTEM_LOG