The enemy types of a directory of Lua scripts, and the behaviors of their enemies.
More...
|
| | EnemyScripts (aegis2d::ecs::Registry ®istry, 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.
|
| |
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.
| 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
-
- Exceptions
-
| std::invalid_argument | If no script gives type. |
| aegis2d::core::ConfigError | If the script is wrong, as for the constructor. |
| 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
-
| type | Name of the type. |
| position | Where it appears. |
| random | Number from 0 to 1 the script gets as self.random. |
| shot_delay | Seconds 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_argument | If no script gives type. |