-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawable.go
56 lines (46 loc) · 1.05 KB
/
drawable.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
package main
import (
"sort"
"github.com/hajimehoshi/ebiten/v2"
)
type Drawable interface {
Draw(screen *ebiten.Image)
ZIndex() int
}
type DrawHandler struct {
// List of drawable objects
drawable []Drawable
}
func (o *DrawHandler) Add(obj Drawable) {
o.drawable = append(o.drawable, obj)
// Sort by ZIndex
// ZIndex が大きいものほどあとに描画されるようにする
// つまり ZIndex が大きいものほど後ろにくるようにソートする
sort.Slice(o.drawable, func(i, j int) bool {
return o.drawable[i].ZIndex() < o.drawable[j].ZIndex()
})
}
func (o *DrawHandler) Remove(obj Drawable) {
for i, v := range o.drawable {
if v == obj {
o.drawable = append(o.drawable[:i], o.drawable[i+1:]...)
return
}
}
}
func (o *DrawHandler) Lookup(obj Drawable) bool {
for _, v := range o.drawable {
if v == obj {
return true
}
}
return false
}
func (o *DrawHandler) HandleDraw(screen *ebiten.Image) {
for _, obj := range o.drawable {
obj.Draw(screen)
}
}
func (o *DrawHandler) Clear() {
o.drawable = []Drawable{}
}