diff options
Diffstat (limited to 'src/timer_type.hpp')
-rw-r--r-- | src/timer_type.hpp | 73 |
1 files changed, 68 insertions, 5 deletions
diff --git a/src/timer_type.hpp b/src/timer_type.hpp index 0ce6b095..d682b3bd 100644 --- a/src/timer_type.hpp +++ b/src/timer_type.hpp @@ -2,17 +2,80 @@ #include "h-basic.h" +#include <functional> + /* * Timer descriptor and runtime data. */ struct timer_type { - timer_type *next; /* The next timer in the list */ +private: + std::function<void ()> m_callback; + +public: + // + // XXX Currently need public access for loading and saving. + // + bool m_enabled; + s32b m_delay = 0; + s32b m_countdown = 0; + +public: + /** + * Create a new timer + */ + timer_type(std::function<void()> callback, s32b delay) + : m_callback(callback) + , m_enabled(false) + , m_delay(delay) + , m_countdown(delay) + { + } + + timer_type(timer_type const &other) = delete; + timer_type &operator =(timer_type const &other) = delete; + + /** + * Enable the timer. + */ + void enable() + { + m_enabled = true; + } + + /** + * Disable the timer. + */ + void disable() + { + m_enabled = false; + } + + /** + * Change delay and reset. + */ + void set_delay_and_reset(s32b delay) + { + m_delay = delay; + m_countdown = delay; + } - bool_ enabled; /* Is it currently counting? */ + /** + * Count down. + */ + void count_down() + { + if (!m_enabled) + { + return; + } - s32b delay; /* Delay between activations */ - s32b countdown; /* The current number of turns passed, when it reaches delay it fires */ + m_countdown--; + if (m_countdown <= 0) + { + m_countdown = m_delay; + m_callback(); + } + } - void (*callback)(); /* The C function to call upon firing */ }; |