Skip to main content

MoveInitializer.ixx File

Spawn initializer that sets initial movement direction for spawned entities. More...

Included Headers

Namespaces Index

namespacehelios
namespaceengine

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

namespaceruntime

Runtime infrastructure for game execution and lifecycle orchestration. More...

namespacespawn

Entity spawning infrastructure for the helios engine. More...

namespacebehavior

Spawn behavior strategies for positioning and initializing entities. More...

namespaceinitializers

Concrete SpawnInitializer implementations. More...

Classes Index

classMoveInitializer

Initializer that sets movement direction for spawned entities. More...

Description

Spawn initializer that sets initial movement direction for spawned entities.

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file MoveInitializer.ixx
3 * @brief Spawn initializer that sets initial movement direction for spawned entities.
4 */
5module;
6
7#include <cassert>
8#include <cmath>
9
10export module helios.engine.runtime.spawn.behavior.initializers.MoveInitializer;
11
12import helios.engine.runtime.spawn.behavior.SpawnInitializer;
13import helios.engine.runtime.spawn.types.SpawnPlanCursor;
14import helios.engine.runtime.spawn.types.SpawnContext;
15import helios.engine.ecs.GameObject;
16import helios.engine.modules.physics.motion.components.Move2DComponent;
17import helios.engine.modules.physics.motion.components.DirectionComponent;
18
19import helios.engine.modules.spatial.transform.components.TranslationStateComponent;
20
21import helios.engine.modules.spatial.transform.components.RotationStateComponent;
22import helios.engine.modules.physics.motion.components.SteeringComponent;
23
24import helios.math;
25import helios.util.Random;
26
29
30 /**
31 * @brief Specifies the direction initialization strategy.
32 */
33 enum class DirectionType {
34
35 /**
36 * @brief Generates a random normalized direction vector.
37 */
39
40 /**
41 * @brief Sets direction to positive X-axis.
42 */
44
45 /**
46 * @brief Sets direction to negative X-axis.
47 */
49
50 /**
51 * @brief Sets direction to a custom axis vector.
52 */
54
55 /**
56 * @brief Sets direction toward a specific target point.
57 *
58 * @details The direction is calculated as the normalized vector from
59 * the entity's current position to the specified target point.
60 */
62 };
63
64 /**
65 * @brief Initializer that sets movement direction for spawned entities.
66 *
67 * @details MoveInitializer configures the DirectionComponent and
68 * Move2DComponent of spawned entities based on the specified DirectionType
69 * strategy. This determines the initial movement vector of the entity.
70 *
71 * ## Direction Strategies
72 *
73 * - **Random:** Generates a random normalized 2D direction using a seeded RNG.
74 * - **Right:** Sets direction to positive X-axis (1, 0, 0).
75 * - **Left:** Sets direction to negative X-axis (-1, 0, 0).
76 * - **Axis:** Sets direction to a custom provided axis vector.
77 * - **Point:** Calculates direction toward a specific target point.
78 *
79 * ## Required Components
80 *
81 * Spawned entities must have:
82 * - `DirectionComponent` - receives the calculated direction.
83 * - `Move2DComponent` - receives the move command with direction.
84 * - `SteeringComponent` (Optional) - receives the rotation looking at direction.
85 * - `TranslationStateComponent` (Required for `Point` strategy) - provides current position.
86 *
87 * ## Usage Examples
88 *
89 * Random direction:
90 * ```cpp
91 * auto initializer = std::make_unique<MoveInitializer>(DirectionType::Random);
92 * spawnProfile.spawnInitializer = std::move(initializer);
93 * ```
94 *
95 * Custom axis direction:
96 * ```cpp
97 * auto axis = helios::math::vec3f{0.707f, 0.707f, 0.0f}; // 45 degrees
98 * auto initializer = std::make_unique<MoveInitializer>(axis);
99 * ```
100 *
101 * Direction toward a target point:
102 * ```cpp
103 * auto target = helios::math::vec3f{100.0f, 50.0f, 0.0f};
104 * auto initializer = std::make_unique<MoveInitializer>(target, DirectionType::Point);
105 * ```
106 *
107 * @see SpawnInitializer
108 * @see DirectionComponent
109 * @see Move2DComponent
110 * @see TranslationStateComponent
111 */
112 class MoveInitializer final : public SpawnInitializer {
113
114 helios::util::Random rGen_{12345};
115
116 /**
117 * @brief The direction strategy to apply during initialization.
118 */
119 const DirectionType directionType_ = DirectionType::Random;
120
121 /**
122 * @brief Stores the custom direction target (axis/point) when using DirectionType::Axis/::Point.
123 */
124 helios::math::vec3f direction_{};
125
126 /**
127 * @brief Applies a random direction to the entity.
128 *
129 * @details Generates a random 2D vector, normalizes it, and sets it
130 * as the entity's direction. Also triggers a move command with
131 * full throttle (1.0).
132 *
133 * If a SteeringComponent is present, the entity is rotated to face the
134 * movement direction.
135 *
136 * @param gameObject The spawned entity to initialize.
137 * @param cursor The spawn plan cursor (unused).
138 * @param spawnContext The spawn context (unused).
139 */
140 void random(
142 const SpawnPlanCursor& cursor,
143 const SpawnContext& spawnContext
144 ) noexcept {
145
148
149 auto dir = helios::math::vec2f{
150 rGen_.randomFloat(-1.0f, 1.0f),
151 rGen_.randomFloat(-1.0f, 1.0f)
152 };
154 if (sc) {
155 sc->setTargetRotationAngle(helios::math::degrees(std::atan2(dir[1], dir[0])));
156 sc->setCurrentRotationAngle(helios::math::degrees(std::atan2(dir[1], dir[0])));
157 }
158
159 dc->setDirection(dir.normalize().toVec3());
160 mc->move(dc->direction(), 1.0f);
161 }
162
163 /**
164 * @brief Aligns the entity's movement direction to a target axis or point.
165 *
166 * @details Depending on the directionType parameter:
167 * - **Axis:** Sets the entity's direction to the provided normalized vector.
168 * - **Point:** Calculates the direction from the entity's current position
169 * to the target point and normalizes it.
170 *
171 * In both cases, triggers a move command with full throttle (1.0) and
172 * updates the SteeringComponent to rotate the entity toward the movement
173 * direction.
174 *
175 * @param gameObject The spawned entity to initialize.
176 * @param cursor The spawn plan cursor (unused).
177 * @param spawnContext The spawn context (unused).
178 * @param target The target axis vector or point coordinates.
179 * @param directionType The direction strategy (`Axis` or `Point`).
180 */
181 void alignTo (
183 const SpawnPlanCursor& cursor,
184 const SpawnContext& spawnContext,
185 const helios::math::vec3f target,
186 const DirectionType directionType = DirectionType::Axis
187
188 ) noexcept {
189
193
194 auto direction = target;
195 if (directionType == DirectionType::Axis) {
196 assert(direction.isNormalized() && "axis initializer requires valid direction vector");
197 } else if (directionType == DirectionType::Point) {
199
200 direction = (target - (tsc->translation() * -1.0f)).normalize();
201 assert(direction.isNormalized() && "point initializer requires valid direction vector");
202 }
203
204 dc->setDirection(direction);
205
206 sc->setTargetRotationAngle(helios::math::degrees(std::atan2(direction[1], direction[0])));
207 sc->setCurrentRotationAngle(helios::math::degrees(std::atan2(direction[1], direction[0])));
208
209 mc->move(direction, 1.0f);
210
211
212 }
213 public:
214
215 /**
216 * @brief Constructs a MoveInitializer with the specified strategy.
217 *
218 * @param direction The direction strategy to use during initialization.
219 */
220 explicit MoveInitializer(const DirectionType direction) : directionType_(direction) {}
221
222 /**
223 * @brief Constructs a MoveInitializer with a custom axis.
224 *
225 * @details Sets the strategy to `DirectionType::Axis` and stores the provided
226 * axis vector.
227 *
228 * @param directionAxis The custom direction vector to use.
229 */
230 explicit MoveInitializer(const helios::math::vec3f directionAxis) : directionType_(DirectionType::Axis), direction_(directionAxis) {}
231
232 /**
233 * @brief Constructs a MoveInitializer with a target point or axis and explicit strategy.
234 *
235 * @details Allows specifying both a target vector and the direction strategy.
236 * Use `DirectionType::Point` to calculate direction toward the target point,
237 * or `DirectionType::Axis` to use the vector as a direct direction.
238 *
239 * @param target The target point or axis vector.
240 * @param direction The direction strategy to use.
241 */
242 explicit MoveInitializer(const helios::math::vec3f target, const DirectionType direction) : directionType_(direction), direction_(target) {}
243
244
245 /**
246 * @brief Initializes the spawned entity's direction and movement.
247 *
248 * @details Delegates to the appropriate strategy method based on
249 * the configured Direction enum value.
250 *
251 * @param gameObject The spawned entity to initialize.
252 * @param cursor The spawn plan cursor providing batch context.
253 * @param spawnContext The spawn context with emitter information.
254 */
257 const SpawnPlanCursor& cursor,
258 const SpawnContext& spawnContext
259 ) override {
260
261 switch (directionType_) {
263 random(gameObject, cursor, spawnContext);
264 return;
266 alignTo(gameObject, cursor, spawnContext, helios::math::X_AXISf);
267 return;
269 alignTo(gameObject, cursor, spawnContext, helios::math::X_AXISf * -1.0f);
270 return;
272 alignTo(gameObject, cursor, spawnContext, direction_);
273 return;
274
276 alignTo(gameObject, cursor, spawnContext, direction_, directionType_);
277 return;
278 }
279 }
280
281 void onReset() noexcept override {
282 rGen_.reset();
283 }
284 };
285
286}
287
288

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.