From 44b68b15e643690f32de96cab3f91dcd89988f15 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Mon, 31 May 2021 17:19:23 +0200 Subject: [PATCH] Adding a timer component --- CMakeLists.txt | 2 +- sources/Component/Timer/TimerComponent.cpp | 32 +++++++++++++++++++ sources/Component/Timer/TimerComponent.hpp | 37 ++++++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 sources/Component/Timer/TimerComponent.cpp create mode 100644 sources/Component/Timer/TimerComponent.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 9ff217e8..0710a85a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -56,7 +56,7 @@ set(SOURCES sources/Component/Renderer/CameraComponent.hpp sources/System/Renderer/Render2DScreenSystem.cpp sources/System/Renderer/Render2DScreenSystem.hpp - sources/System/BombHolder/BombHolderSystem.cpp sources/System/BombHolder/BombHolderSystem.hpp) + sources/System/BombHolder/BombHolderSystem.cpp sources/System/BombHolder/BombHolderSystem.hpp sources/Component/Timer/TimerComponent.cpp sources/Component/Timer/TimerComponent.hpp) add_executable(bomberman sources/main.cpp diff --git a/sources/Component/Timer/TimerComponent.cpp b/sources/Component/Timer/TimerComponent.cpp new file mode 100644 index 00000000..09ad1f5b --- /dev/null +++ b/sources/Component/Timer/TimerComponent.cpp @@ -0,0 +1,32 @@ +// +// Created by Zoe Roux on 5/31/21. +// + +#include "TimerComponent.hpp" + +#include + +namespace BBM +{ + TimerComponent::TimerComponent(WAL::Entity &entity, std::chrono::nanoseconds delay) + : WAL::Component(entity), + ringIn(delay) + {} + + TimerComponent::TimerComponent(WAL::Entity &entity, std::chrono::nanoseconds delay, const WAL::Callback &callback) + : WAL::Component(entity), + ringIn(delay), + callback(callback) + {} + + TimerComponent::TimerComponent(WAL::Entity &entity, std::chrono::nanoseconds delay, std::function callback) + : WAL::Component(entity), + ringIn(delay), + callback(std::move(callback)) + {} + + WAL::Component *TimerComponent::clone(WAL::Entity &entity) const + { + return new TimerComponent(entity, this->ringIn, this->callback); + } +} \ No newline at end of file diff --git a/sources/Component/Timer/TimerComponent.hpp b/sources/Component/Timer/TimerComponent.hpp new file mode 100644 index 00000000..e6c005db --- /dev/null +++ b/sources/Component/Timer/TimerComponent.hpp @@ -0,0 +1,37 @@ +// +// Created by Zoe Roux on 5/31/21. +// + +#pragma once + +#include +#include +#include "Models/Callback.hpp" + +namespace BBM +{ + //! @brief + class TimerComponent : public WAL::Component + { + public: + //! @brief The callback to call when the timer ring. + WAL::Callback callback; + //! @brief The ring delay of this timer component. + std::chrono::nanoseconds ringIn; + + Component * clone(WAL::Entity &entity) const override; + + //! @brief A default constructor + TimerComponent(WAL::Entity &entity, std::chrono::nanoseconds delay); + //! @brief Create a timer with a callback. + TimerComponent(WAL::Entity &entity, std::chrono::nanoseconds delay, const WAL::Callback &callback); + //! @brief Create a timer with a function to call on ring. + TimerComponent(WAL::Entity &entity, std::chrono::nanoseconds delay, std::function callback); + //! @brief A timer component is copy constructable + TimerComponent(const TimerComponent &) = default; + //! @brief A default destructor + ~TimerComponent() override = default; + //! @brief A component is not assignable. + TimerComponent &operator=(const TimerComponent &) = delete; + }; +}