-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.lua
71 lines (60 loc) · 1.91 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
local player = {}
player.x = love.graphics:getWidth()/2
player.y = love.graphics:getHeight() - 70
player.width = 50
player.height = 50
player.speed = 190
player.velocity = 0
player.bullets = {}
function player.reset()
player.x = love.graphics:getWidth()/2
player.y = love.graphics:getHeight() - 70
player.width = 50
player.height = 50
player.speed = 150
player.bullets = {}
end
function player.update(dt)
-- Update player
if love.keyboard.isDown('right') and player.velocity < player.speed and player.x < love.graphics:getWidth() - player.width then
player.velocity = player.velocity + 550 * dt
elseif love.keyboard.isDown('left') and player.velocity > -player.speed and player.x > 0 then
player.velocity = player.velocity - 550 * dt
else
if player.velocity > -15 and player.velocity < 15 then
player.velocity = 0
elseif player.velocity > 0 then
player.velocity = player.velocity - 390 * dt
else
player.velocity = player.velocity + 390 * dt
end
end
player.x = player.x + player.velocity * dt
for i, bullet in ipairs(player.bullets) do
bullet.y = bullet.y - bullet.speed * dt
if bullet.y < 0 then bullet.dead = true end
end
for i=#player.bullets, 1, -1 do
local bullet = player.bullets[i]
if bullet.dead then table.remove(player.bullets, i) end
end
end
function player.draw()
love.graphics.rectangle('fill', player.x, player.y, player.width, player.height)
for i, bullet in ipairs(player.bullets) do
love.graphics.rectangle('fill', bullet.x, bullet.y, bullet.width, bullet.height)
end
end
function player.fireWeapon()
if #player.bullets < 2 then
local bullet = {}
bullet.x = player.x
bullet.y = player.y
bullet.width = 4
bullet.height = 14
bullet.speed = 370
bullet.dead = false
table.insert(player.bullets, bullet)
end
end
return player