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

Shoot'em up building blocks on top of the ECS: player ships, their controls, weapons and their shots. A game of another genre leaves the module out. More...

Classes

struct  AutoTrigger
 Weapon that fires on its own, as soon as it is reloaded (enemies). More...
 
struct  PlayerShip
 Ship steered by a player's input (ecs::PlayerInput). More...
 
class  ShipControls
 The controls of a player ship: arrows move it and Space fires, or a gamepad's D-pad or left stick and its South button (A, Cross) or right trigger. More...
 
struct  ShotFired
 An entity fired: one shot leaves it during the tick. More...
 
struct  Weapon
 Gun of a ship: how often it can fire. More...
 

Functions

AEGIS2D_SHMUP_EXPORT std::string_view name () noexcept
 Name of the module, used to check that it is linked.
 
void steer_player_ships (ecs::Registry &registry, const ecs::Tick &tick)
 Sets the velocity of every player ship from its input.
 
void clamp_player_ships (ecs::Registry &registry, const ecs::Aabb &area)
 Keeps every player ship inside the play area.
 
ecs::Entity spawn_player_ship (ecs::Registry &registry, Vec2 position, float speed=PlayerShip{}.speed)
 Creates a player ship ready to be steered.
 
bool pull_trigger (Weapon &weapon, float delta, bool pulled) noexcept
 Counts a weapon's reload down by one tick and fires it if the trigger is pulled.
 
void trigger_weapons (ecs::Registry &registry, const ecs::Tick &tick, std::vector< ecs::Entity > &shooters)
 Finds the ships that fire during this tick and reloads their weapons.
 
void trigger_auto_weapons (ecs::Registry &registry, const ecs::Tick &tick, std::vector< ecs::Entity > &shooters)
 Finds the entities that fire on their own during this tick and reloads them.
 
void publish_shots (core::EventBus &events, const ecs::Registry &registry, std::span< const ecs::Entity > shooters)
 Publishes a ShotFired for each entity that fires during the tick.
 

Detailed Description

Shoot'em up building blocks on top of the ECS: player ships, their controls, weapons and their shots. A game of another genre leaves the module out.

Function Documentation

◆ clamp_player_ships()

void aegis2d::shmup::clamp_player_ships ( ecs::Registry &  registry,
const ecs::Aabb &  area 
)
inline

Keeps every player ship inside the play area.

Each entity with PlayerShip and ecs::Transform is moved back so that its box (see ecs::half_extent_of()) fits in area: holding a direction against an edge stops the ship there, and it can leave the edge at once in the other direction. Run it after ecs::apply_velocity() and before the collisions, e.g. systems.add("clamp", [area](ecs::Registry& r, const ecs::Tick&) { shmup::clamp_player_ships(r, area); });. Defined in the header because it reads components.

Parameters
registryWorld holding the ships.
areaPlay area, usually {0, 0} to the logical resolution (#85).

◆ name()

AEGIS2D_SHMUP_EXPORT std::string_view aegis2d::shmup::name ( )
noexcept

Name of the module, used to check that it is linked.

Returns
"shmup", a view of a static string.

◆ publish_shots()

void aegis2d::shmup::publish_shots ( core::EventBus &  events,
const ecs::Registry &  registry,
std::span< const ecs::Entity >  shooters 
)
inline

Publishes a ShotFired for each entity that fires during the tick.

Call it after trigger_weapons() or trigger_auto_weapons(), with their shooters, in their order. Defined in the header, like the systems, because it reads components (see ecs::CollisionGrid::build()) and publishes in the caller's code (the bus finds its handlers by typeid).

Parameters
eventsBus to publish on.
registryWorld holding the shooters.
shootersEntities that fire this tick.

◆ pull_trigger()

bool aegis2d::shmup::pull_trigger ( Weapon &  weapon,
float  delta,
bool  pulled 
)
inlinenoexcept

Counts a weapon's reload down by one tick and fires it if the trigger is pulled.

The reload goes down by delta. If pulled and the weapon is ready, it fires and the cooldown is added to the reload; the time left over is kept, so the rate is exact over many ticks even when the cooldown is not a whole number of ticks. A weapon that stays idle does not store shots: its reload stops at zero.

Parameters
weaponWeapon to update.
deltaDuration of the tick, in seconds.
pulledtrue if the weapon should fire when ready.
Returns
true if it fires during this tick.

◆ spawn_player_ship()

ecs::Entity aegis2d::shmup::spawn_player_ship ( ecs::Registry &  registry,
Vec2  position,
float  speed = PlayerShip{}.speed 
)
inline

Creates a player ship ready to be steered.

The ship gets an ecs::Transform at position, a zero ecs::Velocity, an empty ecs::PlayerInput and a PlayerShip with speed. The game adds what depends on it: Collider layers, Health, Lives, Score, Sprite.

Parameters
registryWorld to create it in.
positionCenter of the ship, in logical pixels.
speedSpeed in logical pixels per second.
Returns
The new ship.

◆ steer_player_ships()

void aegis2d::shmup::steer_player_ships ( ecs::Registry &  registry,
const ecs::Tick &  tick 
)
inline

Sets the velocity of every player ship from its input.

For each entity with PlayerShip, ecs::PlayerInput and ecs::Velocity, the velocity becomes ecs::direction_of(input) * speed: the ship moves while a direction is held and stops as soon as it is released. Runs on the server, which owns the simulation; the signature lets it be added to a Scheduler as is. Defined in the header because it reads components (see ecs::CollisionGrid::build()).

Parameters
registryWorld holding the ships.
tickUnused: the speed is per second, applied by ecs::apply_velocity().

◆ trigger_auto_weapons()

void aegis2d::shmup::trigger_auto_weapons ( ecs::Registry &  registry,
const ecs::Tick &  tick,
std::vector< ecs::Entity > &  shooters 
)
inline

Finds the entities that fire on their own during this tick and reloads them.

For each entity with Weapon and AutoTrigger but no PlayerInput, pull_trigger() counts the reload down and fires as soon as the weapon is ready: the entity shoots every cooldown seconds, the first time when its first reload runs out. The game then creates one missile per shooter, e.g. in front of an enemy. Run it next to trigger_weapons(), e.g. systems.add("enemy_fire", [&shooters](ecs::Registry& r, const ecs::Tick& t) { shmup::trigger_auto_weapons(r, t, shooters); });. Defined in the header because it reads components.

Parameters
registryWorld holding the weapons.
tickStep of the simulation.
shootersCleared, then filled with the entities that fire this tick.

◆ trigger_weapons()

void aegis2d::shmup::trigger_weapons ( ecs::Registry &  registry,
const ecs::Tick &  tick,
std::vector< ecs::Entity > &  shooters 
)
inline

Finds the ships that fire during this tick and reloads their weapons.

For each entity with Weapon and PlayerInput, pull_trigger() counts the reload down and fires when Action::Fire is held and the weapon is ready; the entity is then added to shooters. The game then creates one missile per shooter (its spawn rules are its own). Defined in the header because it reads components (see ecs::CollisionGrid::build()).

Parameters
registryWorld holding the weapons.
tickStep of the simulation.
shootersCleared, then filled with the entities that fire this tick.