Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
system.hpp
Go to the documentation of this file.
1
3#pragma once
6#include <aegis2d/ecs/export.hpp>
8
9#include <chrono>
10#include <concepts>
11#include <cstddef>
12#include <cstdint>
13#include <functional>
14#include <memory>
15#include <string>
16#include <string_view>
17#include <utility>
18#include <vector>
19
20namespace aegis2d::ecs {
21
23struct Tick {
28 std::uint64_t index = 0;
29
32 [[nodiscard]] constexpr float delta() const noexcept {
33 return std::chrono::duration<float>{step}.count();
34 }
35};
36
42class AEGIS2D_ECS_EXPORT ISystem {
43public:
44 ISystem() = default;
45 ISystem(const ISystem&) = delete;
46 ISystem& operator=(const ISystem&) = delete;
47 ISystem(ISystem&&) = delete;
50 virtual ~ISystem();
51
55 virtual void update(Registry& registry, const Tick& tick) = 0;
56};
57
59class AEGIS2D_ECS_EXPORT FunctionSystem final : public ISystem {
60public:
62 using Function = std::function<void(Registry& registry, const Tick& tick)>;
63
67 explicit FunctionSystem(Function function);
68
72 void update(Registry& registry, const Tick& tick) override;
73
74private:
75 Function function_;
76};
77
100class AEGIS2D_ECS_EXPORT Scheduler {
101public:
102 Scheduler() = default;
103 Scheduler(const Scheduler&) = delete;
104 Scheduler& operator=(const Scheduler&) = delete;
105 Scheduler(Scheduler&&) noexcept = default;
106 Scheduler& operator=(Scheduler&&) noexcept = default;
107 ~Scheduler() = default;
108
116 template <std::derived_from<ISystem> System, typename... Args>
117 requires std::constructible_from<System, Args...>
118 System& add(std::string name, Args&&... args) {
119 auto system = std::make_unique<System>(std::forward<Args>(args)...);
120 System& added = *system;
121 add(std::move(name), std::move(system));
122 return added;
123 }
124
132 ISystem& add(std::string name, std::unique_ptr<ISystem> system);
133
141 FunctionSystem& add(std::string name, FunctionSystem::Function function);
142
147 bool remove(std::string_view name);
148
152 [[nodiscard]] bool contains(std::string_view name) const noexcept;
153
161 void set_enabled(std::string_view name, bool enabled);
162
167 [[nodiscard]] bool enabled(std::string_view name) const;
168
171 [[nodiscard]] std::vector<std::string_view> names() const;
172
175 [[nodiscard]] std::size_t size() const noexcept;
176
179 [[nodiscard]] bool empty() const noexcept;
180
188 void update(Registry& registry, core::Duration step);
189
193 void set_profiler(core::Profiler* profiler) noexcept;
194
197 [[nodiscard]] std::uint64_t ticks() const noexcept;
198
201 [[nodiscard]] bool updating() const noexcept;
202
203private:
204 struct Entry {
205 std::string name;
206 std::unique_ptr<ISystem> system;
207 bool enabled = true;
208 };
209
210 void check_not_updating(std::string_view action) const;
211 [[nodiscard]] std::vector<Entry>::iterator find(std::string_view name) noexcept;
212 [[nodiscard]] std::vector<Entry>::const_iterator find(std::string_view name) const noexcept;
213 [[nodiscard]] Entry& entry(std::string_view name);
214 [[nodiscard]] const Entry& entry(std::string_view name) const;
215
216 std::vector<Entry> systems_;
217 std::uint64_t ticks_ = 0;
218 core::Profiler* profiler_ = nullptr;
219 bool updating_ = false;
220};
221
222} // namespace aegis2d::ecs
Timers around the parts of a frame (its phases, each system of a ecs::Scheduler......
Definition profiler.hpp:92
System made of a function, for logic that keeps no state of its own.
Definition system.hpp:59
void update(Registry &registry, const Tick &tick) override
Calls the function.
std::function< void(Registry &registry, const Tick &tick)> Function
Signature of the function: the same as ISystem::update().
Definition system.hpp:62
FunctionSystem(Function function)
Wraps a function.
Game logic run on the registry once per tick (movement, collisions, spawns...).
Definition system.hpp:42
ISystem & operator=(const ISystem &)=delete
ISystem(ISystem &&)=delete
ISystem(const ISystem &)=delete
virtual ~ISystem()
Releases the system's own state.
virtual void update(Registry &registry, const Tick &tick)=0
Runs the system for one tick.
ISystem & operator=(ISystem &&)=delete
Entry point of the ECS: creates entities and stores their components.
Definition registry.hpp:34
Ordered list of named systems, all updated once per tick.
Definition system.hpp:100
std::size_t size() const noexcept
Number of systems, enabled or not.
FunctionSystem & add(std::string name, FunctionSystem::Function function)
Adds a function as a system after the others.
void set_enabled(std::string_view name, bool enabled)
Enables or disables a system; a disabled system keeps its place but is skipped by update().
bool enabled(std::string_view name) const
Tells whether a system runs on update().
bool contains(std::string_view name) const noexcept
Tells whether a system has this name.
ISystem & add(std::string name, std::unique_ptr< ISystem > system)
Adds a system after the others.
bool remove(std::string_view name)
Removes a system; the others keep their order.
std::vector< std::string_view > names() const
Names of the systems, in update order.
Scheduler(Scheduler &&) noexcept=default
Scheduler & operator=(const Scheduler &)=delete
Scheduler(const Scheduler &)=delete
Fixed-timestep accumulator and monotonic stopwatch.
std::chrono::nanoseconds Duration
Duration type used for simulation time (nanoseconds).
Definition clock.hpp:14
Entity-Component-System used by client and server.
Definition animation.hpp:13
Definition endpoint.hpp:85
Timers around the parts of a frame, and the breakdown of the last frame.
Registry: owns the entities and one component pool per component type.
One simulation step, as the systems see it.
Definition system.hpp:23
constexpr float delta() const noexcept
Duration of the step in seconds, to scale values given per second.
Definition system.hpp:32
std::uint64_t index
Number of the tick: 0 for the first Scheduler::update(), then one more per completed update.
Definition system.hpp:28
core::Duration step
Duration of the step, the fixed step of core::MainLoop.
Definition system.hpp:25