-
Notifications
You must be signed in to change notification settings - Fork 1
/
compass.go
256 lines (213 loc) · 6.7 KB
/
compass.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package render
import (
"fmt"
"image"
"image/color"
"math"
"github.com/hajimehoshi/ebiten/v2/vector"
"github.com/harbdog/raycaster-go/geom"
"github.com/pixelmek-3d/pixelmek-3d/game/model"
"github.com/tinne26/etxt"
)
var (
// define default colors
_colorCompassPips = _colorDefaultGreen
_colorCompassTurret = color.NRGBA{R: 0, G: 127, B: 0, A: 255}
)
type Compass struct {
HUDSprite
fontRenderer *etxt.Renderer
targetIndicator *compassIndicator
navIndicator *compassIndicator
heading float64
turretAngle float64
}
type compassIndicator struct {
heading float64
enabled bool
}
// NewCompass creates a compass image to be rendered on demand
func NewCompass(font *Font) *Compass {
// create and configure font renderer
renderer := etxt.NewStdRenderer()
renderer.SetCacheHandler(font.FontCache.NewHandler())
renderer.SetFont(font.Font)
renderer.SetAlign(etxt.Top, etxt.XCenter)
renderer.SetColor(color.NRGBA{255, 255, 255, 255})
c := &Compass{
HUDSprite: NewHUDSprite(nil, 1.0),
fontRenderer: renderer,
targetIndicator: &compassIndicator{heading: 2 * geom.Pi / 3, enabled: true},
navIndicator: &compassIndicator{heading: geom.Pi / 3, enabled: true},
}
return c
}
func (c *Compass) updateFontSize(_, height int) {
// set font size based on element size
pxSize := float64(height) / 2
if pxSize < 1 {
pxSize = 1
}
c.fontRenderer.SetSizePx(int(pxSize))
}
func (c *Compass) SetTargetEnabled(b bool) {
c.targetIndicator.enabled = b
}
func (c *Compass) SetTargetHeading(heading float64) {
if heading < 0 {
heading += geom.Pi2
}
c.targetIndicator.heading = heading
}
func (c *Compass) SetNavEnabled(b bool) {
c.navIndicator.enabled = b
}
func (c *Compass) SetNavHeading(heading float64) {
if heading < 0 {
heading += geom.Pi2
}
c.navIndicator.heading = heading
}
func (c *Compass) SetValues(heading, turretAngle float64) {
c.heading = heading
c.turretAngle = turretAngle
}
func (c *Compass) Draw(bounds image.Rectangle, hudOpts *DrawHudOptions) {
screen := hudOpts.Screen
c.fontRenderer.SetTarget(screen)
bX, bY, bW, bH := bounds.Min.X, bounds.Min.Y, bounds.Dx(), bounds.Dy()
c.updateFontSize(bW, bH)
// turret angle appears opposite because it is relative to body heading which counts up counter clockwise
relTurretAngle := -model.AngleDistance(c.heading, c.turretAngle)
headingDeg := geom.Degrees(c.heading)
relTurretDeg := geom.Degrees(relTurretAngle)
midX, topY := float32(bX)+float32(bW)/2, float32(bY)
// turret indicator box
turretColor := hudOpts.HudColor(_colorCompassTurret)
// TODO: support 360 degree turret rotation option
var maxTurretDeg float64 = 90
relTurretRatio := relTurretDeg / maxTurretDeg
tW, tH := float32(relTurretRatio)*float32(bW)/2, float32(bH/4)
tAlpha := uint8(4 * int(turretColor.A) / 5)
vector.DrawFilledRect(screen, midX, topY, tW, tH, color.NRGBA{turretColor.R, turretColor.G, turretColor.B, tAlpha}, false)
// compass pips
pipColor := hudOpts.HudColor(_colorCompassPips)
c.fontRenderer.SetColor(pipColor)
for i := int(-maxTurretDeg); i <= int(maxTurretDeg); i++ {
actualDeg := i + int(math.Round(headingDeg))
if actualDeg < 0 {
actualDeg += 360
} else if actualDeg >= 360 {
actualDeg -= 360
}
var pipWidth, pipHeight float32
if actualDeg%10 == 0 {
pipWidth = 2
pipHeight = float32(bH) / 4
}
if actualDeg%30 == 0 {
pipWidth = 3
pipHeight = float32(bH) / 3
}
if pipWidth > 0 {
// pip shows relative based on index (i) where negative is right of center, positive is left
iRatio := float32(-i) / float32(maxTurretDeg)
iX := float32(bX) + float32(bW)/2 + iRatio*float32(bW)/2
vector.DrawFilledRect(screen, iX-pipWidth/2, topY, pipWidth, pipHeight, pipColor, false)
var pipDegStr string
switch {
case actualDeg == 0:
pipDegStr = "E"
case actualDeg == 90:
pipDegStr = "N"
case actualDeg == 180:
pipDegStr = "W"
case actualDeg == 270:
pipDegStr = "S"
case actualDeg%30 == 0:
pipDegStr = fmt.Sprintf("%d", actualDeg)
}
if pipDegStr != "" {
c.fontRenderer.Draw(pipDegStr, int(iX), int(topY+float32(bH)/2)+2)
}
}
}
// heading indicator line
headingColor := hudOpts.HudColor(_colorAltimeter)
hW, hH := float32(5.0), float32(bH)/2 // TODO: calculate line thickness based on image height
vector.DrawFilledRect(screen, midX-hW/2, topY, hW, hH, headingColor, false)
if c.navIndicator.enabled {
// TODO: draw nav indicator slightly better
iHeading := c.navIndicator.heading
iDeg := int(geom.Degrees(iHeading))
iColor := hudOpts.HudColor(_colorNavPoint)
iRendered := false
for i := int(-maxTurretDeg); i <= int(maxTurretDeg); i++ {
actualDeg := i + int(math.Round(headingDeg))
if actualDeg < 0 {
actualDeg += 360
} else if actualDeg >= 360 {
actualDeg -= 360
}
if iDeg == actualDeg {
iRadius := float32(bH) / 8
iRatio := float32(-i) / float32(maxTurretDeg)
iX := float32(bX) + float32(bW)/2 + iRatio*float32(bW)/2
vector.DrawFilledCircle(screen, iX-iRadius, topY-iRadius, iRadius, iColor, false)
iRendered = true
break
}
}
if !iRendered {
// draw indicator that target is outside of current compass range
actualMinDeg := headingDeg - maxTurretDeg
iMinFound := model.IsBetweenDegrees(actualMinDeg, actualMinDeg-90, float64(iDeg))
var iRatio float32
if iMinFound {
iRatio = -1
} else {
iRatio = 1
}
iRadius := float32(bH) / 12
iX := float32(bX) + float32(bW)/2 + iRatio*float32(bW)/2
vector.DrawFilledCircle(screen, iX-iRadius, topY-iRadius, iRadius, iColor, false)
}
}
if c.targetIndicator.enabled {
// TODO: draw target indicator slightly better
iHeading := c.targetIndicator.heading
iDeg := int(geom.Degrees(iHeading))
iColor := hudOpts.HudColor(_colorEnemy)
iRendered := false
for i := int(-maxTurretDeg); i <= int(maxTurretDeg); i++ {
actualDeg := i + int(math.Round(headingDeg))
if actualDeg < 0 {
actualDeg += 360
} else if actualDeg >= 360 {
actualDeg -= 360
}
if iDeg == actualDeg {
iRadius := float32(bH) / 8
iRatio := float32(-i) / float32(maxTurretDeg)
iX := float32(bX) + float32(bW)/2 + iRatio*float32(bW)/2
vector.DrawFilledCircle(screen, iX-iRadius, topY-iRadius, iRadius, iColor, false)
iRendered = true
break
}
}
if !iRendered {
// draw indicator that target is outside of current compass range
actualMinDeg := headingDeg - maxTurretDeg
iMinFound := model.IsBetweenDegrees(actualMinDeg, actualMinDeg-90, float64(iDeg))
var iRatio float32
if iMinFound {
iRatio = -1
} else {
iRatio = 1
}
iRadius := float32(bH) / 12
iX := float32(bX) + float32(bW)/2 + iRatio*float32(bW)/2
vector.DrawFilledCircle(screen, iX-iRadius, topY-iRadius, iRadius, iColor, false)
}
}
}