-
Notifications
You must be signed in to change notification settings - Fork 1
/
reticle.go
135 lines (109 loc) · 3.72 KB
/
reticle.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
package render
import (
"image"
"github.com/hajimehoshi/ebiten/v2"
)
type TargetReticle struct {
HUDSprite
}
type NavReticle struct {
HUDSprite
}
// NewTargetReticle creates a target reticle from an image with 2 rows and 2 columns, representing the four corners of it
func NewTargetReticle(img *ebiten.Image) *TargetReticle {
r := &TargetReticle{
HUDSprite: NewHUDSpriteFromSheet(img, 1.0, 2, 2, 0),
}
return r
}
// NewNavReticle creates a nav reticle from an image
func NewNavReticle(img *ebiten.Image) *NavReticle {
r := &NavReticle{
HUDSprite: NewHUDSprite(img, 1.0),
}
return r
}
func (t *TargetReticle) Draw(bounds image.Rectangle, hudOpts *DrawHudOptions) {
screen := hudOpts.Screen
// set minimum scale size based on screen size
screenW, screenH := screen.Bounds().Dx(), screen.Bounds().Dy()
screenDim := int(float64(screenW))
if screenH > screenW {
screenDim = int(float64(screenH))
}
screenMinScale := float64(screenDim) / (50 * float64(t.Width()))
// adjust scale based on size of rect target being placed around
targetDim := bounds.Dx()
if bounds.Dy() > targetDim {
targetDim = bounds.Dy()
}
rScale := float64(targetDim) / (10 * float64(t.Width()))
if rScale < screenMinScale {
rScale = screenMinScale
}
rOff := rScale * float64(t.Width()) / 2
minX, minY, maxX, maxY := float64(bounds.Min.X), float64(bounds.Min.Y), float64(bounds.Max.X), float64(bounds.Max.Y)
rColor := hudOpts.HudColor(_colorEnemy)
// setup some common draw modifications
var op *ebiten.DrawImageOptions
geoM := ebiten.GeoM{}
geoM.Scale(rScale, rScale)
colorScale := ebiten.ColorScale{}
colorScale.ScaleWithColor(rColor)
// top left corner
t.SetTextureFrame(0)
op = &ebiten.DrawImageOptions{ColorScale: colorScale, GeoM: geoM}
op.Filter = ebiten.FilterNearest
op.GeoM.Translate(minX-rOff, minY-rOff)
screen.DrawImage(t.Texture(), op)
// top right corner
t.SetTextureFrame(1)
op = &ebiten.DrawImageOptions{ColorScale: colorScale, GeoM: geoM}
op.Filter = ebiten.FilterNearest
op.GeoM.Translate(maxX-rOff, minY-rOff)
screen.DrawImage(t.Texture(), op)
// bottom left corner
t.SetTextureFrame(2)
op = &ebiten.DrawImageOptions{ColorScale: colorScale, GeoM: geoM}
op.Filter = ebiten.FilterNearest
op.GeoM.Translate(minX-rOff, maxY-rOff)
screen.DrawImage(t.Texture(), op)
// bottom right corner
t.SetTextureFrame(3)
op = &ebiten.DrawImageOptions{ColorScale: colorScale, GeoM: geoM}
op.Filter = ebiten.FilterNearest
op.GeoM.Translate(maxX-rOff, maxY-rOff)
screen.DrawImage(t.Texture(), op)
}
func (t *NavReticle) Draw(bounds image.Rectangle, hudOpts *DrawHudOptions) {
screen := hudOpts.Screen
// set minimum scale size based on screen size
screenW, screenH := screen.Bounds().Dx(), screen.Bounds().Dy()
screenDim := int(float64(screenW))
if screenH > screenW {
screenDim = int(float64(screenH))
}
screenMinScale := float64(screenDim) / (50 * float64(t.Width()))
// adjust scale based on size of rect target being placed around
targetDim := bounds.Dx()
if bounds.Dy() > targetDim {
targetDim = bounds.Dy()
}
rScale := float64(targetDim) / float64(t.Width())
if rScale < screenMinScale {
rScale = screenMinScale
}
minX, minY, dX, dY := float64(bounds.Min.X), float64(bounds.Min.Y), float64(bounds.Dx()), float64(bounds.Dy())
rColor := hudOpts.HudColor(_colorNavPoint)
// setup some common draw modifications
var op *ebiten.DrawImageOptions
geoM := ebiten.GeoM{}
geoM.Scale(rScale, rScale)
colorScale := ebiten.ColorScale{}
colorScale.ScaleWithColor(rColor)
rX, rY := 1+minX+dX/2-rScale*float64(t.Width())/2, 1+minY+dY/2-rScale*float64(t.Height())/2
op = &ebiten.DrawImageOptions{ColorScale: colorScale, GeoM: geoM}
op.Filter = ebiten.FilterNearest
op.GeoM.Translate(rX, rY)
screen.DrawImage(t.Texture(), op)
}