18 #if defined(PLATFORM_WEB)
19 #include <emscripten/emscripten.h>
31 std::vector<std::unique_ptr<ISystem>>
_systems = {};
43 auto lastTick = std::chrono::steady_clock::now();
44 std::chrono::nanoseconds fBehind(0);
47 auto now = std::chrono::steady_clock::now();
48 std::chrono::nanoseconds dtime = now - lastTick;
54 for (
auto &system : this->_systems)
55 system->fixedUpdate();
57 for (
auto &system : this->_systems)
58 system->update(dtime);
59 this->_scene->applyChanges();
60 callback(*
this, state);
64 #if defined(PLATFORM_WEB)
66 static void _runIteration(
void *param)
68 static auto [wal, callback, state] = *
reinterpret_cast<std::tuple<Wal &, const Callback<Wal &, T &> &, T &
> *>(param);
69 static auto lastTick = std::chrono::steady_clock::now();
70 static std::chrono::nanoseconds fBehind(0);
72 auto now = std::chrono::steady_clock::now();
73 std::chrono::nanoseconds dtime = now - lastTick;
78 for (
auto &system : wal._systems)
79 system->fixedUpdate();
81 for (
auto &system : wal._systems)
82 system->update(dtime);
83 wal._scene->applyChanges();
91 static constexpr std::chrono::nanoseconds
timestep = std::chrono::milliseconds(32);
103 for (
auto &entity : this->_scene->getEntities()) {
104 for (
auto &cmp : entity._components)
105 cmp.second->onStop();
108 this->_scene = std::move(newScene);
109 for (
auto &entity : this->_scene->getEntities()) {
110 for (
auto &cmp : entity._components)
111 cmp.second->onStart();
117 template<
typename T,
class ...Types>
120 const std::type_info &type =
typeid(T);
121 auto existing = std::find_if(this->_systems.begin(), this->_systems.end(), [&type] (
auto &sys) {
122 return typeid(*sys) == type;
124 if (existing != this->_systems.end())
125 throw DuplicateError(
"A system of the type \"" + std::string(type.name()) +
"\" already exists.");
126 this->_systems.push_back(std::make_unique<T>(*
this, std::forward<Types>(params)...));
135 const std::type_info &type =
typeid(T);
136 auto existing = std::find_if(this->_systems.begin(), this->_systems.end(), [&type] (
auto &sys) {
137 return typeid(*sys) == type;
139 if (existing != this->_systems.end())
140 throw DuplicateError(
"A system of the type \"" + std::string(type.name()) +
"\" already exists.");
141 this->_systems.push_back(std::make_unique<T>(system));
150 const std::type_info &type =
typeid(T);
151 auto existing = std::find_if(this->_systems.begin(), this->_systems.end(), [&type] (
auto &sys) {
152 return typeid(*sys) == type;
154 if (existing == this->_systems.end())
155 throw NotFoundError(
"A system of the type \"" + std::string(type.name()) +
"\" could not be found.");
156 return *
static_cast<T *
>(existing->get());
163 const std::type_info &type =
typeid(T);
164 auto existing = std::find_if(this->_systems.begin(), this->_systems.end(), [&type] (
auto &sys) {
165 return typeid(*sys) == type;
167 if (existing == this->_systems.end())
168 throw NotFoundError(
"No system could be found with the type \"" + std::string(type.name()) +
"\".");
169 this->_systems.erase(existing);
180 #if defined(PLATFORM_WEB)
181 std::tuple<Wal &, const Callback<Wal &, T &> &, T &> iterationParams(*
this, callback, state);
182 return emscripten_set_main_loop_arg((em_arg_callback_func)_runIteration<T>, (
void *)&iterationParams, 0, 1);
184 return this->
_run(callback, state);
191 Wal(
const Wal &) =
delete;