Dawn/src/dawn/time/ITimeManager.hpp
2023-03-10 08:35:57 -08:00

85 lines
2.2 KiB
C++

// 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 {
class ITimeManager {
public:
float_t time = 0.0f;
float_t unpausedTime = 0.0f;
float_t delta = 0.016f;
bool_t isPaused = false;
StateProviderSet<float_t> timeoutProvider;
StateProviderSet<std::pair<float_t, float_t>> 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<class T>
std::function<void()> useTimeout(
std::function<void()> someCallback,
float_t timeout,
T *context
) {
return context->getGame()->timeManager.timeoutProvider.addEffect(someCallback, timeout, context);
}
template<class T>
std::function<void()> useInterval(
std::function<void()> callback,
float_t interval,
T *context
) {
return context->getGame()->timeManager.intervalProvider.addEffect(
callback, std::pair<float_t, float_t>({ interval, 0 }), context
);
}
}