35[[nodiscard]]
inline std::string
at_index(std::string_view parent, std::size_t index) {
36 return std::string{parent} +
'.' + std::to_string(index);
44 const auto value = config.
number(path);
45 if (!value || !std::isfinite(*value) || *value < 0.0) {
48 return static_cast<float>(*value);
52 const auto x = config.
number(path +
".0");
53 const auto y = config.
number(path +
".1");
54 if (!x || !y || config.
contains(path +
".2") || !std::isfinite(*x) || !std::isfinite(*y)) {
57 return Vec2{
static_cast<float>(*x),
static_cast<float>(*y)};
103 std::span<const std::string_view> types = {}) {
105 float previous_end = 0.0F;
109 const std::string groups = path +
".spawns";
110 if (!config.
contains(groups +
".0")) {
115 end = std::max(end, schedule.add_group(config,
detail::at_index(groups, group),
116 wave, begin, types));
121 if (schedule.waves_ == 0) {
122 throw core::ConfigError{
"waves: needs a list of at least one wave"};
125 schedule.duration_ = schedule.spawns_.back().time;
127 const auto loop = config.
boolean(
"loop");
129 throw core::ConfigError{
"loop: must be true or false"};
131 schedule.loop_ = *loop;
133 if (schedule.loop_ && !(schedule.duration_ > 0.0F)) {
134 throw core::ConfigError{
"loop: the waves must last more than 0 s to loop"};
145 std::span<const std::string_view> types = {}) {
156 std::span<const std::string_view> types = {}) {
172 elapsed_ += tick.
delta();
174 while (next_ < spawns_.size() && spawns_.at(next_).time <= elapsed_) {
175 due.push_back(spawns_.at(next_));
178 if (!loop_ || next_ < spawns_.size()) {
181 elapsed_ -= duration_;
194 [[nodiscard]]
bool finished() const noexcept {
return !loop_ && next_ >= spawns_.size(); }
198 [[nodiscard]]
const std::vector<WaveSpawn>&
spawns() const noexcept {
return spawns_; }
202 [[nodiscard]] std::size_t
waves() const noexcept {
return waves_; }
206 [[nodiscard]]
bool loops() const noexcept {
return loop_; }
210 [[nodiscard]]
float duration() const noexcept {
return duration_; }
214 [[nodiscard]]
float elapsed() const noexcept {
return elapsed_; }
217 float add_group(
const core::Config& config,
const std::string& path, std::size_t wave,
218 float begin, std::span<const std::string_view> types) {
219 const auto type = config.
string(path +
".type");
220 if (!type || type->empty()) {
223 if (!types.empty() && std::ranges::find(types, std::string_view{*type}) == types.end()) {
225 for (
const std::string_view
name : types) {
226 known += known.empty() ?
"" :
", ";
229 throw core::ConfigError{path +
".type: unknown type \"" + *type +
230 "\" (known: " + known +
")"};
237 std::int64_t count = 1;
238 if (config.
contains(path +
".count")) {
239 const auto value = config.
integer(path +
".count");
240 if (!value || *value < 1 || *value > max_group) {
241 throw core::ConfigError{path +
".count: must be a whole number from 1 to " +
242 std::to_string(max_group)};
247 for (std::int64_t member = 0; member < count; ++member) {
248 const auto index =
static_cast<float>(member);
249 const float time = begin + at + (every * index);
250 spawns_.push_back(WaveSpawn{
251 .type = *type, .position = position + (step * index), .wave = wave, .time = time});
252 last = std::max(last, time);
257 static constexpr std::int64_t max_group = 1000;
259 std::vector<WaveSpawn> spawns_;
260 std::size_t waves_ = 0;
262 float duration_ = 0.0F;
263 float elapsed_ = 0.0F;
264 std::size_t next_ = 0;
Thrown when a configuration cannot be read or parsed.
Definition config.hpp:23
JSON configuration file (server port, window size...) read through typed getters.
Definition config.hpp:58
static Config from_file(const std::filesystem::path &path)
Reads and parses a JSON file.
std::optional< std::string > string(std::string_view path) const
Reads a string.
std::optional< bool > boolean(std::string_view path) const
Reads a boolean.
std::optional< std::int64_t > integer(std::string_view path) const
Reads an integer.
std::optional< double > number(std::string_view path) const
Reads a number, integer or not.
bool contains(std::string_view path) const
Tells whether a value exists at path, whatever its type.
static Config from_string(std::string_view text)
Parses JSON text.
Enemy waves from a data file: which entities appear, where and when.
Definition waves.hpp:91
static WaveSchedule from_string(std::string_view text, std::span< const std::string_view > types={})
Reads a schedule from JSON text.
Definition waves.hpp:144
static WaveSchedule from_config(const core::Config &config, std::span< const std::string_view > types={})
Reads a schedule from a loaded configuration.
Definition waves.hpp:102
float elapsed() const noexcept
Time spent in the schedule (in the current loop).
Definition waves.hpp:214
float duration() const noexcept
Time of the last spawn.
Definition waves.hpp:210
const std::vector< WaveSpawn > & spawns() const noexcept
Every spawn of the schedule.
Definition waves.hpp:198
WaveSchedule()=default
Creates an empty schedule, finished from the start.
void update(const Tick &tick, std::vector< WaveSpawn > &due)
Moves time forward by one tick and lists the spawns that fall in it.
Definition waves.hpp:170
bool loops() const noexcept
Tells whether the schedule starts again after its last spawn.
Definition waves.hpp:206
std::size_t waves() const noexcept
Number of waves in the file.
Definition waves.hpp:202
static WaveSchedule from_file(const std::filesystem::path &path, std::span< const std::string_view > types={})
Reads a schedule from a JSON file, e.g. core::asset_path("waves/stage1.json").
Definition waves.hpp:155
void restart() noexcept
Goes back to the start of the schedule.
Definition waves.hpp:187
bool finished() const noexcept
Tells whether every spawn has been listed.
Definition waves.hpp:194
JSON configuration with dotted-path lookups, which can be changed and saved.
Vector and matrix types of the engine (GLM behind engine names).
Vec2 vec2_at(const core::Config &config, const std::string &path)
Definition waves.hpp:51
float seconds_at(const core::Config &config, const std::string &path, float fallback)
Definition waves.hpp:39
std::string at_index(std::string_view parent, std::size_t index)
Definition waves.hpp:35
Entity-Component-System used by client and server.
Definition animation.hpp:13
std::string_view name() noexcept
Name of the module, used to check that it is linked.
glm::vec2 Vec2
2D vector of floats: positions, velocities, sizes (glm::vec2).
Definition math.hpp:19
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
One entity a WaveSchedule makes appear.
Definition waves.hpp:21
float time
When it appears, in seconds from the start of the schedule (of each loop).
Definition waves.hpp:30
std::size_t wave
Index of its wave in the file, from 0.
Definition waves.hpp:28
Vec2 position
Where it appears, in logical pixels.
Definition waves.hpp:26
std::string type
Name of the entity type, as written in the file (e.g. "grunt"); the game maps it to its creation func...
Definition waves.hpp:24
Systems and the scheduler that updates them in order, once per tick.