Skip to main content

GameObjectFactory.ixx File

Factory for creating and cloning GameObjects with fluent configuration. More...

Included Headers

#include <memory> #include <functional> #include <helios.math> #include <helios.util.Guid> #include <helios.engine.mechanics> #include <helios.engine.core.units> #include <helios.engine.modules> #include <helios.engine.runtime.world.GameWorld> #include <helios.engine.builder.gameObject.builders> #include <helios.engine.ecs.GameObject>

Namespaces Index

namespacehelios
namespaceengine

Main engine module aggregating core infrastructure and game systems. More...

namespacebuilder

Fluent builder pattern for constructing GameObjects. More...

namespacegameObject

Factory and prototype classes for GameObject construction. More...

Classes Index

classGameObjectFactory

Factory class for creating and cloning GameObjects. More...

classGameObjectPrototype

Internal builder class for fluent GameObject configuration. More...

Description

Factory for creating and cloning GameObjects with fluent configuration.

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file GameObjectFactory.ixx
3 * @brief Factory for creating and cloning GameObjects with fluent configuration.
4 */
5module;
6
7#include <memory>
8#include <functional>
9
10export module helios.engine.builder.gameObject.GameObjectFactory;
11
12import helios.engine.ecs.GameObject;
13
14import helios.engine.builder.gameObject.builders;
15
16import helios.engine.runtime.world.GameWorld;
17
18import helios.engine.modules;
19import helios.engine.mechanics;
20import helios.util.Guid;
21import helios.engine.core.units;
22import helios.math;
23
25
26 /**
27 * @brief Factory class for creating and cloning GameObjects.
28 *
29 * Provides static methods for constructing new GameObjects or creating
30 * copies from existing templates. Uses the builder pattern internally
31 * via `GameObjectPrototype` for fluent component configuration.
32 *
33 * Example usage:
34 * ```cpp
35 * auto player = GameObjectFactory::gameObject()
36 * .withMotion([](auto& m) { m.move2D().speed(5.0f); })
37 * .withRendering([&](auto& r) { r.renderable().shape(shape).shader(shader).build(); })
38 * .make(true);
39 * ```
40 */
42
43 /**
44 * @brief Internal builder class for fluent GameObject configuration.
45 *
46 * Holds domain-specific builders and provides callback-based methods
47 * for configuring different aspects of a GameObject.
48 */
49 class GameObjectPrototype {
50
51 /**
52 * @brief The GameObject being configured.
53 */
55
56 /**
57 * @brief Builder for motion-related components.
58 */
59 std::unique_ptr<helios::engine::builder::gameObject::builders::MotionBuilder> motionBuilder_;
60
61 /**
62 * @brief Builder for rendering-related components.
63 */
64 std::unique_ptr<helios::engine::builder::gameObject::builders::RenderingBuilder> renderingBuilder_;
65
66 /**
67 * @brief Builder for scene-related components.
68 */
69 std::unique_ptr<helios::engine::builder::gameObject::builders::SceneBuilder> sceneBuilder_;
70
71 /**
72 * @brief Builder for collision-related components.
73 */
74 std::unique_ptr<helios::engine::builder::gameObject::builders::CollisionBuilder> collisionBuilder_;
75
76 /**
77 * @brief Builder for spatial transform components.
78 */
79 std::unique_ptr<helios::engine::builder::gameObject::builders::TransformBuilder> transformBuilder_;
80
81 /**
82 * @brief Builder for UI transform components.
83 */
84 std::unique_ptr<helios::engine::builder::gameObject::builders::UiTransformBuilder> uiTransformBuilder_;
85
86 /**
87 * @brief Builder for visual effects components.
88 */
89 std::unique_ptr<helios::engine::builder::gameObject::builders::EffectsBuilder> effectsBuilder_;
90
91 /**
92 * @brief Builder for spawn-related components.
93 */
94 std::unique_ptr<helios::engine::builder::gameObject::builders::SpawnBuilder> spawnBuilder_;
95
96 /**
97 * @brief Builder for AI behavior components.
98 */
99 std::unique_ptr<helios::engine::builder::gameObject::builders::AiBuilder> aiBuilder_;
100
101 /**
102 * @brief Builder for combat-related components.
103 */
104 std::unique_ptr<helios::engine::builder::gameObject::builders::CombatBuilder> CombatBuilder_;
105
106 /**
107 * @brief Builder for lifecycle components.
108 */
109 std::unique_ptr<helios::engine::builder::gameObject::builders::LifecycleBuilder> lifecycleBuilder_;
110
111 /**
112 * @brief Builder for health-related components.
113 */
114 std::unique_ptr<helios::engine::builder::gameObject::builders::HealthBuilder> healthBuilder_;
115
116 /**
117 * @brief Builder for scoring-related components.
118 */
119 std::unique_ptr<helios::engine::builder::gameObject::builders::ScoringBuilder> scoringBuilder_;
120
121 /**
122 * @brief Builder for observer components.
123 */
124 std::unique_ptr<helios::engine::builder::gameObject::builders::ObserverBuilder> observerBuilder_;
125 public:
126
127 /** @brief Callback type for motion configuration. */
128 using MotionBuilderCallback = std::function<void(helios::engine::builder::gameObject::builders::MotionBuilder&)>;
129
130 /** @brief Callback type for rendering configuration. */
131 using RenderingBuilderCallback = std::function<void(helios::engine::builder::gameObject::builders::RenderingBuilder&)>;
132
133 /** @brief Callback type for scene configuration. */
134 using SceneBuilderCallback = std::function<void(helios::engine::builder::gameObject::builders::SceneBuilder&)>;
135
136 /** @brief Callback type for transform configuration. */
137 using TransformBuilderCallback = std::function<void(helios::engine::builder::gameObject::builders::TransformBuilder&)>;
138
139 /** @brief Callback type for UI transform configuration. */
140 using UiTransformBuilderCallback = std::function<void(helios::engine::builder::gameObject::builders::UiTransformBuilder&)>;
141
142 /** @brief Callback type for collision configuration. */
143 using CollisionBuilderCallback = std::function<void(helios::engine::builder::gameObject::builders::CollisionBuilder&)>;
144
145 /** @brief Callback type for effects configuration. */
146 using EffectsBuilderCallback = std::function<void(helios::engine::builder::gameObject::builders::EffectsBuilder&)>;
147
148 /** @brief Callback type for spawn configuration. */
149 using SpawnBuilderCallback = std::function<void(helios::engine::builder::gameObject::builders::SpawnBuilder&)>;
150
151 /** @brief Callback type for AI configuration. */
152 using AiBuilderCallback = std::function<void(helios::engine::builder::gameObject::builders::AiBuilder&)>;
153
154 /** @brief Callback type for combat configuration. */
155 using CombatBuilderCallback = std::function<void(helios::engine::builder::gameObject::builders::CombatBuilder&)>;
156
157 /** @brief Callback type for lifecycle configuration. */
158 using LifecycleBuilderCallback = std::function<void(helios::engine::builder::gameObject::builders::LifecycleBuilder&)>;
159
160 /** @brief Callback type for health configuration. */
161 using HealthBuilderCallback = std::function<void(helios::engine::builder::gameObject::builders::HealthBuilder&)>;
162
163 /** @brief Callback type for scoring configuration. */
164 using ScoringBuilderCallback = std::function<void(helios::engine::builder::gameObject::builders::ScoringBuilder&)>;
165
166 /** @brief Callback type for observer configuration. */
167 using ObserverBuilderCallback = std::function<void(helios::engine::builder::gameObject::builders::ObserverBuilder&)>;
168
169
170 /**
171 * @brief Constructs a prototype with a new empty GameObject.
172 */
173 GameObjectPrototype(helios::engine::runtime::world::GameWorld& gameWorld) :
174 gameObject_(gameWorld.addGameObject()),
175 motionBuilder_(std::make_unique<helios::engine::builder::gameObject::builders::MotionBuilder>(gameObject_)),
176 renderingBuilder_(std::make_unique<helios::engine::builder::gameObject::builders::RenderingBuilder>(gameObject_)),
177 sceneBuilder_(std::make_unique<helios::engine::builder::gameObject::builders::SceneBuilder>(gameObject_)),
178 collisionBuilder_(std::make_unique<helios::engine::builder::gameObject::builders::CollisionBuilder>(gameObject_)),
179 transformBuilder_(std::make_unique<helios::engine::builder::gameObject::builders::TransformBuilder>(gameObject_)),
180 uiTransformBuilder_(std::make_unique<helios::engine::builder::gameObject::builders::UiTransformBuilder>(gameObject_)),
181 effectsBuilder_(std::make_unique<helios::engine::builder::gameObject::builders::EffectsBuilder>(gameObject_)),
182 spawnBuilder_(std::make_unique<helios::engine::builder::gameObject::builders::SpawnBuilder>(gameObject_)),
183 aiBuilder_(std::make_unique<helios::engine::builder::gameObject::builders::AiBuilder>(gameObject_)),
184 CombatBuilder_(std::make_unique<helios::engine::builder::gameObject::builders::CombatBuilder>(gameObject_)),
185 lifecycleBuilder_(std::make_unique<helios::engine::builder::gameObject::builders::LifecycleBuilder>(gameObject_)),
186 healthBuilder_(std::make_unique<helios::engine::builder::gameObject::builders::HealthBuilder>(gameObject_)),
187 scoringBuilder_(std::make_unique<helios::engine::builder::gameObject::builders::ScoringBuilder>(gameObject_)),
188 observerBuilder_(std::make_unique<helios::engine::builder::gameObject::builders::ObserverBuilder>(gameObject_))
189
190 {}
191
192 /**
193 * @brief Returns a reference to this prototype.
194 *
195 * @return Reference to this prototype.
196 */
197 GameObjectPrototype& prototype() {
198 return *this;
199 }
200
201 /**
202 * @brief Finalizes the prototype and returns the configured GameObject.
203 *
204 * @param active Whether the GameObject should be active upon creation.
205 *
206 * @return Ownership of the configured GameObject.
207 */
208 helios::engine::ecs::GameObject make(const bool active = false) {
209 gameObject_.setActive(active);
210 return gameObject_;
211 }
212
213
214
215 /**
216 * @brief Configures motion-related components.
217 *
218 * @param mcb Callback receiving a MotionBuilder reference.
219 *
220 * @return Reference to this prototype for chaining.
221 */
222 GameObjectPrototype& withMotion(const MotionBuilderCallback& mcb) {
223 mcb(*motionBuilder_);
224 return *this;
225 }
226
227 /**
228 * @brief Configures rendering-related components.
229 *
230 * @param rbc Callback receiving a RenderingBuilder reference.
231 *
232 * @return Reference to this prototype for chaining.
233 */
234 GameObjectPrototype& withRendering(const RenderingBuilderCallback& rbc) {
235 rbc(*renderingBuilder_);
236 return *this;
237 }
238
239 /**
240 * @brief Configures collision-related components.
241 *
242 * @param cbc Callback receiving a CollisionBuilder reference.
243 *
244 * @return Reference to this prototype for chaining.
245 */
246 GameObjectPrototype& withCollision(const CollisionBuilderCallback& cbc) {
247 cbc(*collisionBuilder_);
248 return *this;
249 }
250
251 /**
252 * @brief Configures spatial transform components.
253 *
254 * @param tbc Callback receiving a TransformBuilder reference.
255 *
256 * @return Reference to this prototype for chaining.
257 */
258 GameObjectPrototype& withTransform(const TransformBuilderCallback& tbc) {
259 tbc(*transformBuilder_);
260 return *this;
261 }
262
263 /**
264 * @brief Configures UI transform components for viewport-relative positioning.
265 *
266 * @param utbc Callback receiving a UiTransformBuilder reference.
267 *
268 * @return Reference to this prototype for chaining.
269 */
270 GameObjectPrototype& withUiTransform(const UiTransformBuilderCallback& utbc) {
271 utbc(*uiTransformBuilder_);
272 return *this;
273 }
274
275 /**
276 * @brief Configures visual effects components.
277 *
278 * @param ebc Callback receiving an EffectsBuilder reference.
279 *
280 * @return Reference to this prototype for chaining.
281 */
282 GameObjectPrototype& withEffects(const EffectsBuilderCallback& ebc) {
283 ebc(*effectsBuilder_);
284 return *this;
285 }
286
287 /**
288 * @brief Configures spawn-related components.
289 *
290 * @param sbc Callback receiving a SpawnBuilder reference.
291 *
292 * @return Reference to this prototype for chaining.
293 */
294 GameObjectPrototype& withSpawn(const SpawnBuilderCallback& sbc) {
295 sbc(*spawnBuilder_);
296 return *this;
297 }
298
299 /**
300 * @brief Configures AI behavior components.
301 *
302 * @param aibc Callback receiving an AiBuilder reference.
303 *
304 * @return Reference to this prototype for chaining.
305 */
306 GameObjectPrototype& withAi(const AiBuilderCallback& aibc) {
307 aibc(*aiBuilder_);
308 return *this;
309 }
310
311 /**
312 * @brief Configures weapon and shooting components.
313 *
314 * @param sbc Callback receiving a CombatBuilder reference.
315 *
316 * @return Reference to this prototype for chaining.
317 */
318 GameObjectPrototype& withCombat(const CombatBuilderCallback& sbc) {
319 sbc(*CombatBuilder_);
320 return *this;
321 }
322
323 /**
324 * @brief Configures lifecycle components.
325 *
326 * @param sbc Callback receiving a LifecycleBuilder reference.
327 *
328 * @return Reference to this prototype for chaining.
329 */
330 GameObjectPrototype& withLifecycle(const LifecycleBuilderCallback& sbc) {
331 sbc(*lifecycleBuilder_);
332 return *this;
333 }
334
335 /**
336 * @brief Configures health-related components.
337 *
338 * @param hbc Callback receiving a HealthBuilder reference.
339 *
340 * @return Reference to this prototype for chaining.
341 */
342 GameObjectPrototype& withHealth(const HealthBuilderCallback& hbc) {
343 hbc(*healthBuilder_);
344 return *this;
345 }
346
347 /**
348 * @brief Configures scoring-related components.
349 *
350 * @param hbc Callback receiving a ScoringBuilder reference.
351 *
352 * @return Reference to this prototype for chaining.
353 */
354 GameObjectPrototype& withScoring(const ScoringBuilderCallback& hbc) {
355 hbc(*scoringBuilder_);
356 return *this;
357 }
358
359 /**
360 * @brief Configures observer components for data binding.
361 *
362 * @param obc Callback receiving an ObserverBuilder reference.
363 *
364 * @return Reference to this prototype for chaining.
365 */
366 GameObjectPrototype& withObserver(const ObserverBuilderCallback& obc) {
367 obc(*observerBuilder_);
368 return *this;
369 }
370
371 };
372
373 public:
374
375 /**
376 * @brief Returns the singleton instance of the factory.
377 *
378 * @return Reference to the singleton factory.
379 */
381 static GameObjectFactory inst{};
382 return inst;
383 }
384
385 /**
386 * @brief Creates a new empty GameObject prototype.
387 *
388 * @param gameWorld The world where the GameObject lives.
389 *
390 * @return A prototype for fluent configuration.
391 */
392 static GameObjectPrototype gameObject(helios::engine::runtime::world::GameWorld& gameWorld) {
393 return GameObjectPrototype(gameWorld);
394 }
395
396 };
397
398}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.