Skip to main content

DefaultSpawnScheduler.ixx File

Default scheduler that evaluates all spawn rules each frame. 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...

namespacescheduling

Spawn scheduling and plan management. More...

Classes Index

classDefaultSpawnScheduler

Scheduler that evaluates spawn rules and produces spawn plans. More...

Description

Default scheduler that evaluates all spawn rules each frame.

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file DefaultSpawnScheduler.ixx
3 * @brief Default scheduler that evaluates all spawn rules each frame.
4 */
5module;
6
7#include <cassert>
8#include <memory>
9#include <queue>
10#include <ranges>
11#include <unordered_map>
12#include <vector>
13#include <algorithm>
14
15export module helios.engine.runtime.spawn.scheduling.DefaultSpawnScheduler;
16
17import helios.engine.runtime.spawn.scheduling.SpawnScheduler;
18import helios.engine.runtime.spawn.scheduling.DefaultRuleProcessor;
19
20import helios.engine.runtime.world.UpdateContext;
21import helios.engine.runtime.spawn.SpawnManager;
22import helios.engine.runtime.spawn.types.SpawnContext;
23import helios.engine.runtime.world.GameWorld;
24import helios.engine.runtime.spawn.scheduling.SpawnPlan;
25import helios.engine.runtime.spawn.scheduling.ScheduledSpawnPlan;
26import helios.engine.runtime.spawn.types;
27import helios.engine.runtime.spawn.policy.SpawnRule;
28import helios.engine.runtime.spawn.policy.SpawnRuleState;
29import helios.engine.runtime.pooling.GameObjectPoolManager;
30
34
35
36 /**
37 * @brief Scheduler that evaluates spawn rules and produces spawn plans.
38 *
39 * @details SpawnScheduler is the central coordinator for rule-based spawning.
40 * It maintains a collection of spawn rules, evaluates them each frame, and
41 * produces ScheduledSpawnPlan instances for rules whose conditions are met.
42 *
43 * ## Workflow
44 *
45 * 1 **Registration:** Rules are added via `addRule()` with their profile ID
46 * 2 **Evaluation:** `evaluate()` is called each frame by GameObjectSpawnSystem
47 * 3 **Scheduling:** Rules that pass conditions produce ScheduledSpawnPlans
48 * 4 **Draining:** `drainScheduledPlans()` returns and clears pending plans
49 * 5 **Commit:** `commit()` is called when spawns complete to update state
50 *
51 * Example:
52 * ```cpp
53 * SpawnScheduler scheduler;
54 * scheduler.addRule(enemyProfileId, std::make_unique<TimerSpawnRule>(
55 * ruleId, 2.0f, 3 // Every 2s, spawn 3
56 * ));
57 *
58 * // In game loop
59 * scheduler.evaluate(updateContext);
60 * auto plans = scheduler.drainScheduledPlans();
61 * ```
62 *
63 * @see SpawnRule
64 * @see ScheduledSpawnPlan
65 * @see GameObjectSpawnSystem
66 */
68
69 protected:
70
71 /**
72 * @brief Map from spawn profile IDs to their spawn rules.
73 */
75 std::unique_ptr<helios::engine::runtime::spawn::policy::SpawnRule>>
77
78 /**
79 * @brief Map from spawn rule IDs to their runtime state.
80 */
81 std::unordered_map<
85
86 /**
87 * @brief Processor for evaluating individual rules.
88 */
90
91 public:
92
93 /**
94 * @brief Constructs a DefaultSpawnScheduler with optional initial capacity.
95 *
96 * @param initialSpanPlanSize Initial capacity for the spawn plan buffer.
97 */
98 DefaultSpawnScheduler(const size_t initialSpanPlanSize = 20) {
99 scheduledSpawnPlans_.reserve(initialSpanPlanSize);
100 }
101
102 /**
103 * @brief Evaluates all registered spawn rules.
104 *
105 * @details Iterates through all rules, processes each one, and
106 * collects spawn plans for rules whose conditions are met.
107 *
108 * @param gameWorld The game world where evaluation takes place.
109 * @param updateContext Current frame context with delta time and world.
110 * @param spawnContext Context for spawn operations.
111 */
113 const GameWorld& gameWorld,
114 const UpdateContext& updateContext,
115 const SpawnContext& spawnContext) noexcept override{
116
118
119 for (auto& [spawnProfileId, rule] : spawnRules_) {
120
121 auto spawnPlan = ruleProcessor_.processRule(
122 gameWorld, updateContext, spawnContext, spawnProfileId, *rule,
123 spawnRuleStates_[rule->spawnRuleId()]
124 );
125
126 if (spawnPlan.amount > 0) {
127 scheduledSpawnPlans_.push_back({
128 spawnProfileId,
129 std::move(spawnPlan),
130 spawnContext
131 });
132 }
133 }
134 }
135
136 /**
137 * @brief Adds a spawn rule for a profile.
138 *
139 * @param spawnProfileId The profile ID to associate with the rule.
140 * @param spawnRule The spawn rule to add. Ownership transferred.
141 *
142 * @return Reference to this scheduler for chaining.
143 *
144 * @pre No rule is already registered for this profile ID.
145 */
148 std::unique_ptr<helios::engine::runtime::spawn::policy::SpawnRule> spawnRule
149 ) {
150 assert(!spawnRules_.contains(spawnProfileId) && "Duplicate SpawnProfile entry");
151 assert(!spawnRuleStates_.contains(spawnRule->spawnRuleId()) && "Duplicate SpawnRuleId entry");
152
153 spawnRuleStates_.try_emplace(spawnRule->spawnRuleId());
154 spawnRules_.try_emplace(spawnProfileId, std::move(spawnRule));
155
156
157 return *this;
158 }
159
160 /**
161 * @brief Commits a completed spawn to update rule state.
162 *
163 * @details Called after entities are spawned to update the rule's
164 * internal state (e.g., spawn count tracking, timer reset).
165 *
166 * @param spawnRuleId The rule that triggered the spawn.
167 * @param spawnCount Number of entities actually spawned.
168 */
169 void commit(const helios::engine::runtime::spawn::types::SpawnRuleId spawnRuleId, const size_t spawnCount) noexcept override{
170
171 for (auto& spawnRule : spawnRules_ | std::views::values) {
172
173 if (spawnRule->spawnRuleId() == spawnRuleId) {
174
175 auto it = spawnRuleStates_.find(spawnRuleId);
176 assert(it != spawnRuleStates_.end() && "Unexpected missing spawnRuleState");
177
178 spawnRule->commit(it->second, spawnCount);
179 }
180 }
181 }
182
183 /**
184 * @brief Resets all rule states to their initial values.
185 *
186 * @details Iterates through all rules and resets their state.
187 */
188 void reset() noexcept override {
189 for (auto& spawnRule : spawnRules_ | std::views::values) {
190
191 auto it = spawnRuleStates_.find(spawnRule->spawnRuleId());
192 assert(it != spawnRuleStates_.end() && "Unexpected missing spawnRuleState");
193
194 spawnRule->reset(it->second);
195
196 }
197 }
198
199 };
200
201}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.