-
Notifications
You must be signed in to change notification settings - Fork 0
/
flicker.go
55 lines (48 loc) · 1.17 KB
/
flicker.go
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
package gophengine
import (
"log/slog"
"time"
)
type Flicker struct {
Sprite *Sprite
dur time.Duration
interval time.Duration
startTime time.Time
flickerTimer time.Time
flicker bool
OnCompleteCallback func() error
}
func NewFlicker(spr *Sprite, dur time.Duration, interval time.Duration) *Flicker {
return &Flicker{
Sprite: spr,
dur: dur,
interval: interval,
flicker: false,
OnCompleteCallback: func() error { return nil },
}
}
func (f *Flicker) Flicker() {
slog.Debug("[Flicker] starting flicker")
f.flicker = true
f.startTime = time.Now()
f.flickerTimer = time.Now()
}
func (f *Flicker) Update() error {
if !f.flicker {
return nil
}
// Check if the total flicker duration has passed
if time.Since(f.startTime) < f.dur {
// Handle the flicker logic
if time.Since(f.flickerTimer) > f.interval {
slog.Debug("[Flicker] flickering", "visible", f.Sprite.Visible)
f.Sprite.Visible = !f.Sprite.Visible
f.flickerTimer = time.Now()
}
} else {
f.Sprite.Visible = false
f.flicker = false
return f.OnCompleteCallback()
}
return nil
}