Skip to main content

GameObjectFactory.ixx File

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

Included Headers

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

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.