-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspriteRenderSystem.go
54 lines (42 loc) · 1.28 KB
/
spriteRenderSystem.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
package foodzy
import (
"github.com/co0p/foodzy/component"
"github.com/co0p/foodzy/internal/ecs"
"github.com/hajimehoshi/ebiten/v2"
)
type SpriteRenderSystem struct {
manager *ecs.EntityManager
}
func NewSpriteRenderSystem(manager *ecs.EntityManager) *SpriteRenderSystem {
return &SpriteRenderSystem{manager: manager}
}
func (s *SpriteRenderSystem) Draw(screen *ebiten.Image) {
candidates := s.manager.QueryByComponents(component.TransformType, component.SpriteType)
var foreground []*ecs.Entity
var background []*ecs.Entity
for _, e := range candidates {
if !e.Active {
continue
}
transform := e.GetComponent(component.TransformType).(*component.Transform)
if transform.Z > 0 {
foreground = append(foreground, e)
} else {
background = append(background, e)
}
}
orderedEntities := append(background, foreground...)
for _, e := range orderedEntities {
transform := e.GetComponent(component.TransformType).(*component.Transform)
sprite := e.GetComponent(component.SpriteType).(*component.Sprite)
if sprite.Image != nil {
op := &ebiten.DrawImageOptions{}
op.GeoM.Scale(transform.Scale, transform.Scale)
op.GeoM.Translate(transform.X, transform.Y)
screen.DrawImage(sprite.Image, op)
}
}
}
func (s *SpriteRenderSystem) Update() error {
return nil
}