-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgui.go
67 lines (53 loc) · 1.35 KB
/
gui.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
package main
import (
"fmt"
"image/color"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/text"
"golang.org/x/image/font"
"golang.org/x/image/font/opentype"
)
type GuiElement interface {
Draw(screen *ebiten.Image)
Update()
Start()
}
var GuiElements = make(map[GuiElement]struct{})
type GuiText struct {
font font.Face
*Vector
text string
displayLabel string
}
func (menu *GuiText) Draw(screen *ebiten.Image) {
text.Draw(screen, menu.text, menu.font, int(menu.X), int(menu.Y), color.White)
}
func (menu *GuiText) Update() {
}
func (menu *GuiText) Start() {
}
func NewGuiHealthBar(health *Health, X float64, Y float64, label string) *GuiHealthBar {
return &GuiHealthBar{
GuiText: &GuiText{
font: LoadFont("fonts/FiraCode.ttf", opentype.FaceOptions{}),
text: "Health: 100%",
Vector: &Vector{
X: X,
Y: Y,
},
displayLabel: label,
},
Health: health}
}
type GuiHealthBar struct {
*GuiText
Health *Health
}
func (healthBar *GuiHealthBar) Update() {
precentage := int((float64(healthBar.Health.HitPoints) / float64(healthBar.Health.MaxHitPoints)) * 100)
healthBar.text = fmt.Sprintf("%s Health: %d %%, %d/%d", healthBar.displayLabel, precentage, healthBar.Health.HitPoints, healthBar.Health.MaxHitPoints)
}
func SpawnHealthBar[T GuiElement](obj T) T {
GuiElements[obj] = struct{}{}
return obj
}