Merging with develop and reworking all systems

This commit is contained in:
Zoe Roux
2021-06-04 01:04:05 +02:00
41 changed files with 661 additions and 333 deletions
+3 -2
View File
@@ -10,6 +10,7 @@
#include <memory>
#include "Component/Component.hpp"
#include "Exception/WalError.hpp"
#include "Models/TypeHolder.hpp"
namespace WAL
{
@@ -84,13 +85,13 @@ namespace WAL
//! @brief Add a component to this entity. The component is constructed in place.
//! @throw DuplicateError is thrown if a component with the same type already exist.
//! @return This entity is returned
template<typename T, typename ...Types>
template<typename T, typename ...TNested, typename ...Types>
Entity &addComponent(Types &&...params)
{
const std::type_index &type = typeid(T);
if (this->hasComponent(type))
throw DuplicateError("A component of the type \"" + std::string(type.name()) + "\" already exists.");
this->_components[type] = std::make_unique<T>(*this, std::forward<Types>(params)...);
this->_components[type] = std::make_unique<T>(*this, TypeHolder<TNested>()..., std::forward<Types>(params)...);
this->_componentAdded(type);
return *this;
}
+13
View File
@@ -0,0 +1,13 @@
//
// Created by Zoe Roux on 2021-06-02.
//
#pragma once
namespace WAL
{
//! @brief A class only used to specify template arguments.
template<typename T>
class TypeHolder {};
}
+35 -1
View File
@@ -14,6 +14,10 @@
#include "Models/Callback.hpp"
#include "Scene/Scene.hpp"
#if defined(PLATFORM_WEB)
#include <emscripten/emscripten.h>
#endif
namespace WAL
{
class Entity;
@@ -47,7 +51,7 @@ namespace WAL
});
if (existing != this->_systems.end())
throw DuplicateError("A system of the type \"" + std::string(type.name()) + "\" already exists.");
this->_systems.push_back(std::make_unique<T>(std::forward<Types>(params)...));
this->_systems.push_back(std::make_unique<T>(*this, std::forward<Types>(params)...));
return *this;
}
@@ -102,7 +106,13 @@ namespace WAL
void run(const std::function<void (Wal &, T &)> &callback, T state = T())
{
Callback<Wal &, T &> update(callback);
#if defined(PLATFORM_WEB)
std::tuple iterationParams(this, &update, &state);
return emscripten_set_main_loop_arg((em_arg_callback_func)runIteration<T>, (void *)&iterationParams, 0, 1);
#else
return this->run(update, state);
#endif
}
//! @brief Start the game loop
@@ -137,6 +147,30 @@ namespace WAL
}
}
#if defined(PLATFORM_WEB)
template<typename T>
static void runIteration(void *param)
{
static auto iterationParams = reinterpret_cast<std::tuple<Wal *, Callback<Wal &, T &> *, T *> *>(param);
static const Callback<Wal &, T &> callback = *((Callback<Wal &, T &> *)std::get<1>(*iterationParams));
static T *state = (T *)std::get<2>(*iterationParams);
static Wal *wal = (Wal *)std::get<0>(*iterationParams);
static auto lastTick = std::chrono::steady_clock::now();
static std::chrono::nanoseconds fBehind(0);
auto now = std::chrono::steady_clock::now();
std::chrono::nanoseconds dtime = now - lastTick;
fBehind += dtime;
lastTick = now;
while (fBehind > Wal::timestep) {
fBehind -= Wal::timestep;
wal->_fixedUpdate();
}
wal->_update(dtime);
callback(*wal, *state);
}
#endif
//! @brief A default constructor
Wal() = default;
//! @brief A WAL can't be copy constructed