-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnemy.lua
68 lines (56 loc) · 1.81 KB
/
Enemy.lua
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
gMobs = {}
local Enemy = Class {__includes = Entity, map = {}, player = {}}
function Enemy:init(model, x, y)
Entity.init(self, x, y, model.stats.modelWidth, model.stats.modelHeight)
table.insert(gMobs, self)
self.model = AnimationModel(model)
self.attackPattern = model.attackPattern
self.speed = model.stats.moveSpeed
self.hp = model.stats.hitPoints
self.attackRate = model.stats.attackRate
self.attackCD = 0
-- invincibility time after getting hit
self.iTime = model.stats.invincibilityTime
self.hurtCD = 0
self.dying = false
self.attackSound = model.sounds.attack:clone()
self.hitSound = model.sounds.hit:clone()
end
function Enemy:update(dt)
if self.dying then
local done = self.model:update(dt)
if done then self:kill() end
return
end
if self.hurtCD > 0 then self.hurtCD = math.max(0, self.hurtCD - dt) end
local behaviour, orientation = self:attackPattern(dt)
self.model:doo(behaviour)
self.model:face(orientation)
Entity.update(self)
-- hit detection against player
if overlaps(self.boundingBox, self.player.boundingBox) then
self.player:hit(1)
end
self.model:update(dt)
end
function Enemy:hit(damage)
if self.hurtCD > 0 then return end
self.hp = self.hp - damage
self.hitSound:stop()
self.hitSound:play()
if self.hp > 0 then
self.hurtCD = self.iTime
self.model:doo('harm')
else
self.dying = true
self.model:doOnce('die')
end
end
function Enemy:draw()
if self.hurtCD > 0 or self.dying then
-- blink player by skipping some draw calls
if math.floor(love.timer.getTime()*10) % 2 == 0 then return end
end
self.model:draw(self.x, self.y)
end
return Enemy