mirror of
https://github.com/zoriya/Bomberman.git
synced 2025-12-21 13:55:10 +00:00
Cleaning up callbacks
This commit is contained in:
@@ -24,10 +24,14 @@ namespace WAL
|
|||||||
//! @brief Add a method to be called when this callback is invoked.
|
//! @brief Add a method to be called when this callback is invoked.
|
||||||
//! @param callback The list of arguments of the callback method
|
//! @param callback The list of arguments of the callback method
|
||||||
//! @return A unique ID for this callback. That can be used to remove the callback later.
|
//! @return A unique ID for this callback. That can be used to remove the callback later.
|
||||||
int addCallback(std::function<void (Types...)> callback)
|
template<typename Func>
|
||||||
|
int addCallback(Func callback)
|
||||||
{
|
{
|
||||||
int id = this->_nextID++;
|
int id = this->_nextID++;
|
||||||
this->_functions[id] = std::move(callback);
|
if constexpr(std::is_same_v<Func, std::function<void (Types...)>>)
|
||||||
|
this->_functions[id] = std::move(callback);
|
||||||
|
else
|
||||||
|
this->_functions[id] = std::function<void (Types...)>(callback);
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,8 +57,9 @@ namespace WAL
|
|||||||
//! @brief A default assignment operator
|
//! @brief A default assignment operator
|
||||||
Callback &operator=(const Callback &) = default;
|
Callback &operator=(const Callback &) = default;
|
||||||
|
|
||||||
//! @brief Implicitly transform a function into a callback.
|
//! @brief Implicitly transform a callable into a callback.
|
||||||
Callback(std::function<void (Types...)> callback) // NOLINT(google-explicit-constructor)
|
template<typename Func>
|
||||||
|
Callback(Func callback) // NOLINT(google-explicit-constructor)
|
||||||
{
|
{
|
||||||
this->addCallback(callback);
|
this->addCallback(callback);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,6 +28,56 @@ namespace WAL
|
|||||||
private:
|
private:
|
||||||
//! @brief The list of registered systems
|
//! @brief The list of registered systems
|
||||||
std::vector<std::unique_ptr<ISystem>> _systems = {};
|
std::vector<std::unique_ptr<ISystem>> _systems = {};
|
||||||
|
|
||||||
|
//! @brief Start the game loop
|
||||||
|
//! @param callback A callback called after each update of the game. It allow you to update the engine based on a specific game state. (you can also update the game state here)
|
||||||
|
//! @param state An initial game state. If not specified, it will be defaulted.
|
||||||
|
//! @tparam T A type used to track your game state. It must be default constructable.
|
||||||
|
template<typename T>
|
||||||
|
void _run(const Callback<Wal &, T &> &callback, T state = T())
|
||||||
|
{
|
||||||
|
auto lastTick = std::chrono::steady_clock::now();
|
||||||
|
std::chrono::nanoseconds fBehind(0);
|
||||||
|
|
||||||
|
while (!this->shouldClose) {
|
||||||
|
auto now = std::chrono::steady_clock::now();
|
||||||
|
std::chrono::nanoseconds dtime = now - lastTick;
|
||||||
|
fBehind += dtime;
|
||||||
|
lastTick = now;
|
||||||
|
|
||||||
|
while (fBehind > Wal::timestep) {
|
||||||
|
fBehind -= Wal::timestep;
|
||||||
|
for (auto &system : this->_systems)
|
||||||
|
system->fixedUpdate();
|
||||||
|
}
|
||||||
|
for (auto &system : this->_systems)
|
||||||
|
system->update(dtime);
|
||||||
|
callback(*this, state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(PLATFORM_WEB)
|
||||||
|
template<typename T>
|
||||||
|
static void _runIteration(void *param)
|
||||||
|
{
|
||||||
|
static auto [wal, callback, state] = *reinterpret_cast<std::tuple<Wal &, const Callback<Wal &, T &> &, T &> *>(param);
|
||||||
|
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;
|
||||||
|
for (auto &system : wal._systems)
|
||||||
|
system->fixedUpdate();
|
||||||
|
}
|
||||||
|
for (auto &system : wal._systems)
|
||||||
|
system->update(dtime);
|
||||||
|
callback(wal, state);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
public:
|
public:
|
||||||
//! @brief The scene that contains entities.
|
//! @brief The scene that contains entities.
|
||||||
std::shared_ptr<Scene> scene;
|
std::shared_ptr<Scene> scene;
|
||||||
@@ -94,23 +144,6 @@ namespace WAL
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
//! @brief Start the game loop
|
|
||||||
//! @param callback A callback called after each update of the game. It allow you to update the engine based on a specific game state. (you can also update the game state here)
|
|
||||||
//! @param state An initial game state. If not specified, it will be defaulted.
|
|
||||||
//! @tparam T A type used to track your game state. It must be default constructable.
|
|
||||||
template<typename T>
|
|
||||||
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
|
//! @brief Start the game loop
|
||||||
//! @param callback A callback called after each update of the game. It allow you to update the engine based on a specific game state. (you can also update the game state here)
|
//! @param callback A callback called after each update of the game. It allow you to update the engine based on a specific game state. (you can also update the game state here)
|
||||||
//! @param state An initial game state. If not specified, it will be defaulted.
|
//! @param state An initial game state. If not specified, it will be defaulted.
|
||||||
@@ -118,50 +151,14 @@ namespace WAL
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
void run(const Callback<Wal &, T &> &callback, T state = T())
|
void run(const Callback<Wal &, T &> &callback, T state = T())
|
||||||
{
|
{
|
||||||
auto lastTick = std::chrono::steady_clock::now();
|
#if defined(PLATFORM_WEB)
|
||||||
std::chrono::nanoseconds fBehind(0);
|
std::tuple<Wal &, const Callback<Wal &, T &> &, T &> iterationParams(*this, callback, state);
|
||||||
|
return emscripten_set_main_loop_arg((em_arg_callback_func)_runIteration<T>, (void *)&iterationParams, 0, 1);
|
||||||
while (!this->shouldClose) {
|
#else
|
||||||
auto now = std::chrono::steady_clock::now();
|
return this->_run(callback, state);
|
||||||
std::chrono::nanoseconds dtime = now - lastTick;
|
#endif
|
||||||
fBehind += dtime;
|
|
||||||
lastTick = now;
|
|
||||||
|
|
||||||
while (fBehind > Wal::timestep) {
|
|
||||||
fBehind -= Wal::timestep;
|
|
||||||
for (auto &system : this->_systems)
|
|
||||||
system->fixedUpdate();
|
|
||||||
}
|
|
||||||
for (auto &system : this->_systems)
|
|
||||||
system->update(dtime);
|
|
||||||
callback(*this, state);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#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
|
//! @brief A default constructor
|
||||||
Wal() = default;
|
Wal() = default;
|
||||||
//! @brief A WAL can't be copy constructed
|
//! @brief A WAL can't be copy constructed
|
||||||
|
|||||||
@@ -16,14 +16,6 @@ namespace BBM
|
|||||||
return new CollisionComponent(entity);
|
return new CollisionComponent(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
CollisionComponent::CollisionComponent(WAL::Entity &entity, std::function<void (WAL::Entity &, const WAL::Entity &)> onCollide, std::function<void (WAL::Entity &, const WAL::Entity &)> onCollided, Vector3f bound)
|
|
||||||
: WAL::Component(entity), onCollide(onCollide), onCollided(onCollided), bound(bound)
|
|
||||||
{ }
|
|
||||||
|
|
||||||
CollisionComponent::CollisionComponent(WAL::Entity &entity, std::function<void (WAL::Entity &, const WAL::Entity &)> onCollide, std::function<void (WAL::Entity &, const WAL::Entity &)> onCollided, float boundSize)
|
|
||||||
: WAL::Component(entity), onCollide(onCollide), onCollided(onCollided), bound({boundSize, boundSize, boundSize})
|
|
||||||
{ }
|
|
||||||
|
|
||||||
CollisionComponent::CollisionComponent(WAL::Entity &entity, WAL::Callback<WAL::Entity &, const WAL::Entity &> onCollide, WAL::Callback<WAL::Entity &, const WAL::Entity &> onCollided, Vector3f bound)
|
CollisionComponent::CollisionComponent(WAL::Entity &entity, WAL::Callback<WAL::Entity &, const WAL::Entity &> onCollide, WAL::Callback<WAL::Entity &, const WAL::Entity &> onCollided, Vector3f bound)
|
||||||
: WAL::Component(entity), onCollide(onCollide), onCollided(onCollided), bound(bound)
|
: WAL::Component(entity), onCollide(onCollide), onCollided(onCollided), bound(bound)
|
||||||
{ }
|
{ }
|
||||||
|
|||||||
@@ -27,12 +27,6 @@ namespace BBM
|
|||||||
//! @brief A component can't be instantiated, it should be derived.
|
//! @brief A component can't be instantiated, it should be derived.
|
||||||
explicit CollisionComponent(WAL::Entity &entity);
|
explicit CollisionComponent(WAL::Entity &entity);
|
||||||
|
|
||||||
//! @brief Constructor with a callback function
|
|
||||||
CollisionComponent(WAL::Entity &entity, std::function<void (WAL::Entity &, const WAL::Entity &)> onCollide, std::function<void (WAL::Entity &, const WAL::Entity &)> onCollided, Vector3f bound);
|
|
||||||
|
|
||||||
//! @brief Constructor with a callback function, same boundSize for all axis
|
|
||||||
CollisionComponent(WAL::Entity &entity, std::function<void (WAL::Entity &, const WAL::Entity &)> onCollide, std::function<void (WAL::Entity &, const WAL::Entity &)> onCollided, float boundSize = 0);
|
|
||||||
|
|
||||||
//! @brief Constructor with a WAL::Callback
|
//! @brief Constructor with a WAL::Callback
|
||||||
CollisionComponent(WAL::Entity &entity, WAL::Callback<WAL::Entity &, const WAL::Entity &> onCollide, WAL::Callback<WAL::Entity &, const WAL::Entity &> onCollided,Vector3f bound);
|
CollisionComponent(WAL::Entity &entity, WAL::Callback<WAL::Entity &, const WAL::Entity &> onCollide, WAL::Callback<WAL::Entity &, const WAL::Entity &> onCollided,Vector3f bound);
|
||||||
|
|
||||||
|
|||||||
@@ -26,26 +26,14 @@ namespace BBM
|
|||||||
this->_window.clear();
|
this->_window.clear();
|
||||||
|
|
||||||
this->_window.useCamera(this->_camera);
|
this->_window.useCamera(this->_camera);
|
||||||
for (auto &entity : this->_wal.scene->getEntities()) {
|
for (auto &[_, pos, drawable] : this->_wal.scene->view<PositionComponent, Drawable3DComponent>()) {
|
||||||
if (!entity.hasComponent<Drawable3DComponent>()
|
|
||||||
|| !entity.hasComponent<PositionComponent>())
|
|
||||||
continue;
|
|
||||||
auto &drawable = entity.getComponent<Drawable3DComponent>();
|
|
||||||
auto &pos = entity.getComponent<PositionComponent>();
|
|
||||||
|
|
||||||
drawable.drawable->setPosition(pos.position);
|
drawable.drawable->setPosition(pos.position);
|
||||||
drawable.drawable->drawOn(this->_window);
|
drawable.drawable->drawOn(this->_window);
|
||||||
}
|
}
|
||||||
this->_window.unuseCamera();
|
this->_window.unuseCamera();
|
||||||
|
|
||||||
// TODO sort entities based on the Z axis
|
// TODO sort entities based on the Z axis
|
||||||
for (auto &entity : this->_wal.scene->getEntities()) {
|
for (auto &[_, pos, drawable] : this->_wal.scene->view<PositionComponent, Drawable2DComponent>()) {
|
||||||
if (!entity.hasComponent<Drawable2DComponent>()
|
|
||||||
|| !entity.hasComponent<PositionComponent>())
|
|
||||||
continue;
|
|
||||||
auto &drawable = entity.getComponent<Drawable2DComponent>();
|
|
||||||
auto &pos = entity.getComponent<PositionComponent>();
|
|
||||||
|
|
||||||
drawable.drawable->setPosition(Vector2f(pos.position.x, pos.position.y));
|
drawable.drawable->setPosition(Vector2f(pos.position.x, pos.position.y));
|
||||||
drawable.drawable->drawOn(this->_window);
|
drawable.drawable->drawOn(this->_window);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user