Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
System.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
15#pragma once
16
17// =============================================================================
18// Includes
19// =============================================================================
20
21// Project includes
22#include "../core/Types.hpp"
23#include "../core/World.hpp"
24
25// Standard library
26#include <algorithm>
27
28namespace esengine::ecs {
29
30// Forward declaration
31class Registry;
32
33// =============================================================================
34// System Base Class
35// =============================================================================
36
59class System {
60public:
62 virtual ~System() = default;
63
71 virtual void init(Registry& registry) { (void)registry; }
72
80 virtual void update(World& world) = 0;
81
88 virtual void shutdown(Registry& registry) { (void)registry; }
89
90 // =========================================================================
91 // Enable/Disable
92 // =========================================================================
93
100 void setEnabled(bool enabled) { enabled_ = enabled; }
101
106 bool isEnabled() const { return enabled_; }
107
108 // =========================================================================
109 // Priority
110 // =========================================================================
111
119 void setPriority(i32 priority) { priority_ = priority; }
120
125 i32 getPriority() const { return priority_; }
126
127protected:
129 bool enabled_ = true;
132};
133
134// =============================================================================
135// System Group
136// =============================================================================
137
164public:
173 systems_.push_back(std::move(system));
174 sortSystems();
175 }
176
189 template<typename T, typename... Args>
190 T& createSystem(Args&&... args) {
191 auto system = makeUnique<T>(std::forward<Args>(args)...);
192 T& ref = *system;
193 addSystem(std::move(system));
194 return ref;
195 }
196
204 void init(Registry& registry) {
205 for (auto& system : systems_) {
206 system->init(registry);
207 }
208 }
209
218 void update(World& world) {
219 for (auto& system : systems_) {
220 if (system->isEnabled()) {
221 system->update(world);
222 }
223 }
224 }
225
233 void shutdown(Registry& registry) {
234 for (auto& system : systems_) {
235 system->shutdown(registry);
236 }
237 }
238
239private:
243 void sortSystems() {
244 std::sort(systems_.begin(), systems_.end(),
245 [](const Unique<System>& a, const Unique<System>& b) {
246 return a->getPriority() < b->getPriority();
247 });
248 }
249
251 std::vector<Unique<System>> systems_;
252};
253
254} // namespace esengine::ecs
Core type definitions and utilities for ESEngine.
Unified context passed to all ECS systems.
Central container for all entities and components.
Definition Registry.hpp:73
Container for organizing and executing systems.
Definition System.hpp:163
void update(World &world)
Updates all enabled systems.
Definition System.hpp:218
T & createSystem(Args &&... args)
Creates and adds a system to the group.
Definition System.hpp:190
void addSystem(Unique< System > system)
Adds an existing system to the group.
Definition System.hpp:172
void shutdown(Registry &registry)
Shuts down all systems.
Definition System.hpp:233
void init(Registry &registry)
Initializes all systems in the group.
Definition System.hpp:204
Base class for all ECS systems.
Definition System.hpp:59
virtual void shutdown(Registry &registry)
Called once when the system is removed.
Definition System.hpp:88
void setEnabled(bool enabled)
Enables or disables the system.
Definition System.hpp:100
virtual void update(World &world)=0
Called every frame to execute system logic.
i32 getPriority() const
Gets the system's execution priority.
Definition System.hpp:125
i32 priority_
Execution priority (lower = earlier).
Definition System.hpp:131
virtual void init(Registry &registry)
Called once when the system is added to a group.
Definition System.hpp:71
bool isEnabled() const
Checks if the system is enabled.
Definition System.hpp:106
virtual ~System()=default
Virtual destructor for proper cleanup.
void setPriority(i32 priority)
Sets the system's execution priority.
Definition System.hpp:119
bool enabled_
Whether the system is enabled.
Definition System.hpp:129
constexpr Unique< T > makeUnique(Args &&... args)
Creates a Unique pointer with the given arguments.
Definition Types.hpp:99
std::int32_t i32
Signed 32-bit integer.
Definition Types.hpp:54
std::unique_ptr< T > Unique
Unique ownership smart pointer.
Definition Types.hpp:75
Context passed to every System::update call.
Definition World.hpp:40