// Copyright (c) 2022 Dominic Masters // // This software is released under the MIT License. // https://opensource.org/licenses/MIT #pragma once #include "dawnlibs.hpp" #include "event/Event.hpp" #include "state/StateProvider.hpp" namespace Dawn { struct IntervalProviderData { float_t interval; float_t time; }; class ITimeManager { public: float_t time = 0.0f; float_t unpausedTime = 0.0f; float_t delta = 0.016f; bool_t isPaused = false; StateProviderSet timeoutProvider; StateProviderSet intervalProvider; Event<> eventTimePaused; Event<> eventTimeResumed; /** * Constructor for the Time Manager. */ ITimeManager(); /** * Updates / Ticks the time manager instance. * * @param delta Time in seconds to tick the instance by. */ void update(float_t delta); /** * Pauses the game. */ void pause(); /** * Resumes the game. */ void resume(); /** * Returns the current system timestamp. * * @return Current timestamp. */ virtual int64_t getTimestamp() = 0; }; /** * Use timeout provider method. Invokes your callback after some specified * time has passed. * * @tparam T Your context type (usually SceneItemComponent). * @param someCallback Callback to be invoked. * @param timeout Timeout to wait before the method is invoked. * @param context Context of the component, just use (this). * @return Method that when invoked will unsubscribe from the timeout. */ template std::function useTimeout( std::function someCallback, float_t timeout, T *context ) { return context->getGame()->timeManager.timeoutProvider.addEffect(someCallback, timeout, context); } /** * Use interval provider method. Invokes your callback at a consistant and * common time based on the interval you provide. * * @tparam T Your context type (usually SceneItemComponent). * @param someCallback Callback to be invoked. * @param timeout Interval to invoke your callback. * @param context Context of the component, just use (this). * @return Method that when invoked will unsubscribe from the interval. */ template std::function useInterval( std::function callback, float_t interval, T *context ) { return context->getGame()->timeManager.intervalProvider.addEffect( callback, { .interval = interval, .time = 0 }, context ); } }