generated from sionleroux/ebitengine-game-template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
zombie.go
354 lines (307 loc) · 8.63 KB
/
zombie.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
// Use of this source code is subject to an MIT-style
// licence which can be found in the LICENSE file.
package main
import (
"errors"
"image"
"log"
"math"
"math/rand"
"time"
"github.com/hajimehoshi/ebiten/v2"
"github.com/solarlune/resolv"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
// zombieSpeed is the distance the zombie moves per update cycle
var zombieSpeed float64 = 0.4
// zombieCrawlerSpeed is the distance the crawler zombie moves per update cycle
var zombieCrawlerSpeed float64 = 0.2
// zombieSprinterSpeed is the distance the sprinter zombie moves per update cycle
var zombieSprinterSpeed float64 = 1.2
// zombieRange is how far away the zombie sees something to attack
var zombieRange float64 = 220
// Types of zombies
type ZombieType uint8
const (
zombieNormal ZombieType = iota
zombieCrawler
zombieSprinter
zombieBig
)
// Zombielike is anything that behaves like a zombie (e.g. attacks things and dies)
type Zombielike interface {
Update(*GameScreen) error
Draw(*GameScreen)
Hit(*GameScreen)
Die(*GameScreen)
Type() ZombieType
Remove()
Position() *Coord
}
// Zombies is an array of Zombie
type Zombies []Zombielike
// Update updates all the zombies
func (zs *Zombies) Update(g *GameScreen) {
zKilled := false
for i, z := range *zs {
err := z.Update(g)
if err != nil {
// clear and remove dead zombies
log.Println(err)
g.Zombies[i] = nil
g.Zombies = append((*zs)[:i], (*zs)[i+1:]...)
z.Remove()
zKilled = true
}
}
// If a zombie is killed and a kill voice can be played then it is played
if zKilled {
if g.Checkpoint > 0 && g.Checkpoint < 7 {
if g.NextVoiceStep == voiceStepKill && g.VoiceGuardTime > voiceGuardTime {
g.VoiceGuardTime = 0
g.NextVoiceStep++
g.Voices[voiceKill].PlayVariant(g.Checkpoint - 1)
}
}
}
}
func NewZombie(spawnpoint *SpawnPoint, position Coord, zombieType ZombieType, sprites *SpriteSheet) *Zombie {
// the head and shoulders are about 3px from the middle
const collisionBoxSize float64 = 6
var speed float64
var hitToDie int
switch zombieType {
case zombieNormal:
speed = zombieSpeed
hitToDie = 1 + rand.Intn(2)
case zombieCrawler:
speed = zombieCrawlerSpeed
hitToDie = 1 + rand.Intn(2)
case zombieSprinter:
speed = zombieSprinterSpeed
hitToDie = 1
case zombieBig:
speed = zombieSpeed
hitToDie = 10
}
dimensions := sprites.Sprite[0].Position
object := resolv.NewObject(
position.X, position.Y,
float64(dimensions.W), float64(dimensions.H),
tagMob,
)
object.SetShape(resolv.NewRectangle(
0, 0, // origin
collisionBoxSize, collisionBoxSize,
))
object.Shape.(*resolv.ConvexPolygon).RecenterPoints()
z := &Zombie{
Object: object,
Angle: 0,
Sprite: sprites,
Speed: speed * (1 + rand.Float64()),
HitToDie: hitToDie,
ZombieType: zombieType,
TempSpeed: 1,
}
z.Object.Data = z
z.SpawnPoint = spawnpoint
return z
}
// Draw draws all the zombies
func (zs Zombies) Draw(g *GameScreen) {
for _, z := range zs {
z.Draw(g)
}
}
// List of possible zombie states
const (
zombieIdle = iota // Doesn't have any target to attack
zombieWalking // Walking in some direction
zombieHit // Hit by a shot, but not deadly
zombieDeath // Plays the death animation
zombieDead // Marked as dead, will be removed on next Update
)
// Zombie is a monster that's trying to eat the player character
type Zombie struct {
Object *resolv.Object // Used for collision detection with other objects
Angle float64 // The angle the zombies is facing at
Frame int // The current animation frame
State int // The current animation state
Sprite *SpriteSheet // Used for zombie animations
Speed float64 // The speed this zombie walks at
TempSpeed float64 // Temporary speed multiplier
Target *resolv.Vector // Target vector (player or dog)
HitToDie int // Number of hits needed to die
ZombieType ZombieType // Type of the zombie
SpawnPoint *SpawnPoint // Reference for the SpawnPoint where the zombie was spawned
}
// Remove the zombie from the game's list of zombies and from the spawn point's
// list of live zombies
func (z *Zombie) Remove() {
if z.Object.Space != nil {
z.Object.Space.Remove(z.Object)
}
z.SpawnPoint.RemoveZombie(z)
}
// Position returns the current coordinates of the zombie
func (z *Zombie) Position() *Coord {
return &Coord{
X: z.Object.Position.X,
Y: z.Object.Position.Y,
}
}
// Type returns what type of zombie this is
func (z *Zombie) Type() ZombieType {
return z.ZombieType
}
// Update updates the state of the zombie
func (z *Zombie) Update(g *GameScreen) error {
if z.State == zombieDead {
return errors.New("Zombie died")
}
if z.State == zombieIdle || z.State == zombieWalking {
playerDistance, _, _ := CalcObjectDistance(z.Position(), g.Player.Position())
dogDistance, _, _ := CalcObjectDistance(z.Position(), g.Dog.Position())
zShouldWalk := false
if playerDistance < zombieRange {
z.Target = &g.Player.Object.Position
zShouldWalk = true
} else if dogDistance < zombieRange*1.2 {
z.Target = &g.Dog.Object.Position
zShouldWalk = true
}
if zShouldWalk {
if z.State == zombieIdle {
// Zombie detects target
if z.ZombieType == zombieNormal || z.ZombieType == zombieCrawler {
g.Sounds[soundZombieGrowl].Play()
} else if z.ZombieType == zombieSprinter {
g.Sounds[soundZombieScream].Play()
} else {
g.Sounds[soundBigZombieSound].Play()
}
}
z.walk()
} else {
z.State = zombieIdle
}
}
z.Frame = Animate(z.Frame, g.Tick, z.Sprite.Meta.FrameTags[z.State])
if z.Frame == z.Sprite.Meta.FrameTags[z.State].To {
z.animationBasedStateChanges(g)
}
z.Object.Shape.SetRotation(-z.Angle)
z.Object.Update()
return nil
}
func (z *Zombie) walk() {
// Zombies rotate towards their target
adjacent := z.Target.X - z.Object.Position.X
opposite := z.Target.Y - z.Object.Position.Y
z.Angle = math.Atan2(opposite, adjacent)
// Zombie movement logic
// TODO: this could be simplified using maths
if z.Object.Position.X < z.Target.X {
z.MoveRight()
}
if z.Object.Position.X > z.Target.X {
z.MoveLeft()
}
if z.Object.Position.Y < z.Target.Y {
z.MoveDown()
}
if z.Object.Position.Y > z.Target.Y {
z.MoveUp()
}
}
// Animation-trigged state changes
func (z *Zombie) animationBasedStateChanges(g *GameScreen) {
switch z.State {
case zombieHit:
z.State = zombieWalking
case zombieDeath:
z.State = zombieDead
}
}
// MoveUp moves the zombie upwards
func (z *Zombie) MoveUp() {
z.move(0, -z.Speed*z.TempSpeed)
}
// MoveDown moves the zombie downwards
func (z *Zombie) MoveDown() {
z.move(0, z.Speed*z.TempSpeed)
}
// MoveLeft moves the zombie left
func (z *Zombie) MoveLeft() {
z.move(-z.Speed*z.TempSpeed, 0)
}
// MoveRight moves the zombie right
func (z *Zombie) MoveRight() {
z.move(z.Speed*z.TempSpeed, 0)
}
// Move the Zombie by the given vector if it is possible to do so
func (z *Zombie) move(dx, dy float64) {
z.State = zombieWalking
if collision := z.Object.Check(dx, dy, tagMob, tagWall); collision == nil {
z.Object.Position.X += dx
z.Object.Position.Y += dy
}
// Collision detection and response between sand trap and zombie
z.TempSpeed = 1
if collision := z.Object.Check(0, 0, tagSandTrap); collision != nil {
if z.Object.Overlaps(collision.Objects[0]) {
if z.Object.Shape.Intersection(0, 0, collision.Objects[0].Shape) != nil {
z.TempSpeed = sandTrapSpeedMultiplier
}
}
}
}
// Draw draws the Zombie to the screen
func (z *Zombie) Draw(g *GameScreen) {
// -2, // the centre of the zombie's head is 2px up from the middle
const centerOffset float64 = 2
s := z.Sprite
frame := s.Sprite[z.Frame]
op := &ebiten.DrawImageOptions{}
// Centre and rotate
op.GeoM.Translate(
float64(-frame.Position.W/2),
float64(-frame.Position.H/2)+centerOffset/2,
)
op.GeoM.Rotate(z.Angle + math.Pi/2)
g.Camera.Surface.DrawImage(
s.Image.SubImage(image.Rect(
frame.Position.X,
frame.Position.Y,
frame.Position.X+frame.Position.W,
frame.Position.Y+frame.Position.H,
)).(*ebiten.Image),
g.Camera.GetTranslation(
op,
float64(z.Object.Position.X),
float64(z.Object.Position.Y),
),
)
}
// Hit changes zombie state and updates game data in response to it getting shot
func (z *Zombie) Hit(g *GameScreen) {
g.Stat.CounterZombiesHit++
z.State = zombieHit
z.HitToDie--
if z.HitToDie == 0 {
z.Die(g)
} else {
g.Sounds[soundHit].Play()
g.Sounds[soundZombieGrowl].Play()
}
}
// Die changes zombie state and updates game data in case of a deadly shot
func (z *Zombie) Die(g *GameScreen) {
g.Stat.CounterZombiesKilled++
g.Sounds[soundZombieDeath].Play()
z.Remove()
z.State = zombieDeath
}