R-Type
Networked shoot'em up built on Aegis2D
Loading...
Searching...
No Matches
enemy_scripts.hpp
Go to the documentation of this file.
1
3#pragma once
4#include <aegis2d/core/hot_reload.hpp>
5#include <aegis2d/core/math.hpp>
6#include <aegis2d/ecs/entity.hpp>
7#include <aegis2d/ecs/registry.hpp>
8#include <aegis2d/script/state.hpp>
9
10#include <cstdint>
11#include <filesystem>
12#include <string>
13#include <string_view>
14#include <vector>
15
16namespace rtype {
17
19struct EnemyType {
21 std::string name;
23 aegis2d::Vec2 size{32.0F, 32.0F};
25 std::int32_t health = 1;
27 std::uint32_t score = 100;
29 float fire_every = 0.0F;
31 float first_shot = 1.0F;
32};
33
37[[nodiscard]] std::filesystem::path default_enemies_directory();
38
43[[nodiscard]] std::vector<std::string>
44enemy_types(const std::filesystem::path& directory = default_enemies_directory());
45
58public:
65 explicit EnemyScripts(aegis2d::ecs::Registry& registry,
66 const std::filesystem::path& directory = default_enemies_directory());
67
70 [[nodiscard]] const std::vector<EnemyType>& types() const noexcept;
71
75 [[nodiscard]] const EnemyType* find(std::string_view name) const noexcept;
76
89 aegis2d::ecs::Entity spawn(std::string_view type, aegis2d::Vec2 position, float random = 0.0F,
90 float shot_delay = 0.0F);
91
100 void reload(std::string_view type);
101
105 void watch(aegis2d::core::HotReload& hot);
106
109 void stop(aegis2d::ecs::Entity enemy);
110
113 void update(float seconds);
114
117 [[nodiscard]] aegis2d::script::ScriptState& lua() noexcept;
118
119private:
120 aegis2d::ecs::Registry* registry_;
121 std::filesystem::path directory_;
122 aegis2d::script::ScriptState lua_;
123 std::vector<EnemyType> types_;
124};
125
126} // namespace rtype
The enemy types of a directory of Lua scripts, and the behaviors of their enemies.
Definition enemy_scripts.hpp:57
void reload(std::string_view type)
Reads the script of a type again, as when it changed on disk.
const std::vector< EnemyType > & types() const noexcept
The types loaded.
aegis2d::script::ScriptState & lua() noexcept
State of the scripts.
void watch(aegis2d::core::HotReload &hot)
Reloads the script of each type when its file changes.
const EnemyType * find(std::string_view name) const noexcept
Finds a type by name.
void stop(aegis2d::ecs::Entity enemy)
Stops running the behavior of an enemy: it keeps its components and its speed.
EnemyScripts(aegis2d::ecs::Registry &registry, const std::filesystem::path &directory=default_enemies_directory())
Loads every .lua file of a directory.
void update(float seconds)
Runs the update of every enemy alive, and forgets the others.
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.
R-Type game code, built on the Aegis2D engine.
Definition enemy_scripts.hpp:16
std::vector< std::string > enemy_types(const std::filesystem::path &directory=default_enemies_directory())
The enemy types of a directory, as waves files name them.
std::filesystem::path default_enemies_directory()
Directory of the enemy scripts the server plays.
Values of an enemy type, as its script gives them to enemy { ... }.
Definition enemy_scripts.hpp:19
std::string name
Name of the type: its file name without .lua, as waves files name it.
Definition enemy_scripts.hpp:21
float fire_every
Seconds between two shots (fire_every), or 0, the default, to never fire.
Definition enemy_scripts.hpp:29
float first_shot
Seconds before its first shot (first_shot, 1 by default).
Definition enemy_scripts.hpp:31
std::uint32_t score
Points for its killer (score, 100 by default).
Definition enemy_scripts.hpp:27
std::int32_t health
Health points (health, 1 by default).
Definition enemy_scripts.hpp:25
aegis2d::Vec2 size
Size on screen, also its hit box (width and height, 32 by default).
Definition enemy_scripts.hpp:23