Skip to main content

GameObjectSpawnSystem.ixx File

System for processing spawn schedules and enqueuing spawn commands. More...

Included Headers

Namespaces Index

namespacehelios
namespaceengine

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

namespacemechanics

High-level gameplay systems and components for game logic. More...

namespacespawn

Entity spawning and lifecycle management. More...

namespacesystems

Systems for spawn lifecycle management. More...

Classes Index

classGameObjectSpawnSystem

System that evaluates spawn conditions and enqueues spawn commands. More...

Description

System for processing spawn schedules and enqueuing spawn commands.

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file GameObjectSpawnSystem.ixx
3 * @brief System for processing spawn schedules and enqueuing spawn commands.
4 */
5module;
6
7#include <cassert>
8#include <memory>
9#include <vector>
10
11export module helios.engine.mechanics.spawn.systems.GameObjectSpawnSystem;
12
13import helios.engine.ecs.System;
14import helios.engine.runtime.messaging.command.CommandBuffer;
15import helios.engine.runtime.world.UpdateContext;
16import helios.engine.runtime.spawn.commands.ScheduledSpawnPlanCommand;
17import helios.engine.runtime.spawn.scheduling.SpawnScheduler;
18import helios.engine.runtime.spawn.events.SpawnPlanCommandExecutedEvent;
19
21
22 /**
23 * @brief System that evaluates spawn conditions and enqueues spawn commands.
24 *
25 * @details GameObjectSpawnSystem bridges the spawn scheduling logic with the
26 * command-based spawning pipeline. It manages multiple SpawnScheduler instances
27 * and performs the following each frame:
28 *
29 * 1 **Read Frame Events:** Consumes `SpawnPlanCommandExecutedEvent` from the
30 * previous frame to commit completed spawn operations to all schedulers.
31 *
32 * 2 **Evaluate Conditions:** Calls `SpawnScheduler::evaluate()` on each
33 * scheduler to check all registered spawn rules against their conditions.
34 *
35 * 3 **Drain Scheduled Plans:** Retrieves all plans that are ready to execute
36 * and enqueues them as `ScheduledSpawnPlanCommand` into the CommandBuffer.
37 *
38 * The actual spawning is performed by the command dispatcher/manager layer,
39 * keeping this system focused on scheduling logic only.
40 *
41 * Example setup:
42 * ```cpp
43 * std::vector<std::unique_ptr<SpawnScheduler>> schedulers;
44 *
45 * auto enemyScheduler = std::make_unique<SpawnScheduler>();
46 * enemyScheduler->addRule(enemyProfileId, std::move(enemyRule));
47 * schedulers.push_back(std::move(enemyScheduler));
48 *
49 * auto powerupScheduler = std::make_unique<SpawnScheduler>();
50 * powerupScheduler->addRule(powerupProfileId, std::move(powerupRule));
51 * schedulers.push_back(std::move(powerupScheduler));
52 *
53 * auto spawnSystem = std::make_unique<GameObjectSpawnSystem>(schedulers);
54 * mainPhase.addPass().add(std::move(spawnSystem));
55 * ```
56 *
57 * @note This system reads frame-level events via `readFrame()`, meaning it
58 * processes spawn confirmations from the previous frame.
59 *
60 * @see SpawnScheduler
61 * @see ScheduledSpawnPlanCommand
62 * @see SpawnPlanCommandExecutedEvent
63 */
65
66 /**
67 * @brief Collection of schedulers that manage spawn rules and conditions.
68 *
69 * @details Each scheduler owns and evaluates its registered spawn rules
70 * independently. When conditions are met, the scheduler produces
71 * ScheduledSpawnPlan instances for execution. Multiple schedulers allow
72 * grouping spawn rules by category (e.g., enemies, powerups, projectiles).
73 */
74 std::vector<std::unique_ptr<helios::engine::runtime::spawn::scheduling::SpawnScheduler>> spawnSchedulers_;
75
76 public:
77
78 /**
79 * @brief Constructs a GameObjectSpawnSystem with the given schedulers.
80 *
81 * @param spawnSchedulers Vector of schedulers managing spawn rules.
82 * Ownership of all schedulers is transferred to this system.
83 */
85 std::vector<std::unique_ptr<helios::engine::runtime::spawn::scheduling::SpawnScheduler>> spawnSchedulers
86 ) :
87 spawnSchedulers_(std::move(spawnSchedulers)) {}
88
89 /**
90 * @brief Processes spawn scheduling and enqueues spawn commands.
91 *
92 * @details Iterates through all schedulers and performs these steps for each:
93 * 1 Reads `SpawnPlanCommandExecutedEvent` from frame event bus
94 * 2 Commits completed spawns to the scheduler (for tracking/cooldowns)
95 * 3 Evaluates all spawn rules against current conditions
96 * 4 Drains ready spawn plans and enqueues them as commands
97 *
98 * @param updateContext The current frame's update context.
99 */
100 void update(helios::engine::runtime::world::UpdateContext& updateContext) noexcept override {
101
102 const auto& events = updateContext.readFrame<
104 >();
105
106 for (const auto& spawnScheduler : spawnSchedulers_) {
107
108 /**
109 * @todo this should be processed before iterating over all schedulers: A scheduler owns the rule,
110 * hence a rule that triggered the event's can be associated with the owning Scheduler, making it
111 * unneccesary to iterate over already processed events
112 */
113 for (const auto& event : events) {
114 spawnScheduler->commit(event.spawnRuleId, event.spawnCount);
115 }
116
117 spawnScheduler->evaluate(updateContext);
118
119 auto scheduledPlans = spawnScheduler->drainScheduledPlans();
120
121 for (auto& plan : scheduledPlans) {
122 updateContext.commandBuffer().add<
124 >(
125 plan.spawnProfileId, plan.spawnPlan, plan.spawnContext
126 );
127 }
128 }
129
130 }
131
132 };
133
134}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.