Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
spawner.hpp
Go to the documentation of this file.
1
3#pragma once
7
8#include <cstdint>
9#include <stdexcept>
10#include <vector>
11
12namespace aegis2d::ecs {
13
24 float min_interval = 1.0F;
26 float max_interval = 1.0F;
27};
28
38public:
44 RandomSpawner(const SpawnSettings& settings, std::uint64_t seed)
45 : settings_{settings}, random_{seed} {
47 throw std::invalid_argument{"aegis2d: spawn intervals must be positive and ordered"};
48 }
49 until_next_ = draw_interval();
50 }
51
57 void update(const Tick& tick, std::vector<Vec2>& spawns) {
58 spawns.clear();
59 until_next_ -= tick.delta();
60 while (until_next_ <= 0.0F) {
61 spawns.push_back(draw_point());
62 until_next_ += draw_interval();
63 }
64 }
65
68 [[nodiscard]] core::Random& random() noexcept { return random_; }
69
72 [[nodiscard]] const SpawnSettings& settings() const noexcept { return settings_; }
73
76 [[nodiscard]] float until_next() const noexcept { return until_next_; }
77
78private:
79 Vec2 draw_point() noexcept {
80 return settings_.from + ((settings_.to - settings_.from) * random_.uniform());
81 }
82
83 float draw_interval() noexcept {
84 return random_.uniform(settings_.min_interval, settings_.max_interval);
85 }
86
87 SpawnSettings settings_;
88 core::Random random_;
89 float until_next_ = 0.0F;
90};
91
92} // namespace aegis2d::ecs
Seeded pseudo-random generator (PCG32), for gameplay the server must replay.
Definition random.hpp:16
constexpr float uniform() noexcept
Real number between 0 and 1.
Definition random.hpp:75
Decides when and where entities spawn, the same way for the same seed.
Definition spawner.hpp:37
const SpawnSettings & settings() const noexcept
Spawn line and intervals.
Definition spawner.hpp:72
RandomSpawner(const SpawnSettings &settings, std::uint64_t seed)
Creates a spawner whose first spawn is one random interval away.
Definition spawner.hpp:44
float until_next() const noexcept
Time left before the next spawn.
Definition spawner.hpp:76
void update(const Tick &tick, std::vector< Vec2 > &spawns)
Moves time forward by one tick and lists the spawns that fall in it.
Definition spawner.hpp:57
core::Random & random() noexcept
Random numbers of the spawner, to decide more about the spawned entities.
Definition spawner.hpp:68
Vector and matrix types of the engine (GLM behind engine names).
Entity-Component-System used by client and server.
Definition animation.hpp:13
glm::vec2 Vec2
2D vector of floats: positions, velocities, sizes (glm::vec2).
Definition math.hpp:19
Seeded random numbers that are the same on every platform.
Where and how often a RandomSpawner makes entities appear.
Definition spawner.hpp:18
float min_interval
Shortest time between two spawns, in seconds; must be above 0.
Definition spawner.hpp:24
Vec2 from
One end of the spawn line, in logical pixels.
Definition spawner.hpp:20
float max_interval
Longest time between two spawns, in seconds; at least min_interval.
Definition spawner.hpp:26
Vec2 to
Other end of the spawn line; equal to from for a single spawn point.
Definition spawner.hpp:22
One simulation step, as the systems see it.
Definition system.hpp:23
constexpr float delta() const noexcept
Duration of the step in seconds, to scale values given per second.
Definition system.hpp:32
Systems and the scheduler that updates them in order, once per tick.