/
spriteAnimator.go
51 lines (39 loc) · 1.14 KB
/
spriteAnimator.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
package systems
import (
"time"
"github.com/co0p/tankism/game/ecs/components"
"github.com/co0p/tankism/lib/ecs"
"github.com/hajimehoshi/ebiten/v2"
)
type SpriteAnimator struct {
EntityManager *ecs.EntityManager
}
func (s *SpriteAnimator) Draw(screen *ebiten.Image) {}
func (s *SpriteAnimator) Update() error {
now := time.Now()
entities := s.EntityManager.FindByComponents(components.SpriteAnimationType, components.SpriteType)
for _, e := range entities {
spriteAnimation := e.GetComponent(components.SpriteAnimationType).(*components.SpriteAnimation)
sprite := e.GetComponent(components.SpriteType).(*components.Sprite)
lastUpdate := spriteAnimation.LastUpdate
duration := spriteAnimation.Duration
idx := spriteAnimation.Idx
// lazy init
if lastUpdate.IsZero() {
lastUpdate = now
spriteAnimation.LastUpdate = now
}
// out of bounds
if idx == len(spriteAnimation.Images)-1 {
sprite.Image = nil
continue
}
nextUpdate := lastUpdate.Add(duration)
if now.After(nextUpdate) {
sprite.Image = spriteAnimation.Images[idx]
spriteAnimation.LastUpdate = nextUpdate
spriteAnimation.Idx++
}
}
return nil
}