-
Notifications
You must be signed in to change notification settings - Fork 0
/
flasher.go
60 lines (50 loc) · 1.11 KB
/
flasher.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
56
57
58
59
60
package gophengine
import (
"image/color"
"log/slog"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/colorm"
"github.com/tanema/gween"
"github.com/tanema/gween/ease"
)
type Flasher struct {
alpha float32
flash bool
done bool
tween *gween.Tween
whiteImg *ebiten.Image
}
func NewFlasher(width, height int, dur float32) *Flasher {
white := ebiten.NewImage(width, height)
white.Fill(color.White)
return &Flasher{
alpha: 1,
flash: false,
done: false,
tween: gween.New(1, 0, dur, ease.Linear),
whiteImg: white,
}
}
func (f *Flasher) Draw(img *ebiten.Image) {
if f.flash && !f.done {
slog.Debug("drawing", "flash", f.flash, "done", f.done, "alpha", f.alpha)
cm := colorm.ColorM{}
cm.Scale(255, 255, 255, float64(f.alpha))
colorm.DrawImage(img, f.whiteImg, cm, &colorm.DrawImageOptions{})
}
}
func (f *Flasher) Update(dt float64) {
if f.done {
f.flash = false
f.done = false
f.tween.Reset()
return
}
if f.flash && !f.done {
f.alpha, f.done = f.tween.Update(float32(dt))
}
}
func (f *Flasher) Flash() {
f.tween.Reset()
f.flash = true
}