-
Notifications
You must be signed in to change notification settings - Fork 0
/
scene.go
105 lines (78 loc) · 1.82 KB
/
scene.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"sort"
"github.com/hajimehoshi/ebiten/v2"
)
type Updatable interface {
Update() error
}
type Drawable interface {
Draw(*ebiten.Image, ebiten.DrawImageOptions)
DrawOrder() int
SetDrawOrder(o int)
}
type Point struct {
X, Y float64
}
type Scene struct {
Components []interface{}
// Do not draw
// Hidden bool
// Do not update
Disabled bool
// Drawing offset
// Offset Point
// Unique ID for each scene component
ID string
drawOrder int
}
func (s *Scene) Register(component interface{}) {
s.Components = append(s.Components, component)
}
func (s *Scene) Update() error {
if s.Disabled {
return nil
}
for _, c := range s.Components {
if u, ok := c.(Updatable); ok {
if err := u.Update(); err != nil {
return err
}
}
}
return nil
}
type Drawables []Drawable
func (a Drawables) Len() int { return len(a) }
func (a Drawables) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a Drawables) Less(i, j int) bool { return a[i].DrawOrder() < a[j].DrawOrder() }
func (s *Scene) Draw(screen *ebiten.Image, opts ebiten.DrawImageOptions) {
// if s.Hidden {
// return
// }
// Copy opts
// newOpts := opts
// Reset GeoM to an identity matrix
// newOpts.GeoM.Reset()
// Translate scenespace to worldspace (translate by s.Offset)
// newOpts.GeoM.Translate(s.Offset.X, s.Offset.Y)
// Reapply the original opts.GeoM to translate into screenspace or whatever
// newOpts.GeoM.Concat(opts.GeoM)
drawables := make(Drawables, 0, len(s.Components))
for _, c := range s.Components {
if d, ok := c.(Drawable); ok {
drawables = append(drawables, d)
}
}
// Draw the subcomponents
sort.Sort(drawables)
for _, d := range drawables {
d.Draw(screen, opts)
}
}
func (s *Scene) DrawOrder() int {
return s.drawOrder
}
func (s *Scene) SetDrawOrder(o int) {
s.drawOrder = o
}