R-Type
Networked shoot'em up built on Aegis2D
Loading...
Searching...
No Matches
rtype::EnemyScripts Class Reference

The enemy types of a directory of Lua scripts, and the behaviors of their enemies. More...

#include <rtype/enemy_scripts.hpp>

Public Member Functions

 EnemyScripts (aegis2d::ecs::Registry &registry, const std::filesystem::path &directory=default_enemies_directory())
 Loads every .lua file of a directory.
 
const std::vector< EnemyType > & types () const noexcept
 The types loaded.
 
const EnemyType * find (std::string_view name) const noexcept
 Finds a type by name.
 
aegis2d::ecs::Entity spawn (std::string_view type, aegis2d::Vec2 position, float random=0.0F, float shot_delay=0.0F)
 Creates an enemy and runs the spawn of its type.
 
void reload (std::string_view type)
 Reads the script of a type again, as when it changed on disk.
 
void watch (aegis2d::core::HotReload &hot)
 Reloads the script of each type when its file changes.
 
void stop (aegis2d::ecs::Entity enemy)
 Stops running the behavior of an enemy: it keeps its components and its speed.
 
void update (float seconds)
 Runs the update of every enemy alive, and forgets the others.
 
aegis2d::script::ScriptState & lua () noexcept
 State of the scripts.
 

Detailed Description

The enemy types of a directory of Lua scripts, and the behaviors of their enemies.

Each <type>.lua file describes one type with enemy { ... }: its values (see EnemyType) and two optional functions, spawn(self) when an enemy appears and update(self, dt) at each tick. self is the enemy's own table: entity (its id for the ecs functions), x and y where it appeared, random, a number from 0 to 1 the game draws for it, and age, its seconds alive; the script may keep its own fields there. The scripts share one sandboxed state, with ecs (script::bind_components()) and events. A behavior that fails, by an error or by taking the whole budget of the state, is logged once and stopped: an update stops moving the enemies of its type, which keep their speed, and the enemies of a type whose spawn failed are not created. Not thread-safe, as the registry.

Constructor & Destructor Documentation

◆ EnemyScripts()

rtype::EnemyScripts::EnemyScripts ( aegis2d::ecs::Registry &  registry,
const std::filesystem::path &  directory = default_enemies_directory() 
)
explicit

Loads every .lua file of a directory.

Parameters
registryWorld the enemies live in, kept by reference.
directoryDirectory of the scripts.
Exceptions
aegis2d::core::ConfigErrorIf a file name is not a type name (lowercase letters, digits and _), a script fails, does not call enemy { ... } or gives a wrong value; the message names the file, and the line of a script error.

Member Function Documentation

◆ find()

const EnemyType * rtype::EnemyScripts::find ( std::string_view  name) const
noexcept

Finds a type by name.

Parameters
nameName of the type.
Returns
The type, or nullptr if no script gives it.

◆ lua()

aegis2d::script::ScriptState & rtype::EnemyScripts::lua ( )
noexcept

State of the scripts.

Returns
The state.

◆ reload()

void rtype::EnemyScripts::reload ( std::string_view  type)

Reads the script of a type again, as when it changed on disk.

The enemies of the type alive run the new update from the next tick on, with the fields their self holds; the new values (size, health...) are for the enemies spawned from now on. A behavior stopped by a failure runs again if the new script gives it again. A wrong script is refused and the type keeps the one it had.

Parameters
typeName of the type.
Exceptions
std::invalid_argumentIf no script gives type.
aegis2d::core::ConfigErrorIf the script is wrong, as for the constructor.

◆ spawn()

aegis2d::ecs::Entity rtype::EnemyScripts::spawn ( std::string_view  type,
aegis2d::Vec2  position,
float  random = 0.0F,
float  shot_delay = 0.0F 
)

Creates an enemy and runs the spawn of its type.

The enemy flies nowhere until its script moves it: it has a Transform, a Velocity of zero, a Sprite and a Collider of its type's size on the enemy layer, its Health, a ScoreValue, the ramming Damage, an OffscreenDespawn, a NetworkId and, for a type that fires, a shmup::Weapon with its shmup::AutoTrigger.

Parameters
typeName of the type.
positionWhere it appears.
randomNumber from 0 to 1 the script gets as self.random.
shot_delaySeconds added to the type's first_shot.
Returns
The enemy, or a null handle if the type's spawn failed, now or before (the enemy is destroyed: it would stand still).
Exceptions
std::invalid_argumentIf no script gives type.

◆ stop()

void rtype::EnemyScripts::stop ( aegis2d::ecs::Entity  enemy)

Stops running the behavior of an enemy: it keeps its components and its speed.

Parameters
enemyEnemy created by spawn().

◆ types()

const std::vector< EnemyType > & rtype::EnemyScripts::types ( ) const
noexcept

The types loaded.

Returns
One per script, sorted by name.

◆ update()

void rtype::EnemyScripts::update ( float  seconds)

Runs the update of every enemy alive, and forgets the others.

Parameters
secondsDuration of the tick.

◆ watch()

void rtype::EnemyScripts::watch ( aegis2d::core::HotReload &  hot)

Reloads the script of each type when its file changes.

The scripts must not move afterwards: hot calls reload() on them.

Parameters
hotWatcher, whose update() reloads; a reload that fails is logged by it.

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