Acheron
Loading...
Searching...
No Matches
system_function.hpp
1#pragma once
2
3#include "system.hpp"
4#include "world.hpp"
5
6#include <functional>
7
8namespace acheron::ecs {
14 template<typename Func>
15 class SystemFunction : public System {
16 public:
17 explicit SystemFunction(Func&& f) : func(std::forward<Func>(f)) {}
18
25 void Update(World& world, double dt) override {
26 if (!entities.empty()) {
27 for (auto entity : entities) {
28 this->template Call<Func>(world, entity, dt);
29 }
30 } else {
31 if constexpr (!std::is_invocable_v<Func, World&, Entity, double> &&
32 !std::is_invocable_v<Func, World&, Entity>) {
33 this->template Call<Func>(world, Entity{}, dt);
34 }
35 }
36 }
37
38 private:
39 Func func;
40
51 template<typename F>
52 void Call(World& world, Entity entity, double dt) {
53 if constexpr (std::is_invocable_v<F, World&, Entity, double>) {
54 std::invoke(func, world, entity, dt);
55 } else if constexpr (std::is_invocable_v<F, World&, Entity>) {
56 std::invoke(func, world, entity);
57 } else if constexpr (std::is_invocable_v<F, World&, double>) {
58 std::invoke(func, world, dt);
59 } else if constexpr (std::is_invocable_v<F, World&>) {
60 std::invoke(func, world);
61 } else {
62 static_assert(always_false<F>::value, "SystemFunction callable signature not supported");
63 }
64 }
65
66 template<class>
67 struct always_false : std::false_type {};
68 };
69}
SystemFunction(Func &&f)
System Function Wrapper.
Definition system_function.hpp:17
void Update(World &world, double dt) override
Iterates through all entities associated with system, and call it.
Definition system_function.hpp:25
Systems called every frame based on entities.
Definition system.hpp:34
std::unordered_set< Entity > entities
List of entities a system is matched with.
Definition system.hpp:39
Central context for the ECS.
Definition world.hpp:20