-
Notifications
You must be signed in to change notification settings - Fork 1
/
ui.go
163 lines (142 loc) · 4.56 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
package main
import (
"fmt"
"github.com/golang/freetype"
"github.com/golang/freetype/truetype"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/text"
log "github.com/sirupsen/logrus"
"github.com/ungerik/go3d/float64/vec2"
"golang.org/x/image/colornames"
"image"
"image/color"
"image/draw"
"io/ioutil"
"math"
"path/filepath"
)
const uiSprite = "ui-level-bar.png"
const uiRedBarSprite = "red-level.png"
const uiBlueBarSprite = "blue-level.png"
const fontfile = "ka1.ttf"
const barW = 32
//const barH = 120
const barMargin = 7
const uiMarginLeft = 10
var imgBar *ebiten.Image
var imgO2Level *ebiten.Image
var imgFuelLevel *ebiten.Image
func init() {
imgBar = loadSprite(filepath.Join(spritesPath, uiSprite))
imgO2Level = loadSprite(filepath.Join(spritesPath, uiBlueBarSprite))
imgFuelLevel = loadSprite(filepath.Join(spritesPath, uiRedBarSprite))
}
type UserInterface struct {
player *J0hn
o2Position vec2.T
fuelPosition vec2.T
distancePosition vec2.T
o2Level int
o2Offset float64
fuelLevel int
fuelOffset float64
distanceLevel int
uiScale float64
src image.Image
font *truetype.Font
ctxFont *freetype.Context
}
func NewUi(player *J0hn) *UserInterface {
ui := new(UserInterface)
ui.uiScale = 3
ui.o2Position = vec2.T{
uiMarginLeft,
windowHeight / ui.uiScale,
}
ui.fuelPosition = vec2.T{
uiMarginLeft + (barW * ui.uiScale),
windowHeight / ui.uiScale,
}
ui.player = player
ui.src = image.NewUniform(colornames.Green)
fontBytes, err := ioutil.ReadFile(fontfile)
if err != nil {
log.Println(err)
}
f, err := freetype.ParseFont(fontBytes)
if err != nil {
log.Println(err)
}
ui.font = f
imgClip := image.NewRGBA(image.Rect(0, 0, 500, 200))
draw.Draw(imgClip, imgClip.Bounds(), image.NewUniform(colornames.Green), image.Point{}, draw.Src)
ctxFont := freetype.NewContext()
ctxFont.SetDPI(72)
ctxFont.SetFontSize(20)
ctxFont.SetClip(imgClip.Bounds())
ctxFont.SetSrc(imgClip)
ctxFont.SetFont(ui.font)
ui.ctxFont = ctxFont
return ui
}
func (ui *UserInterface) Draw(screen *ebiten.Image) {
//if !ui.player.isLifting && ui.player.flying {}
ui.ctxFont.SetDst(screen)
optO2 := &ebiten.DrawImageOptions{}
optO2.GeoM.Translate(ui.o2Position[0]/ui.uiScale, ui.o2Position[1]/ui.uiScale)
optO2.GeoM.Scale(ui.uiScale, ui.uiScale)
optO2Fill := &ebiten.DrawImageOptions{}
optO2Fill.GeoM.Translate(ui.o2Position[0]/ui.uiScale, (ui.o2Position[1]+ui.o2Offset)/ui.uiScale)
optO2Fill.GeoM.Scale(ui.uiScale, ui.uiScale)
_ = screen.DrawImage(imgO2Level.SubImage(image.Rect(0, 0, barW, ui.o2Level)).(*ebiten.Image), optO2Fill)
_ = screen.DrawImage(imgBar, optO2)
optFuel := &ebiten.DrawImageOptions{}
optFuel.GeoM.Translate(ui.fuelPosition[0]/ui.uiScale, ui.fuelPosition[1]/ui.uiScale)
optFuel.GeoM.Scale(ui.uiScale, ui.uiScale)
optFuelFill := &ebiten.DrawImageOptions{}
optFuelFill.GeoM.Translate(ui.fuelPosition[0]/ui.uiScale, (ui.fuelPosition[1]+ui.fuelOffset)/ui.uiScale)
optFuelFill.GeoM.Scale(ui.uiScale, ui.uiScale)
_ = screen.DrawImage(imgFuelLevel.SubImage(image.Rect(0, 0, barW, ui.fuelLevel)).(*ebiten.Image), optFuelFill)
_ = screen.DrawImage(imgBar, optFuel)
text.Draw(screen, fmt.Sprintf("%vkm", math.Round(ui.player.relativePosition[1])), truetype.NewFace(ui.font, &truetype.Options{
Size: 30,
DPI: 72,
Hinting: 0,
GlyphCacheEntries: 0,
SubPixelsX: 0,
SubPixelsY: 0,
}), uiMarginLeft*2, windowHeight-10, colornames.Green)
text.Draw(screen, "O2", truetype.NewFace(ui.font, &truetype.Options{
Size: 20,
DPI: 72,
Hinting: 0,
GlyphCacheEntries: 0,
SubPixelsX: 0,
SubPixelsY: 0,
}), int(ui.o2Position[0])+15,
int(ui.o2Position[1])-4,
color.RGBA{0x5b, 0x6E, 0xE1, 0xFF},
)
text.Draw(screen, "Fuel", truetype.NewFace(ui.font, &truetype.Options{
Size: 20,
DPI: 72,
Hinting: 0,
GlyphCacheEntries: 0,
SubPixelsX: 0,
SubPixelsY: 0,
}), int(ui.fuelPosition[0]),
int(ui.fuelPosition[1])-4,
color.RGBA{0xac, 0x32, 0x32, 0xFF},
)
}
func (ui *UserInterface) Update(screen *ebiten.Image, delta int64) {
_, h := imgO2Level.Size()
ui.o2Level = int(float64(h-14) * ui.player.o2 / 100)
ui.o2Level += barMargin
ui.o2Offset = float64(h-ui.o2Level) * ui.uiScale
ui.o2Offset -= barMargin * ui.uiScale
ui.fuelLevel = int(float64(h-14) * ui.player.fuel / 100)
ui.fuelLevel += barMargin
ui.fuelOffset = float64(h-ui.fuelLevel) * ui.uiScale
ui.fuelOffset -= barMargin * ui.uiScale
}