-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.odin
More file actions
76 lines (63 loc) · 1.53 KB
/
timer.odin
File metadata and controls
76 lines (63 loc) · 1.53 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//Timer system
package extra
Timer :: struct {
waitTime, timeLeft: f32,
active, paused: bool,
}
//Creates a timer that waits for `waitTime`
TimerCreate :: proc(waitTime: f32) -> Timer {
return {waitTime = waitTime, timeLeft = waitTime, active = false, paused = false}
}
//Starts or restarts a timer
TimerStart :: proc(timer: ^Timer) {
timer.active = true
timer.timeLeft = timer.waitTime
}
//Stops a timer
TimerStop :: proc(timer: ^Timer) {
timer.active = false
}
//Pauses a timer
TimerPause :: proc(timer: ^Timer) {
timer.paused = true
}
//Unpauses a timer
TimerUnpause :: proc(timer: ^Timer) {
timer.paused = false
}
//Updates a timer
TimerUpdate :: proc(timer: ^Timer, deltaTime: f32) {
if !timer.active || timer.paused {
return
}
timer.timeLeft -= deltaTime
if timer.timeLeft < 0.0 {
timer.timeLeft = 0.0
timer.active = false
}
}
//Updates multiple timers
TimersUpdate :: proc {
_update_timers_fixed_array,
_update_timers_dynamic_array,
}
@(private)
_update_timers_fixed_array :: proc(timers: ^[$N]Timer, deltaTime: f32) {
for &timer in timers do TimerUpdate(timer, deltaTime)
}
@(private)
_update_timers_dynamic_array :: proc(timers: [dynamic]Timer, deltaTime: f32) {
for &timer in timers do TimerUpdate(&timer, deltaTime)
}
//Checks if a timer is active
TimerIsActive :: proc(timer: Timer) -> bool {
return timer.active
}
//Checks if a timer is paused
TimerIsPaused :: proc(timer: Timer) -> bool {
return timer.paused
}
//Checks if timer is done
TimerIsDone :: proc(timer: ^Timer) -> bool {
return timer.timeLeft == 0.0
}