-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.lua
98 lines (85 loc) · 2.94 KB
/
Player.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
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
local Player = Class {__includes = Entity}
function Player:init(model)
Entity.init(self, 0, 0, model.stats.modelWidth, model.stats.modelHeight)
self.model = AnimationModel(model)
self.defaultStats = model.stats
self.attackSound = model.sounds.attack
self.hitSound = model.sounds.hit
self.deathSound = model.sounds.death
-- self.walkSound = model.sounds.walk
self:reset()
end
function Player:reset(x, y)
self.x = x or (GAME_WIDTH - self.width) / 2
self.y = y or (GAME_HEIGHT - self.height) / 2
self.hp = self.defaultStats.hitPoints
self.speed = self.defaultStats.moveSpeed
self.attackRate = self.defaultStats.attackRate
self.attackCD = 0
-- invincibility time after getting hit
self.iTime = self.defaultStats.invincibilityTime
self.hurtCD = 0
self.model:doo('idle')
self.model:face('south')
Entity.update(self)
end
function Player:update(dt)
-- get user input
local hMov, vMov, movDirection = dir8('w', 's', 'a', 'd')
local hAim, vAim, aimDirection = dir8('up', 'down', 'left', 'right')
-- update position
local ds = self.speed * dt
self.x = coerce(self.x + hMov * ds, 0, GAME_WIDTH - self.width)
self.y = coerce(self.y + vMov * ds, 0, GAME_HEIGHT - self.height)
Entity.update(self)
-- update cooldowns / timeouts
self.hurtCD = math.max(0, self.hurtCD - dt)
self.attackCD = math.max(0, self.attackCD - dt)
-- player state transitions
local finished = self.model.animationComplete
local action = self.model.doing
local orientation = self.model.facing
if finished then action = 'idle' end
if action == 'harm' then
-- stay in 'harm'
elseif #aimDirection > 0 and self.attackCD <= 0 then
self.attackCD = self.attackRate
self.attackSound:stop()
self.attackSound:play()
Projectile(self.x, self.y, hAim, vAim)
action, orientation = 'attacking', aimDirection
elseif action ~= 'attacking' then
if #movDirection > 0 then
action, orientation = 'walking', movDirection
-- self.walkSound:play()
else
action = 'idle'
-- self.walkSound:stop()
end
end
self.model:doo(action)
self.model:face(orientation)
self.model:update(dt)
end
function Player:hit(damage)
if self.hurtCD > 0 then return end
self.hp = self.hp - damage
if self.hp > 0 then
self.hurtCD = self.iTime
self.hitSound:stop()
self.hitSound:play()
self.model:doo('harm')
else
gGameState = 'gameover'
self.deathSound:play()
self.model:doOnce('die')
end
end
function Player:draw()
if self.hurtCD > 0 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 Player