-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgfx_draw.go
More file actions
247 lines (223 loc) · 6.15 KB
/
Copy pathgfx_draw.go
File metadata and controls
247 lines (223 loc) · 6.15 KB
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
package main
import (
"fmt"
"image/color"
"math"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
)
var fadePixel *ebiten.Image
func drawHugeText(screen *ebiten.Image, text string, centerX, centerY int, scaleFactor float64) {
scale := 4
baseW := len(text) * 7
baseH := 16
img := ebiten.NewImage(baseW, baseH)
img.Fill(color.RGBA{0, 0, 0, 0})
ebitenutil.DebugPrintAt(img, text, 0, 4)
scaleF := float64(scale) * scaleFactor
w := float64(baseW) * scaleF
h := float64(baseH) * scaleF
x := float64(centerX) - w/2
y := float64(centerY) - h/2
op := &ebiten.DrawImageOptions{}
op.GeoM.Scale(scaleF, scaleF)
op.GeoM.Translate(x, y)
screen.DrawImage(img, op)
}
func drawGlowText(screen *ebiten.Image, text string, centerX, centerY int, scaleFactor float64, glow float64) {
baseW := len(text) * 7
baseH := 16
img := ebiten.NewImage(baseW, baseH)
img.Fill(color.RGBA{0, 0, 0, 0})
ebitenutil.DebugPrintAt(img, text, 0, 4)
scaleF := scaleFactor
w := float64(baseW) * scaleF
h := float64(baseH) * scaleF
x := float64(centerX) - w/2
y := float64(centerY) - h/2
// Main text color fades white -> yellow
mainOp := &ebiten.DrawImageOptions{}
mainOp.GeoM.Scale(scaleF, scaleF)
mainOp.GeoM.Translate(x, y)
r := 1.0
gc := 1.0
b := 1.0 - 0.7*glow
mainOp.ColorM.Scale(r, gc, b, 0.95)
screen.DrawImage(img, mainOp)
}
func drawFadeOverlay(screen *ebiten.Image, r, g, b, alpha float64) {
if fadePixel == nil {
fadePixel = ebiten.NewImage(1, 1)
fadePixel.Fill(color.RGBA{R: 255, G: 255, B: 255, A: 255})
}
op := &ebiten.DrawImageOptions{}
op.GeoM.Scale(float64(screenWidth), float64(screenHeight))
op.ColorM.Scale(r, g, b, alpha)
screen.DrawImage(fadePixel, op)
}
func drawStartImage(screen *ebiten.Image, img *ebiten.Image) {
if img == nil {
return
}
w := float64(img.Bounds().Dx())
h := float64(img.Bounds().Dy())
sw := float64(screenWidth)
sh := float64(screenHeight)
if w == 0 || h == 0 {
return
}
scale := sw / w
if sh/h > scale {
scale = sh / h
}
op := &ebiten.DrawImageOptions{}
op.GeoM.Scale(scale, scale)
x := (sw - w*scale) / 2
y := (sh - h*scale) / 2
op.GeoM.Translate(x, y)
screen.DrawImage(img, op)
}
func drawStarfield(screen *ebiten.Image, stars []Star) {
for _, s := range stars {
if s.Halo {
ebitenutil.DrawRect(screen, s.X-1, s.Y-1, s.Size+2, s.Size+2, color.RGBA{R: 120, G: 140, B: 180, A: 80})
}
ebitenutil.DrawRect(screen, s.X, s.Y, s.Size, s.Size, color.RGBA{R: 220, G: 230, B: 255, A: 200})
}
}
func drawHUD(screen *ebiten.Image, g *Game) {
scale := 3.0
left := fmt.Sprintf("SCORE: %d", g.Score)
center := fmt.Sprintf("LEVEL %d", g.Level)
right := fmt.Sprintf("LIVES: %d", g.Lives)
drawHUDText(screen, left, 10, 8, scale)
centerWidth := hudTextWidth(center, scale)
drawHUDText(screen, center, screenWidth/2-centerWidth/2, 8, scale)
rightWidth := hudTextWidth(right, scale)
drawHUDText(screen, right, screenWidth-10-rightWidth, 8, scale)
}
func hudTextWidth(text string, scale float64) int {
baseW := len(text) * 7
return int(float64(baseW) * scale)
}
func drawHUDText(screen *ebiten.Image, text string, x, y int, scale float64) {
baseW := len(text) * 7
baseH := 16
img := ebiten.NewImage(baseW, baseH)
img.Fill(color.RGBA{0, 0, 0, 0})
ebitenutil.DebugPrintAt(img, text, 0, 4)
op := &ebiten.DrawImageOptions{}
op.GeoM.Scale(scale, scale)
op.GeoM.Translate(float64(x), float64(y))
screen.DrawImage(img, op)
}
func drawAmigaText(screen *ebiten.Image, font *AmigaFont, text string, centerX, y int, alpha float64, waveAmp float64, tick int) {
if font == nil || font.W == 0 {
return
}
scale := 2.0
spacing := 1
totalW := 0
for _, r := range text {
if r == ' ' || font.Glyphs[r] == nil {
totalW += int(float64(font.W+spacing) * scale)
continue
}
totalW += int(float64(font.W+spacing) * scale)
}
if totalW > 0 {
totalW -= int(float64(spacing) * scale)
}
x := centerX - totalW/2
if alpha < 0 {
alpha = 0
}
if alpha > 1 {
alpha = 1
}
for _, r := range text {
if r == ' ' || font.Glyphs[r] == nil {
x += int(float64(font.W+spacing) * scale)
continue
}
op := &ebiten.DrawImageOptions{}
op.GeoM.Scale(scale, scale)
offsetY := 0.0
if waveAmp > 0 {
offsetY = math.Sin(float64(tick)*0.12+float64(x)*0.03) * waveAmp
}
op.GeoM.Translate(float64(x), float64(y)+offsetY)
op.ColorM.Scale(1, 1, 1, alpha)
screen.DrawImage(font.Glyphs[r], op)
x += int(float64(font.W+spacing) * scale)
}
}
func drawAmigaTextZoom(screen *ebiten.Image, font *AmigaFont, text string, centerX, finalY int, tick, duration int, startScale, endScale float64) {
if font == nil || font.W == 0 {
return
}
if duration <= 0 {
duration = 1
}
spacing := 1
finalAdvance := (float64(font.W) + float64(spacing)) * endScale
totalW := finalAdvance*float64(len(text)) - float64(spacing)*endScale
x := float64(centerX) - totalW/2
startY := -float64(font.H)*startScale - 10
charDelay := 4
for i, r := range text {
if r == ' ' || font.Glyphs[r] == nil {
x += finalAdvance
continue
}
localTick := tick - i*charDelay
if localTick < 0 {
x += finalAdvance
continue
}
t := float64(localTick) / float64(duration)
if t > 1 {
t = 1
}
ease := easeOutQuad(t)
scale := startScale + (endScale-startScale)*ease
y := startY + (float64(finalY)-startY)*ease
offsetX := (finalAdvance - (float64(font.W)+float64(spacing))*scale) / 2
op := &ebiten.DrawImageOptions{}
op.GeoM.Scale(scale, scale)
op.GeoM.Translate(x+offsetX, y)
screen.DrawImage(font.Glyphs[r], op)
x += finalAdvance
}
}
func (g *Game) playerSpriteImage() *ebiten.Image {
sets := g.PlayerSprites
if g.Player.SuperFrames > 0 {
blinkWindow := g.Player.SuperFrames > playerSuperDurationFrames-playerSuperBlinkFirstFrames ||
g.Player.SuperFrames <= playerSuperBlinkLastFrames
if blinkWindow && (g.Player.SuperFrames/playerSuperBlinkPeriod)%2 == 1 {
sets = g.PlayerSprites
} else {
sets = g.PlayerSuperSprites
}
}
if sets.M == nil {
return nil
}
if g.Player.AnimStep == 0 {
return sets.M
}
if g.Player.AnimDir < 0 {
if g.Player.AnimStep == 1 {
return sets.L1
}
return sets.L2
}
if g.Player.AnimDir > 0 {
if g.Player.AnimStep == 1 {
return sets.R1
}
return sets.R2
}
return sets.M
}