-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponent_loading.go
129 lines (112 loc) · 2.73 KB
/
component_loading.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package engoutil
import (
"github.com/EngoEngine/ecs"
"github.com/EngoEngine/engo"
"github.com/EngoEngine/engo/common"
"github.com/EngoEngine/math"
)
var _ ecs.System = (*LoadingComponent)(nil)
var _ ecs.Initializer = (*LoadingComponent)(nil)
func NewLoadingComponent(x, y, size float32, fgColor, bgColor uint32) *LoadingComponent {
return &LoadingComponent{
position: engo.Point{X: x, Y: y},
size: size,
fgColor: fgColor,
bgColor: bgColor,
arcSpeed: 1.0,
rotateSpeed: 0.5,
increase: 1.0,
}
}
type LoadingComponent struct {
items [2]*Shape
position engo.Point
size float32
fgColor uint32
bgColor uint32
radian float32
increase float32
arcSpeed float32
rotateSpeed float32
paused bool
update bool
hidden bool
}
func (l *LoadingComponent) Move(x, y float32) {
if l.position.X != x || l.position.Y != y {
l.position.X = x
l.position.Y = y
l.update = true
}
}
func (l *LoadingComponent) Speed() float32 {
return l.arcSpeed
}
func (l *LoadingComponent) SetSpeed(speed float32) {
l.arcSpeed = speed
l.rotateSpeed = speed / 2
}
func (l *LoadingComponent) Pause(state bool) {
l.paused = state
}
func (l *LoadingComponent) Toggle() {
l.paused = !l.paused
}
func (l *LoadingComponent) State() bool {
return !l.paused
}
func (l *LoadingComponent) Hide() {
l.hidden = true
l.update = true
}
func (l *LoadingComponent) Show() {
l.hidden = false
l.update = true
}
// Implement System interface
func (*LoadingComponent) Remove(ecs.BasicEntity) {}
// Implement System interface
func (l *LoadingComponent) Update(dt float32) {
if l.update {
for _, item := range l.items {
item.Render.Hidden = l.hidden
if !l.hidden {
item.Move(l.position.X, l.position.Y)
}
}
l.update = false
}
if !l.hidden && !l.paused {
if l.radian >= 350 {
l.increase = -1.0
} else if l.radian <= 10 {
l.increase = 1.0
}
l.radian += math.Mod(dt * l.arcSpeed * 360 * l.increase, 360)
l.items[0].SetArc(l.radian)
if l.increase > 0 {
l.items[0].AddRotate(dt * l.rotateSpeed * 360)
} else {
// 当弧度减少时, 2转速
l.items[0].AddRotate(dt * l.arcSpeed * 2 * 360)
}
l.items[0].Space.SetCenter(l.position)
}
}
// Implement Initializer interface
func (l *LoadingComponent) New(w *ecs.World) {
// FG
l.items[0] = NewCircle(l.position.X, l.position.Y, l.size*0.5, 350, l.size*0.1, l.fgColor, 0)
// BG
l.items[1] = NewCircle(l.position.X, l.position.Y, l.size*0.5, 0, l.size*0.1, l.bgColor, 0)
l.items[0].Render.SetZIndex(9997)
l.items[1].Render.SetZIndex(9996)
for _, system := range w.Systems() {
switch sys := system.(type) {
case *common.RenderSystem:
for _, item := range l.items {
sys.AddByInterface(item)
}
}
}
}