/
objects.go
398 lines (340 loc) 路 8.8 KB
/
objects.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// Copyright 2020 Si么n le Roux. All rights reserved.
// Use of this source code is subject to an MIT-style
// licence which can be found in the LICENSE file.
package main
import (
"image"
"image/color"
"image/png"
"log"
"math"
"time"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/inpututil"
)
// An Object is something that can be seen and positioned in the game
type Object struct {
Image *ebiten.Image
Op *ebiten.DrawImageOptions
Center image.Point
Radius float64
}
// Overlaps reports whether o and p have a non-empty intersection
func (o *Object) Overlaps(p *Object) bool {
diff := o.Center.Sub(p.Center)
distance := math.Sqrt(math.Pow(float64(diff.X), 2) + math.Pow(float64(diff.Y), 2))
if distance <= o.Radius+p.Radius {
return true
}
return false
}
// NewObject makes a new game Object with fields calculated from the input image
func NewObject(filename string) *Object {
img := loadImage(filename)
return NewObjectFromImage(img)
}
// NewObjectFromImage makes a new game Object with fields calculated from an
// already loaded image
func NewObjectFromImage(img *ebiten.Image) *Object {
return &Object{
Image: img,
Op: &ebiten.DrawImageOptions{},
Center: image.Pt(0, 0),
Radius: float64(img.Bounds().Dx()) / 2,
}
}
// Moon is our moon, orbiting around the earth
type Moon struct {
*Object
*Turret
}
// Update recalculates moon position
func (o Moon) Update(g *Game) {
t := g.Rotation / MoonOrbitRatio
d := g.Earth.Radius + o.Radius*MoonOrbitDistance
// Calculated centre for collision detection
x := (d) * math.Cos(t)
y := (d) * math.Sin(t)
o.Center = image.Pt(
int(x)+g.Width/2,
int(y)+g.Height/2,
)
// Spin the moon
// Re-translate GeoM
o.Op.GeoM.Reset()
o.Op.GeoM.Translate(-o.Radius, -o.Radius)
o.Op.GeoM.Rotate(t)
o.Op.GeoM.Translate(o.Radius, o.Radius)
o.Op.GeoM.Translate(float64(o.Center.X), float64(o.Center.Y))
o.Op.GeoM.Translate(-o.Radius, -o.Radius)
for _, v := range g.Asteroids {
if o.Overlaps(v.Object) && v.Alive && !v.Explosion.Exploding {
v.Explosion.Exploding = true
g.Sounds.ExplsnHi.Rewind()
g.Sounds.ExplsnHi.Play()
g.Count--
}
}
o.Turret.Update(g)
}
// Draw renders a Moon to the screen
func (o *Moon) Draw(screen *ebiten.Image) {
screen.DrawImage(o.Image, o.Op)
o.Turret.Draw(screen)
}
// A Turret is a weapon on the moon that shoots lasers
type Turret struct {
*Object
Angle float64
}
// Update calculates Turrent game logic
func (o *Turret) Update(g *Game) {
o.Center = g.Moon.Center
// Calculate rotation towards crosshair if it's not cooling down
if !g.Crosshair.CoolingDown {
adjacent := float64(o.Center.X - g.Crosshair.Center.X)
opposite := float64(o.Center.Y - g.Crosshair.Center.Y)
o.Angle = math.Atan2(opposite, adjacent)
}
o.Op.GeoM.Reset()
// Spin
o.Op.GeoM.Translate(-o.Radius, -o.Radius)
o.Op.GeoM.Rotate(o.Angle)
o.Op.GeoM.Translate(o.Radius, o.Radius)
// Reposition
o.Op.GeoM.Translate(float64(o.Center.X), float64(o.Center.Y))
o.Op.GeoM.Translate(-o.Radius, -o.Radius)
}
// Draw renders a Turret to the screen
func (o *Turret) Draw(screen *ebiten.Image) {
screen.DrawImage(o.Image, o.Op)
}
// Earth is the earth, our home planet
type Earth struct {
*Object
Center image.Point
Impacted bool
}
// Update repositions Earth
func (o Earth) Update(g *Game) {
o.Op.GeoM.Reset()
o.Op.GeoM.Translate(
-o.Radius,
-o.Radius,
)
o.Op.GeoM.Rotate(g.Rotation)
o.Op.GeoM.Translate(o.Pt())
}
// Draw renders a Earth to the screen
func (o *Earth) Draw(screen *ebiten.Image) {
if !o.Impacted {
screen.DrawImage(o.Image, o.Op)
}
}
// Pt is a shortcut for the Earth's X and Y coordinates
func (o Earth) Pt() (X, Y float64) {
return float64(o.Center.X), float64(o.Center.Y)
}
// Asteroid is an asteroid on impact course with the Earth
type Asteroid struct {
*Object
Angle float64
Distance float64
Explosion *Explosion
Alive bool
Impacting bool
}
// Update recalculates Asteroid position
func (o *Asteroid) Update(g *Game) {
var RotationSpeed float64 = AsteroidSpinRatio
// Asteroid impacts earth
if o.Distance > 0 {
o.Distance = o.Distance - 1
} else if o.Alive {
o.Impacting = true
o.Explosion.Exploding = true
}
// Calculated centre for collision detection
t := o.Angle
d := o.Distance + g.Earth.Radius
x := (d) * math.Cos(t)
y := (d) * math.Sin(t)
o.Center = image.Pt(
int(x)+g.Width/2,
int(y)+g.Height/2,
)
// Re-translate GeoM
o.Op.GeoM.Reset()
// Spin the asteroid
o.Op.GeoM.Translate(-o.Radius, -o.Radius)
o.Op.GeoM.Rotate(g.Rotation * RotationSpeed)
o.Op.GeoM.Translate(o.Radius, o.Radius)
// Move to newly calculated x, y with image offset to center
o.Op.GeoM.Translate(float64(o.Center.X), float64(o.Center.Y))
o.Op.GeoM.Translate(-o.Radius, -o.Radius)
// Handle Explosion
o.Explosion.Update(g, o.Center)
if o.Explosion.Done && o.Alive {
o.Alive = false
}
}
// Draw renders a Asteroid to the screen
func (o *Asteroid) Draw(screen *ebiten.Image) {
if o.Alive {
screen.DrawImage(o.Image, o.Op)
o.Explosion.Draw(screen)
}
}
// Asteroids are multiple of a single Asteroid
type Asteroids []*Asteroid
// Update updates all the Asteroids
func (as Asteroids) Update(g *Game) {
for _, v := range as {
v.Update(g)
}
// TODO: delete dead asteroids
// append(s[:index], s[index+1:]...)
}
// Draw updates all the Asteroids
func (as Asteroids) Draw(screen *ebiten.Image) {
for _, v := range as {
v.Draw(screen)
}
}
// Alive returns true if any Asteroids are alive
func (as Asteroids) Alive() bool {
for _, v := range as {
if v.Alive {
return true
}
}
return false
}
// Impacting returns true if any Asteroids are impacting
func (as Asteroids) Impacting() bool {
for _, v := range as {
if v.Impacting {
return true
}
}
return false
}
// An Explosion is an animated impact explosion
type Explosion struct {
*Object
Frame int
Exploding bool
Done bool
}
// Update sets positioning and animation for Explosions
func (o *Explosion) Update(g *Game, coords image.Point) {
o.Center.X, o.Center.Y = coords.X, coords.Y
o.Op.GeoM.Reset()
o.Op.GeoM.Translate(float64(o.Center.X), float64(o.Center.Y))
o.Op.GeoM.Translate(-o.Radius, -o.Radius)
if o.Exploding {
if o.Frame < 7 {
o.Frame++
} else {
o.Frame = 1
o.Exploding = false
o.Done = true
}
}
}
// Draw renders an Explosion to the screen
func (o *Explosion) Draw(screen *ebiten.Image) {
const frameSize int = 87
if o.Exploding {
screen.DrawImage(o.Image.SubImage(image.Rect(
o.Frame*frameSize, 0, // top-left
(1+o.Frame)*frameSize, frameSize, // bottom-right
)).(*ebiten.Image), o.Op)
}
}
// The Crosshair is a target showing where the the player will shoot
type Crosshair struct {
*Object
CoolingDown bool
Shooting bool
Missing bool
ShootingFrom image.Point
Explosion *Explosion
}
// Update recalculates the crosshair position
func (o *Crosshair) Update(g *Game) {
o.Shooting = false
o.Missing = false
o.Op.GeoM.Reset()
o.Center = image.Pt(ebiten.CursorPosition())
o.Op.GeoM.Translate(
float64(o.Center.X)-o.Radius,
float64(o.Center.Y)-o.Radius,
)
canShoot := !g.Breathless && !o.CoolingDown && !g.GameOver && g.Wave > 0
if canShoot && clicked() {
o.Missing = true
o.Shooting = true
o.ShootingFrom = g.Moon.Center
g.Sounds.Laser.Rewind()
g.Sounds.Laser.Play()
for _, v := range g.Asteroids {
if o.Overlaps(v.Object) && v.Alive && !v.Explosion.Exploding {
v.Explosion.Exploding = true
soundEffectDelay := time.NewTimer(time.Millisecond * 100)
go func() {
<-soundEffectDelay.C
g.Sounds.ExplsnMid.Rewind()
g.Sounds.ExplsnMid.Play()
}()
g.Count--
o.Missing = false
}
}
}
if o.Missing {
o.CoolingDown = true
o.Explosion.Exploding = true
coolDownTimer := time.NewTimer(time.Second)
go func() {
<-coolDownTimer.C
o.CoolingDown = false
}()
}
o.Explosion.Update(g, g.Moon.Center)
}
// Draw renders a Crosshair to the screen
func (o *Crosshair) Draw(screen *ebiten.Image) {
screen.DrawImage(o.Image, o.Op)
o.Explosion.Draw(screen)
// Draw laser from the moon to the crosshair
if o.Shooting {
ebitenutil.DrawLine(
screen,
float64(o.ShootingFrom.X),
float64(o.ShootingFrom.Y),
float64(o.Center.X),
float64(o.Center.Y),
color.RGBA{255, 0, 0, 255},
)
}
}
// Shorthand for when the left mouse button has just been clicked
func clicked() bool {
return inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft)
}
// Load an image from embedded FS into an ebiten Image object
func loadImage(name string) *ebiten.Image {
log.Printf("loading %s\n", name)
file, err := assets.Open(name)
if err != nil {
log.Fatalf("error opening file %s: %v\n", name, err)
}
defer file.Close()
raw, err := png.Decode(file)
if err != nil {
log.Fatalf("error decoding file %s as PNG: %v\n", name, err)
}
return ebiten.NewImageFromImage(raw)
}