-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.h
53 lines (43 loc) · 912 Bytes
/
timer.h
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
53
#pragma once
# include <functional>
class Timer {
public:
Timer() = default;
~Timer() = default;
void restart() {
pass_time = 0;
shotted = false;
}
void set_wait_time(int val) {
wait_time = val;
}
void set_one_shot(bool flag) {
one_shot = flag;
}
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 = true;
pass_time = 0;
}
}
void set_callback(std::function<void()> callback) {
this->callback = callback;
}
void pause() {
paused = true;
}
void resume() {
paused = false;
}
protected:
std::function<void()> callback;
int pass_time = 0;//已过时间
int wait_time = 0;//等待时间
bool paused = false;//是否暂停
bool shotted = false;//是否触发
bool one_shot = false;//单次触发
};