From a477ef125245bb1ecfc31082b11a6842d8ec5d79 Mon Sep 17 00:00:00 2001 From: "arthur.jamet" Date: Wed, 16 Jun 2021 13:26:48 +0200 Subject: [PATCH] timer system for ui --- CMakeLists.txt | 2 ++ sources/Runner/Runner.cpp | 2 ++ sources/System/Timer/TimerSystem.cpp | 11 +--------- sources/System/Timer/TimerSystem.hpp | 4 ++-- sources/System/Timer/TimerUISystem.cpp | 30 ++++++++++++++++++++++++++ sources/System/Timer/TimerUISystem.hpp | 29 +++++++++++++++++++++++++ 6 files changed, 66 insertions(+), 12 deletions(-) create mode 100644 sources/System/Timer/TimerUISystem.cpp create mode 100644 sources/System/Timer/TimerUISystem.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index a3af798d..2c87fc07 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -143,6 +143,8 @@ set(SOURCES sources/Runner/LobbyScene.cpp sources/Runner/HowToPlayScene.cpp sources/Runner/ScoreScene.cpp + sources/System/Timer/TimerUISystem.hpp + sources/System/Timer/TimerUISystem.cpp ) add_executable(bomberman sources/main.cpp diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index 647ccfd6..4f48de05 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -16,6 +16,7 @@ #include "Runner.hpp" #include "Models/GameState.hpp" #include +#include #include #include #include @@ -67,6 +68,7 @@ namespace BBM void Runner::addSystems(WAL::Wal &wal) { wal.addSystem() + .addSystem() .addSystem() .addSystem() .addSystem() diff --git a/sources/System/Timer/TimerSystem.cpp b/sources/System/Timer/TimerSystem.cpp index 8335a084..bb27dd3b 100644 --- a/sources/System/Timer/TimerSystem.cpp +++ b/sources/System/Timer/TimerSystem.cpp @@ -15,7 +15,7 @@ namespace BBM : System(wal) {} - void TimerSystem::onUpdate(WAL::ViewEntity &entity, std::chrono::nanoseconds dtime) + void TimerSystem::onUpdate(WAL::ViewEntity &entity, std::chrono::nanoseconds dtime) { auto &timer = entity.get(); timer.ringIn -= dtime; @@ -23,14 +23,5 @@ namespace BBM timer.setDisable(true); timer.callback(entity, this->_wal); } - - RAY2D::Text *text = dynamic_cast(entity.get().drawable.get()); - - if (text) { - unsigned long second = std::chrono::duration_cast(timer.ringIn).count(); - unsigned long minutes = second / 60; - second = second % 60; - text->setText(std::to_string(minutes) + ":" + std::to_string(second)); - } } } \ No newline at end of file diff --git a/sources/System/Timer/TimerSystem.hpp b/sources/System/Timer/TimerSystem.hpp index 19090837..2be1c1f7 100644 --- a/sources/System/Timer/TimerSystem.hpp +++ b/sources/System/Timer/TimerSystem.hpp @@ -11,11 +11,11 @@ namespace BBM { - class TimerSystem : public WAL::System + class TimerSystem : public WAL::System { public: //! @inherit - void onUpdate(WAL::ViewEntity &entity, std::chrono::nanoseconds dtime) override; + void onUpdate(WAL::ViewEntity &entity, std::chrono::nanoseconds dtime) override; //! @brief A default constructor TimerSystem(WAL::Wal &); diff --git a/sources/System/Timer/TimerUISystem.cpp b/sources/System/Timer/TimerUISystem.cpp new file mode 100644 index 00000000..a1f4cfa4 --- /dev/null +++ b/sources/System/Timer/TimerUISystem.cpp @@ -0,0 +1,30 @@ +// +// Created by Zoe Roux on 5/31/21. +// + +#include "TimerUISystem.hpp" +#include "Component/Timer/TimerComponent.hpp" +#include "Drawables/2D/Text.hpp" + +using namespace std::chrono_literals; +namespace RAY2D = RAY::Drawables::Drawables2D; + +namespace BBM +{ + TimerUISystem::TimerUISystem(WAL::Wal &wal) + : System(wal) + {} + + void TimerUISystem::onUpdate(WAL::ViewEntity &entity, std::chrono::nanoseconds dtime) + { + auto &timer = entity.get(); + RAY2D::Text *text = dynamic_cast(entity.get().drawable.get()); + + if (text) { + unsigned long second = std::chrono::duration_cast(timer.ringIn).count(); + unsigned long minutes = second / 60; + second = second % 60; + text->setText(std::to_string(minutes) + ":" + std::to_string(second)); + } + } +} \ No newline at end of file diff --git a/sources/System/Timer/TimerUISystem.hpp b/sources/System/Timer/TimerUISystem.hpp new file mode 100644 index 00000000..c21a0300 --- /dev/null +++ b/sources/System/Timer/TimerUISystem.hpp @@ -0,0 +1,29 @@ +// +// Created by Zoe Roux on 5/31/21. +// + +#pragma once + +#include +#include +#include +#include "Component/Renderer/Drawable2DComponent.hpp" + +namespace BBM +{ + class TimerUISystem : public WAL::System + { + public: + //! @inherit + void onUpdate(WAL::ViewEntity &entity, std::chrono::nanoseconds dtime) override; + + //! @brief A default constructor + TimerUISystem(WAL::Wal &); + //! @brief A timer system is copy constructable. + TimerUISystem(const TimerUISystem &) = default; + //! @brief A default destructor + ~TimerUISystem() override = default; + //! @breief A timer system is assignable. + TimerUISystem &operator=(const TimerUISystem &) = default; + }; +}