-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.h
More file actions
52 lines (48 loc) · 928 Bytes
/
Copy pathtimer.h
File metadata and controls
52 lines (48 loc) · 928 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#pragma once
#include <functional>
class Timer
{
public:
Timer()=default;
~Timer() = default;
void restart() {
pass_time = 0;
shotted = 0;
}
void set_wait_time(int val) {
wait_time = val;
}
void set_one_shot(bool flag) {
one_shot = flag;
}
void set_callback(std::function<void()> callback){
this->callback = callback;
}
void pause() {
paused = 1;
}
void resume() {
paused = 0;
}
void on_update(int delta){
if (paused) return;
pass_time += delta;
if (pass_time>=wait_time)
{
if ((!one_shot || (one_shot && !shotted)) && callback)
//若(单次触发且未触发)或(循环触发)并有回调函数 则执行
{
callback();
}
shotted = 1;
pass_time = 0;
}
}
private:
int pass_time = 0;//已过时间
int wait_time = 0;//等待时间
bool paused = 0;//是否暂停
bool shotted = 0;//是否已触发
bool one_shot = 0;//是否单次触发
std::function<void()>callback;//定时器结束后触发的回调函数
};