Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
aegis2d::ecs::WaveSchedule Class Reference

Enemy waves from a data file: which entities appear, where and when. More...

#include <aegis2d/ecs/waves.hpp>

Public Member Functions

 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.
 
void restart () noexcept
 Goes back to the start of the schedule.
 
bool finished () const noexcept
 Tells whether every spawn has been listed.
 
const std::vector< WaveSpawn > & spawns () const noexcept
 Every spawn of the schedule.
 
std::size_t waves () const noexcept
 Number of waves in the file.
 
bool loops () const noexcept
 Tells whether the schedule starts again after its last spawn.
 
float duration () const noexcept
 Time of the last spawn.
 
float elapsed () const noexcept
 Time spent in the schedule (in the current loop).
 

Static Public Member Functions

static WaveSchedule from_config (const core::Config &config, std::span< const std::string_view > types={})
 Reads a schedule from a loaded configuration.
 
static WaveSchedule from_string (std::string_view text, std::span< const std::string_view > types={})
 Reads a schedule from JSON text.
 
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").
 

Detailed Description

Enemy waves from a data file: which entities appear, where and when.

The file describes waves that follow each other, so a level is changed without recompiling. Each wave starts delay seconds after the last spawn of the previous wave (after the start for the first wave) and lists groups of spawns:

{
"loop": false, // start again after the last wave (default false)
"waves": [
{
"delay": 2.0, // seconds before the wave (default 0)
"spawns": [
// 5 grunts in a column, one every 0.4 s, each 64 px lower
{"type": "grunt", "position": [1296, 104], "count": 5,
"every": 0.4, "step": [0, 64]},
{"type": "grunt", "position": [1296, 360], "at": 3.0}
]
}
]
}

A group needs a type and a position; at (seconds from the start of the wave), count (1), every (seconds between two members, 0) and step (offset between two members, [0, 0]) are optional. Comments are allowed. The engine knows no entity type: update() lists the spawns due and the game creates them by type; give the known types to the loader to reject a typo when the file is read rather than in the middle of a game. Spawns fall on the first tick at or after their time, so the schedule plays the same at any tick rate.

Constructor & Destructor Documentation

◆ WaveSchedule()

aegis2d::ecs::WaveSchedule::WaveSchedule ( )
default

Creates an empty schedule, finished from the start.

Member Function Documentation

◆ duration()

float aegis2d::ecs::WaveSchedule::duration ( ) const
inlinenoexcept

Time of the last spawn.

Returns
Seconds from the start; the length of one loop.

◆ elapsed()

float aegis2d::ecs::WaveSchedule::elapsed ( ) const
inlinenoexcept

Time spent in the schedule (in the current loop).

Returns
Seconds since the start.

◆ finished()

bool aegis2d::ecs::WaveSchedule::finished ( ) const
inlinenoexcept

Tells whether every spawn has been listed.

Returns
true once the last spawn is past; never for a looping schedule.

◆ from_config()

static WaveSchedule aegis2d::ecs::WaveSchedule::from_config ( const core::Config &  config,
std::span< const std::string_view >  types = {} 
)
inlinestatic

Reads a schedule from a loaded configuration.

Parameters
configDocument whose root has waves (and optionally loop).
typesEntity types the game can create; empty to accept any type.
Returns
The schedule, at its start.
Exceptions
core::ConfigErrorIf the document does not describe valid waves; the message starts with the path of the faulty value, e.g. waves.1.spawns.0.type: ....

◆ from_file()

static WaveSchedule aegis2d::ecs::WaveSchedule::from_file ( const std::filesystem::path &  path,
std::span< const std::string_view >  types = {} 
)
inlinestatic

Reads a schedule from a JSON file, e.g. core::asset_path("waves/stage1.json").

Parameters
pathFile to read.
typesEntity types the game can create; empty to accept any type.
Returns
The schedule, at its start.
Exceptions
core::ConfigErrorIf the file cannot be read or does not describe valid waves; the message starts with the file path.

◆ from_string()

static WaveSchedule aegis2d::ecs::WaveSchedule::from_string ( std::string_view  text,
std::span< const std::string_view >  types = {} 
)
inlinestatic

Reads a schedule from JSON text.

Parameters
textJSON document.
typesEntity types the game can create; empty to accept any type.
Returns
The schedule, at its start.
Exceptions
core::ConfigErrorIf text is not valid JSON or not valid waves.

◆ loops()

bool aegis2d::ecs::WaveSchedule::loops ( ) const
inlinenoexcept

Tells whether the schedule starts again after its last spawn.

Returns
The loop value of the file.

◆ restart()

void aegis2d::ecs::WaveSchedule::restart ( )
inlinenoexcept

Goes back to the start of the schedule.

◆ spawns()

const std::vector< WaveSpawn > & aegis2d::ecs::WaveSchedule::spawns ( ) const
inlinenoexcept

Every spawn of the schedule.

Returns
The spawns of one loop, in time order.

◆ update()

void aegis2d::ecs::WaveSchedule::update ( const Tick &  tick,
std::vector< WaveSpawn > &  due 
)
inline

Moves time forward by one tick and lists the spawns that fall in it.

Spawns come in time order. A looping schedule starts again after its last spawn, the first wave waiting its delay again.

Parameters
tickStep of the simulation.
dueCleared, then filled with the spawns due this tick.

◆ waves()

std::size_t aegis2d::ecs::WaveSchedule::waves ( ) const
inlinenoexcept

Number of waves in the file.

Returns
At least 1 for a loaded schedule, 0 for an empty one.

The documentation for this class was generated from the following file: