-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhealthSystem.go
38 lines (29 loc) · 985 Bytes
/
healthSystem.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
package foodzy
import (
"fmt"
"github.com/co0p/foodzy/component"
"github.com/co0p/foodzy/internal/ecs"
"github.com/hajimehoshi/ebiten/v2"
)
type HealthSystem struct {
manager *ecs.EntityManager
gameOverAction func(*ecs.EntityManager)
}
func NewHealthSystem(manager *ecs.EntityManager, gameOverAction func(entityManager *ecs.EntityManager)) *HealthSystem {
return &HealthSystem{manager: manager, gameOverAction: gameOverAction}
}
func (s *HealthSystem) Draw(screen *ebiten.Image) {}
func (s *HealthSystem) Update() error {
// update player consumption
player := s.manager.QueryFirstByTag("player")
health := player.GetComponent(component.HealthType).(*component.Health)
health.Consume()
// update the score
score := s.manager.QueryFirstByTag("score")
text := score.GetComponent(component.TextType).(*component.Text)
text.Value = fmt.Sprintf("%3d%%", health.CurrentHealth())
if health.CurrentHealth() == 0 {
s.gameOverAction(s.manager)
}
return nil
}