Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
prefabfactory.h
Go to the documentation of this file.
1/**
2 * @file prefabfactory.h
3 * @brief Header file for PrefabFactory class, responsible for creating game object prefabs
4 * @author Nicolas Chereau
5 * @date 2025
6 *
7 * Purpose:
8 * - PrefabFactory class handles entity creation from blueprints
9 * - Supports both legacy function-based prefabs and modern component-agnostic instantiation
10 */
11#pragma once
12#include "system/system_utils.h"
13#include "ComponentDefinition.h"
14#include "PrefabScanner.h"
15#include "ParameterResolver.h"
16#include <map>
17#include <memory>
18#include <functional>
19#include <vector>
20
21using PrefabBuilder = std::function<void(EntityID)>;
22
23/**
24 * @brief Factory class for creating entities from prefab blueprints
25 *
26 * PrefabFactory manages entity creation from JSON blueprint files.
27 * It supports both modern component-agnostic instantiation and legacy function-based prefabs.
28 *
29 * @see PrefabBlueprint
30 * @see ComponentDefinition
31 */
33{
34public:
35 /**
36 * @brief Get singleton instance
37 * @return Reference to PrefabFactory instance
38 */
40 {
42 return instance;
43 }
44
45 // ========================================================================
46 // Component Factory Registry (Auto-Registration System)
47 // ========================================================================
48
49 /// Register a component factory (called by auto-registration system)
50 void RegisterComponentFactory(const std::string& componentName,
51 std::function<bool(EntityID, const ComponentDefinition&)> factory);
52
53 /// Check if a component is registered
54 bool IsComponentRegistered(const std::string& componentName) const;
55
56 /// Get list of all registered components (for debugging)
57 std::vector<std::string> GetRegisteredComponents() const;
58
59 // ========================================================================
60 // NEW: Centralized Prefab Management
61 // ========================================================================
62
63 /**
64 * @brief Preload all prefabs from directory
65 *
66 * Scans and caches all prefab blueprints for fast entity creation.
67 * Should be called once at engine startup (World::Initialize).
68 *
69 * @param prefabDirectory Path to prefab directory (default: "Blueprints/EntityPrefab")
70 */
71 void PreloadAllPrefabs(const std::string& prefabDirectory = "Blueprints/EntityPrefab");
72
73 /**
74 * @brief Create entity from prefab name
75 *
76 * Instantiates a new entity by loading and applying all components
77 * defined in the prefab blueprint file.
78 *
79 * @param prefabName Name of the prefab (without .json extension)
80 * @return EntityID of created entity, or INVALID_ENTITY_ID on failure
81 *
82 * @see PrefabBlueprint
83 * @see InstantiateComponent
84 */
85 EntityID CreateEntityFromPrefabName(const std::string& prefabName);
86
87 /// Create entity from prefab name with explicit layer override
88 // UNUSED EntityID CreateEntityFromPrefabName(const std::string& prefabName, RenderLayer layer);
89
90 /**
91 * @brief Get prefab count
92 * @return Number of loaded prefabs
93 */
94 int GetPrefabCount() const { return static_cast<int>(m_prefabRegistry.GetCount()); }
95
96 /**
97 * @brief Check if prefab exists
98 * @param prefabName Name of the prefab to check
99 * @return true if prefab exists in registry
100 */
101 bool HasPrefab(const std::string& prefabName) const
102 {
103 return m_prefabRegistry.Find(prefabName) != nullptr;
104 }
105
106 // ========================================================================
107 // Legacy API (for backward compatibility)
108 // ========================================================================
109
110 /**
111 * @brief Register a legacy prefab builder function
112 * @param name Prefab name
113 * @param builder Function that builds the entity
114 */
115 void RegisterPrefab(const std::string& name, PrefabBuilder builder)
116 {
117 m_prefabs[name] = builder;
118 SYSTEM_LOG << "PrefabFactory::RegisteredPrefab has registered: " << name << "\n";
119 }
120
121 /**
122 * @brief Create an entity using legacy prefab system
123 * @param prefabName Name of the prefab
124 * @return EntityID of created entity
125 */
126 EntityID CreateEntity(const std::string& prefabName);
127
128 // ========================================================================
129 // Modern Component-Agnostic API
130 // ========================================================================
131
132 /**
133 * @brief Set the prefab registry cache
134 * @param registry PrefabRegistry to use for lookups
135 */
137
138 /**
139 * @brief Get the cached prefab registry
140 * @return Reference to the prefab registry
141 */
143
144 /**
145 * @brief Normalize a type string to canonical form
146 * @param type Type string to normalize
147 * @return Normalized type string
148 */
149 std::string NormalizeType(const std::string& type) const;
150
151 /**
152 * @brief Check if two types are equivalent
153 * @param type1 First type string
154 * @param type2 Second type string
155 * @return true if types are equivalent
156 */
157 bool AreTypesEquivalent(const std::string& type1, const std::string& type2) const;
158
159 /**
160 * @brief Check if a type is registered
161 * @param type Type string to check (will be normalized)
162 * @return true if type is registered
163 */
164 bool IsTypeRegistered(const std::string& type) const;
165
166 /**
167 * @brief Get canonical type info for debugging
168 * @param type Type to query
169 * @param outCanonical Output parameter for canonical type name
170 * @param outPrefabFile Output parameter for prefab file path
171 * @return true if type was found
172 */
173 bool GetCanonicalInfo(const std::string& type, std::string& outCanonical,
174 std::string& outPrefabFile) const;
175
176 /**
177 * @brief Create entity from a parsed blueprint
178 *
179 * @param blueprint Parsed prefab blueprint
180 * @param autoAssignLayer If true, automatically assign layer based on EntityType
181 * @return EntityID of created entity
182 */
184
185 /**
186 * @brief Create entity from blueprint with level instance parameter overrides
187 *
188 * This is the unified method for both static and dynamic object creation.
189 *
190 * @param blueprint Parsed prefab blueprint
191 * @param instanceParams Level instance parameters to override blueprint defaults
192 * @param autoAssignLayer If true, automatically assign layer based on EntityType
193 * @return EntityID of created entity
194 */
198 bool autoAssignLayer = true);
199
200 /**
201 * @brief Instantiate a single component on an entity
202 *
203 * Component-agnostic instantiation method that works with any registered component type.
204 *
205 * @param entity EntityID to add component to
206 * @param componentDef Component definition with type and parameters
207 * @return true if component was successfully instantiated
208 */
210
211private:
212 std::map<std::string, PrefabBuilder> m_prefabs;
214 bool m_prefabsPreloaded = false;
215 std::unique_ptr<PrefabScanner> m_scanner; // For type normalization
216
217 // Registry: component name -> factory function
218 std::map<std::string, std::function<bool(EntityID, const ComponentDefinition&)>> m_componentFactories;
219
220 PrefabFactory() = default;
221
222 // ========================================================================
223 // Component-specific instantiation helpers
224 // ========================================================================
225
245 bool InstantiateFX(EntityID entity, const ComponentDefinition& def);
250 bool InstantiateNPC(EntityID entity, const ComponentDefinition& def);
259};
260
261// Helper function for auto-registration (called by macro)
263 std::function<bool(EntityID, const ComponentDefinition&)> factory);
264
265// Macro pour faciliter l'enregistrement automatique (similaire � votre ancienne m�thode)
266#define REGISTER_PREFAB(Name, BuilderLambda) \
267 namespace { \
268 struct AutoRegister##Name { \
269 AutoRegister##Name() { \
270 PrefabFactory::Get().RegisterPrefab(#Name, BuilderLambda); \
271 } \
272 }; \
273 AutoRegister##Name global_##Name; \
274 }
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
std::uint64_t EntityID
Definition ECS_Entity.h:21
Factory class for creating entities from prefab blueprints.
const PrefabRegistry & GetPrefabRegistry() const
Get the cached prefab registry.
bool InstantiatePosition(EntityID entity, const ComponentDefinition &def)
bool InstantiateAIBehavior(EntityID entity, const ComponentDefinition &def)
void RegisterComponentFactory(const std::string &componentName, std::function< bool(EntityID, const ComponentDefinition &)> factory)
Register a component factory (called by auto-registration system)
bool InstantiateComponent(EntityID entity, const ComponentDefinition &componentDef)
Instantiate a single component on an entity.
bool InstantiateAttackIntent(EntityID entity, const ComponentDefinition &def)
bool InstantiateAudioSource(EntityID entity, const ComponentDefinition &def)
bool InstantiateCameraEffects(EntityID entity, const ComponentDefinition &def)
std::unique_ptr< PrefabScanner > m_scanner
std::map< std::string, std::function< bool(EntityID, const ComponentDefinition &)> > m_componentFactories
bool InstantiateCameraBounds(EntityID entity, const ComponentDefinition &def)
bool IsComponentRegistered(const std::string &componentName) const
Check if a component is registered.
bool InstantiatePhysicsBody(EntityID entity, const ComponentDefinition &def)
int GetPrefabCount() const
Create entity from prefab name with explicit layer override.
bool InstantiateCamera(EntityID entity, const ComponentDefinition &def)
bool InstantiatePlayerController(EntityID entity, const ComponentDefinition &def)
std::string NormalizeType(const std::string &type) const
Normalize a type string to canonical form.
bool InstantiateAIBlackboard(EntityID entity, const ComponentDefinition &def)
PrefabFactory()=default
PrefabRegistry m_prefabRegistry
bool AreTypesEquivalent(const std::string &type1, const std::string &type2) const
Check if two types are equivalent.
bool InstantiateController(EntityID entity, const ComponentDefinition &def)
bool GetCanonicalInfo(const std::string &type, std::string &outCanonical, std::string &outPrefabFile) const
Get canonical type info for debugging.
void SetPrefabRegistry(const PrefabRegistry &registry)
Set the prefab registry cache.
bool IsTypeRegistered(const std::string &type) const
Check if a type is registered.
bool InstantiateBoundingBox(EntityID entity, const ComponentDefinition &def)
EntityID CreateEntityWithOverrides(const PrefabBlueprint &blueprint, const LevelInstanceParameters &instanceParams, bool autoAssignLayer=true)
Create entity from blueprint with level instance parameter overrides.
bool InstantiateVisualSprite(EntityID entity, const ComponentDefinition &def)
bool InstantiateVisualAnimation(EntityID entity, const ComponentDefinition &def)
bool InstantiateMoveIntent(EntityID entity, const ComponentDefinition &def)
std::vector< std::string > GetRegisteredComponents() const
Get list of all registered components (for debugging)
bool InstantiatePlayerBinding(EntityID entity, const ComponentDefinition &def)
bool InstantiateBehaviorTreeRuntime(EntityID entity, const ComponentDefinition &def)
EntityID CreateEntityFromBlueprint(const PrefabBlueprint &blueprint, bool autoAssignLayer=true)
Create entity from a parsed blueprint.
bool InstantiateCollisionZone(EntityID entity, const ComponentDefinition &def)
std::map< std::string, PrefabBuilder > m_prefabs
bool InstantiateVisualEditor(EntityID entity, const ComponentDefinition &def)
bool InstantiateFX(EntityID entity, const ComponentDefinition &def)
bool InstantiateAIState(EntityID entity, const ComponentDefinition &def)
static PrefabFactory & Get()
Get singleton instance.
bool InstantiateInputMapping(EntityID entity, const ComponentDefinition &def)
bool InstantiateNavigationAgent(EntityID entity, const ComponentDefinition &def)
bool InstantiateCameraInputBinding(EntityID entity, const ComponentDefinition &def)
bool InstantiateAISenses(EntityID entity, const ComponentDefinition &def)
bool InstantiateIdentity(EntityID entity, const ComponentDefinition &def)
EntityID CreateEntity(const std::string &prefabName)
Create an entity using legacy prefab system.
void RegisterPrefab(const std::string &name, PrefabBuilder builder)
Register a legacy prefab builder function.
EntityID CreateEntityFromPrefabName(const std::string &prefabName)
Create entity from prefab name.
bool InstantiateAnimation(EntityID entity, const ComponentDefinition &def)
bool InstantiateHealth(EntityID entity, const ComponentDefinition &def)
bool InstantiateNPC(EntityID entity, const ComponentDefinition &def)
void PreloadAllPrefabs(const std::string &prefabDirectory="Blueprints/EntityPrefab")
Preload all prefabs from directory.
bool InstantiateTriggerZone(EntityID entity, const ComponentDefinition &def)
bool InstantiateMovement(EntityID entity, const ComponentDefinition &def)
bool InstantiateInventory(EntityID entity, const ComponentDefinition &def)
bool InstantiateCameraTarget(EntityID entity, const ComponentDefinition &def)
bool HasPrefab(const std::string &prefabName) const
Check if prefab exists.
const PrefabBlueprint * Find(const std::string &name) const
int GetCount() const
void RegisterComponentFactory_Internal(const char *componentName, std::function< bool(EntityID, const ComponentDefinition &)> factory)
std::function< void(EntityID)> PrefabBuilder
#define SYSTEM_LOG