/
hud.go
191 lines (176 loc) · 4.38 KB
/
hud.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package tentsuyu
import "github.com/hajimehoshi/ebiten/v2"
//HUD represents the player's Heads Up Display
type HUD struct {
x, y, prevX, prevY float64
w, h float64
uiElements map[string]*hudElement
topLeft, topRight, bottomLeft, bottomRight *hudSection
}
//hudElement is just for use in by the HUD
type hudElement struct {
UIElement
hudX, hudY float64
}
//NewHUD returns a new HUD object
func NewHUD(screenWidth, screenHeight float64) *HUD {
hud := &HUD{
w: screenWidth,
h: screenHeight,
uiElements: make(map[string]*hudElement),
}
hud.createSections()
return hud
}
//Update updates the HUD....prevHealth
func (hud *HUD) Update() {
//hud.x = Components.Camera.GetX()
//hud.y = Components.Camera.GetY()
//if hud.x != hud.prevX || hud.y != hud.prevY {
for _, element := range hud.topLeft.elements {
element.UIElement.SetPosition(hud.x+element.hudX, hud.y+element.hudY)
element.UIElement.Update()
}
for _, element := range hud.topRight.elements {
element.UIElement.SetPosition(hud.x+element.hudX, hud.y+element.hudY)
element.UIElement.Update()
}
for _, element := range hud.bottomRight.elements {
element.UIElement.SetPosition(hud.x+element.hudX, hud.y+element.hudY)
element.UIElement.Update()
}
for _, element := range hud.bottomLeft.elements {
element.UIElement.SetPosition(hud.x+element.hudX, hud.y+element.hudY)
element.UIElement.Update()
}
//}
hud.prevX = hud.x
hud.prevY = hud.y
}
//Draw All elements
func (hud *HUD) Draw(screen *ebiten.Image, camera *Camera) {
for _, element := range hud.topLeft.elements {
element.Draw(screen, camera)
}
for _, element := range hud.topRight.elements {
element.Draw(screen, camera)
}
for _, element := range hud.bottomRight.elements {
element.Draw(screen, camera)
}
for _, element := range hud.bottomLeft.elements {
element.Draw(screen, camera)
}
}
//AddTopLeft of the HUD
func (hud *HUD) AddTopLeft(element UIElement) {
section := hud.topLeft
h := &hudElement{
hudX: section.startX,
UIElement: element,
hudY: section.startY,
}
if section.count > 0 {
addition := 0
for _, element := range section.elements {
_, h := element.Size()
addition += h
}
h.hudY += float64(addition)
}
section.count++
section.addElement(h)
}
//AddTopRight of the hud
func (hud *HUD) AddTopRight(element UIElement) {
section := hud.topRight
width, _ := element.Size()
h := &hudElement{
hudX: section.startX - float64(width),
UIElement: element,
hudY: section.startY,
}
if section.count > 0 {
addition := 0
for _, element := range section.elements {
_, h := element.Size()
addition += h
}
h.hudY += float64(addition)
}
section.count++
section.addElement(h)
}
//AddBottomRight of the HUD
func (hud *HUD) AddBottomRight(element UIElement) {
section := hud.bottomRight
width, height := element.Size()
h := &hudElement{
hudX: section.startX - float64(width),
UIElement: element,
hudY: section.startY - float64(height),
}
if section.count > 0 {
addition := 0
for _, element := range section.elements {
_, h := element.Size()
addition += h
}
h.hudY -= float64(addition)
}
section.count++
section.addElement(h)
}
//AddBottomLeft of the HUD
func (hud *HUD) AddBottomLeft(element UIElement) {
section := hud.bottomLeft
_, height := element.Size()
h := &hudElement{
hudX: section.startX,
UIElement: element,
hudY: section.startY - float64(height),
}
if section.count > 0 {
addition := 0
for _, element := range section.elements {
_, h := element.Size()
addition += h
}
h.hudY -= float64(addition)
}
section.count++
section.addElement(h)
}
type hudSection struct {
startX, startY float64
invertY bool
count int
elements []*hudElement
}
func (hs *hudSection) addElement(element *hudElement) {
hs.elements = append(hs.elements, element)
}
func (hud *HUD) createSections() {
hud.topLeft = &hudSection{
startX: 10,
startY: 5,
elements: []*hudElement{},
}
hud.topRight = &hudSection{
startX: hud.w - 10,
startY: 5,
elements: []*hudElement{},
}
hud.bottomLeft = &hudSection{
elements: []*hudElement{},
startX: 10,
startY: hud.h - 10,
invertY: true,
}
hud.bottomRight = &hudSection{
elements: []*hudElement{},
startX: hud.w - 10,
startY: hud.h - 10,
invertY: true,
}
}