-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.go
169 lines (142 loc) · 3.8 KB
/
ui.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
package main
import (
"image"
"github.com/hajimehoshi/ebiten"
)
// UIImage is a type for the UIImage enum
type UIImage int
const (
// UIHealthBar ... UIIMAGE ENUM [1]
UIHealthBar UIImage = iota + 1
// UIEnergyBar ... UIIMAGE ENUM [2]
UIEnergyBar
// UIHealthMeter ... UIIMAGE ENUM [3]
UIHealthMeter
// UIEnergyMeter ... UIIMAGE ENUM [4]
UIEnergyMeter
// UIBlinkMeter ... UIIMAG ENUM [5]
UIBlinkMeter
)
func (u UIImage) String() string {
return [...]string{
"Unknown",
"UIHealthBar",
"UIEnergyBar",
"UIHealthMeter",
"UIEnergyMeter",
"UIBlinkMeter",
}[u]
}
// ^ UITYPE ENUM ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Generate UI
func generateUI(image *ebiten.Image) []UI {
return []UI{
createStaticImage(newVec2i(4, screenHeight-14), UIHealthBar, image),
createStaticImage(newVec2i(55, screenHeight-14), UIEnergyBar, image),
createMeterImage(newVec2i(20, screenHeight-12), UIHealthMeter, image),
createMeterImage(newVec2i(71, screenHeight-12), UIEnergyMeter, image),
createMeterImage(newVec2i(108, screenHeight-14), UIBlinkMeter, image),
}
}
// UI is all the UI in the game
type UI interface {
render(screen *ebiten.Image)
uiImage() UIImage
update(Vec2i)
}
// getUIRect returns a image.Rectangle of the UI image
func getUIRect(ui UIImage) image.Rectangle {
switch ui {
case (UIHealthBar):
return image.Rect(0, 52, 45, 62)
case (UIEnergyBar):
return image.Rect(0, 71, 45, 81)
case (UIHealthMeter):
return image.Rect(16, 64, 43, 70)
case (UIEnergyMeter):
return image.Rect(16, 82, 43, 88)
case (UIBlinkMeter):
return image.Rect(0, 88, 17, 99)
}
// Default, empty
return image.Rectangle{}
}
// StaticImage is an image with nothing else special
type StaticImage struct {
position Vec2i
ui UIImage
image *ebiten.Image
}
func createStaticImage(position Vec2i, ui UIImage, image *ebiten.Image) *StaticImage {
return &StaticImage{
position,
ui,
image,
}
}
func (s StaticImage) uiImage() UIImage {
return s.ui
}
func (s StaticImage) update(sizeValues Vec2i) {
}
func (s StaticImage) render(screen *ebiten.Image) {
subRect := getUIRect(s.ui)
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(s.position.x), float64(s.position.y))
op.Filter = ebiten.FilterNearest // Maybe fix rotation grossness?
screen.DrawImage(s.image.SubImage(subRect).(*ebiten.Image), op)
}
// MeterImage is a bar, it shrinks or grows based on a value
type MeterImage struct {
position Vec2i
ui UIImage
meterSize Vec2i
image *ebiten.Image
}
func createMeterImage(position Vec2i, ui UIImage, image *ebiten.Image) *MeterImage {
return &MeterImage{
position,
ui,
newVec2i(1, 1),
image,
}
}
func (m MeterImage) uiImage() UIImage {
return m.ui
}
// update sizeValues.x = currentSize; sizeValues.y = maxSize
func (m *MeterImage) update(sizeValues Vec2i) {
m.meterSize = sizeValues
}
func (m MeterImage) render(screen *ebiten.Image) {
subRect := getUIRect(m.ui)
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(m.position.x), float64(m.position.y))
op.Filter = ebiten.FilterNearest // Maybe fix rotation grossness?
//op.ColorM.Translate(0, 255, 0, 0.5)
screen.DrawImage(m.image.SubImage(image.Rect(
subRect.Min.X,
subRect.Min.Y,
subRect.Min.X+int(float64(subRect.Max.X-subRect.Min.X)*(float64(m.meterSize.x)/float64(m.meterSize.y))),
subRect.Max.Y,
)).(*ebiten.Image), op)
}
func updateUI(g *Game) {
for _, u := range g.ui {
switch {
case u.uiImage() == UIHealthMeter:
u.update(newVec2i(g.player.health, g.player.maxHealth))
case u.uiImage() == UIEnergyMeter:
u.update(newVec2i(g.player.energy, g.player.maxEnergy))
case u.uiImage() == UIBlinkMeter:
u.update(newVec2i(g.player.blinkTimer, g.player.blinkTimerMax))
default:
go u.update(Vec2i{})
}
}
}
func renderUI(g *Game, screen *ebiten.Image) {
for _, u := range g.ui {
u.render(screen)
}
}