forked from loganfechner/ap-performance-task
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.lua
351 lines (306 loc) · 8.49 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
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
local inspect = require "inspect"
local tilesize = require "tilesize"
local Animation = require "animation"
local Bullet = require "bullet"
local Shake = require "shake"
local SoundFX = require "soundfx"
local Timer = require "timer"
local Weapon = require "weapon"
local World = require "world"
local Player = {}
function Player:consts(health, ammo, speed, rof, minAtkPwr, maxAtkPwr)
self.maxHealth = health or 100
self.health = health
self.maxAmmunition = ammo or 50
self.maxSpeed = speed
self.speed = speed or 105
self.minAtkPwr = 15 or minAtkPwr
self.maxAtkPwr = 30 or maxAtkPwr
end
local image = love.graphics.newImage("img/tileset.png")
function Player:initialize(room, world, stats)
self.kind = "player"
--[[
self.bullet = {
list = {},
width = 6,
height = 6,
speed = 550,
minAtkPwr = self.minAtkPwr,
maxAtkPwr = self.maxAtkPwr
}
]]
self.weapon = Weapon.getWeapon("rifle", 25, 35)
self.bullet = self.weapon.bulletInfo
self.rof = self.weapon.rof
self:consts(
stats.health,
stats.ammo,
stats.speed,
stats.minAtkPwr,
stats.maxAtkPwr
)
self.x = math.floor(room.x + room.width / 2) * tilesize
self.y = math.floor(room.y + room.height / 2) * tilesize
self.width = tilesize
self.height = tilesize
self.walking = false
self.walkRate = 2*self.speed * 10^-3
self.walkTimer = Timer:new(self.walkRate)
self.shootTimer = Timer:new(self.rof)
self.canShoot = true
self.canContinue = false
self.ammunition = self.maxAmmunition
world:add(self, self.x, self.y, self.width, self.height)
self.animations = {
{
name = 'RunRight',
animation = Animation:new(image, 1, 4, self.speed * 10^-3, self.x, self.y)
},
{
name = 'RunLeft',
animation = Animation:new(image, 5, 8, self.speed * 10^-3, self.x, self.y)
},
{
name = 'RunUp',
animation = Animation:new(image, 9, 12, self.speed * 10^-3, self.x, self.y)
},
{
name = 'RunDown',
animation = Animation:new(image, 13, 16, self.speed * 10^-3, self.x, self.y)
},
{
name = 'IdleRight',
animation = Animation:new(image, 17, 18, .4, self.x, self.y)
},
{
name = 'IdleLeft',
animation = Animation:new(image, 19, 20, .4, self.x, self.y)
}
}
self.currentAnimation = "IdleRight"
self.dead = false
self.isHurt = false
self.rateOfHurt = .3
self.hurtTimer = Timer:new(self.rateOfHurt)
end
function Player:update(dt, world, drawList, enemies)
self:move(dt, world, drawList)
self:updateBullets(dt, world, drawList, enemies)
self:updateTimers(dt)
self:updateAnimations(dt)
self:updateDead()
end
function Player:updateAnimations(dt)
for _, v in ipairs(self.animations) do
if v.name == self.currentAnimation then
v.animation:update(dt)
end
end
end
local function collisionFilter(item, other)
if other.kind ~= "powerup" and other.kind ~= "exit" then
return "slide"
else
return "cross"
end
end
function Player:move(dt, world, drawList)
local dx, dy = 0, 0
if love.keyboard.isDown("w") then
dy = -self.speed
self.currentAnimation = "RunUp"
end
if love.keyboard.isDown("s") then
dy = self.speed
self.currentAnimation = "RunDown"
end
if love.keyboard.isDown("d") then
dx = self.speed
self.currentAnimation = "RunRight"
end
if love.keyboard.isDown("a") then
dx = -self.speed
self.currentAnimation = "RunLeft"
end
if dx == 0 and dy == 0 then
if self.currentAnimation == 'RunDown' or self.currentAnimation == 'RunLeft' then
self.currentAnimation = 'IdleLeft'
end
if self.currentAnimation == 'RunUp' or self.currentAnimation == 'RunRight' then
self.currentAnimation = 'IdleRight'
end
self.walking = false
else
self.walking = true
end
local goalX, goalY = self.x + dx * dt, self.y + dy * dt
self.x, self.y, cols, len = world:move(self, goalX, goalY, collisionFilter)
self:updateCollisions(cols, len, world, drawList)
for i = 1, len do
local col = cols[i]
-- print(inspect(col))
end
end
local function aabb(bullet, x, y, w, h)
return bullet.x1 + bullet.width > x and bullet.x1 < x + w and
bullet.y1 + bullet.height > y and bullet.y1 < y + h
end
local remove = table.remove
function Player:updateBullets(dt, world, drawList, enemies)
for i = #self.bullet.list, 1, -1 do
local bullet = self.bullet.list[i]
bullet:update(dt)
world:update(bullet, bullet.x1, bullet.y1)
for j = 1, #drawList do
local p = drawList[j]
if p.num == 1 then
local x, y = p.x * tilesize, p.y * tilesize
local w, h = tilesize, tilesize
if aabb(bullet, x, y, w, h) then
remove(self.bullet.list, i)
world:remove(bullet)
break
end
end
end
for j = 1, #enemies do
local enemy = enemies[j]
if aabb(bullet, enemy.x, enemy.y, enemy.width, enemy.height) then
enemy:hurtEnemy(bullet.atkPwr)
remove(self.bullet.list, i)
world:remove(bullet)
break
end
end
end
end
function Player:updateTimers(dt)
self.shootTimer:update(dt, function()
self.canShoot = true
end)
self.walkTimer:update(dt, function()
if self.walking then
SoundFX:play("walk")
end
end)
self.hurtTimer:update(dt, function()
self.isHurt = false
end, not self.isHurt)
end
local remove = table.remove
function Player:updateCollisions(cols, len, world, drawList)
for i = 1, len do
local col = cols[i]
if col.other.kind == "powerup" then
local name = col.other.name
if name == "ammunition" then
self.ammunition = col.other.ammunition(self.ammunition, self.maxAmmunition)
end
if name == "health" then
self.health = col.other.health(self.health, self.maxHealth)
end
if name == "speed" then
self.speed = col.other.speed(self.speed + 20)
if self.rof > .1 then
self.rof = self.rof - .008
end
end
SoundFX:play("powerup")
-- Remove powerup from world
world:remove(col.other)
local x, y = col.otherRect.x / col.otherRect.w, col.otherRect.y / col.otherRect.h
for i = #drawList, 1, -1 do
local p = drawList[i]
if p.x == x and p.y == y then
remove(drawList, i)
end
end
break
end
if col.other.kind == "exit" then
self.canContinue = true
end
end
if len <= 0 then
self.canContinue = false
end
end
function Player:updateDead()
if self.health <= 0 then
self.dead = true
SoundFX:play("killed")
self.health = 0
end
end
function Player:isDead()
return self.dead
end
function Player:fireBullet(key, world)
if self.canShoot and self.ammunition > 0 then
local width, height = self.bullet.width, self.bullet.height
local atkPwr = math.random(self.minAtkPwr, self.maxAtkPwr)
local speed = self.bullet.speed
if key == "up" then
local x = (self.x + self.width / 2) - (self.bullet.width / 2)
local y = self.y - self.bullet.height
local bullet = Bullet:new(x, y, x, y-5, width, height, atkPwr, speed)
self.bullet.list[#self.bullet.list+1] = bullet
world:add(bullet, x, y, width, height)
elseif key == "down" then
local x = (self.x + self.width / 2) - (self.bullet.width / 2)
local y = self.y + self.height
local bullet = Bullet:new(x, y, x, y+5, width, height, atkPwr, speed)
self.bullet.list[#self.bullet.list+1] = bullet
world:add(bullet, x, y, width, height)
elseif key == "right" then
local x = self.x + self.width
local y = (self.y + self.height / 2) - (self.bullet.height / 2)
local bullet = Bullet:new(x, y, x+5, y, width, height, atkPwr, speed)
self.bullet.list[#self.bullet.list+1] = bullet
world:add(bullet, x, y, width, height)
elseif key == "left" then
local x = self.x - self.bullet.width
local y = (self.y + self.height / 2) - (self.bullet.height / 2)
local bullet = Bullet:new(x, y, x-5, y, width, height, atkPwr, speed)
self.bullet.list[#self.bullet.list+1] = bullet
world:add(bullet, x, y, width, height)
end
if key == "left" or key == "up" or key == "down" or key == "right" then
SoundFX:play("shoot")
-- Shake.more(.75)
self.canShoot = false
self.ammunition = self.ammunition - 1
end
end
end
function Player:damagePlayer(atkPwr)
self.health = self.health - atkPwr
self.isHurt = true
SoundFX:play("hit", 1.0)
local growth = 10
Shake.more(growth)
end
function Player:getPosition()
return self.x, self.y
end
function Player:draw()
if not self.isHurt then
love.graphics.setColor(255, 255, 255)
else
love.graphics.setColor(255, 35, 35)
end
for _, v in ipairs(self.animations) do
if v.name == self.currentAnimation then
v.animation:draw(self.x, self.y)
end
end
if #self.bullet.list > 0 then
for i = 1, #self.bullet.list do
local bullet = self.bullet.list[i]
love.graphics.setColor(40,255,40)
love.graphics.rectangle("fill", bullet.x1, bullet.y1, bullet.width, bullet.height)
end
end
end
return Player