-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.lua
More file actions
89 lines (74 loc) · 1.99 KB
/
Copy pathPlayer.lua
File metadata and controls
89 lines (74 loc) · 1.99 KB
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
require 'Bullet'
Player = {}
Player.yPos = 0 --some default values - not to be used
Player.xPos = 100 --some default values - not to be used
Player.bullets = {} --to be filled later
Player.speed = 100
Player.timeSinceLastBullet = 0
Player.bulletsPerSecond = 1.3
Player.moveStep = love.graphics.getHeight() / 2 --the step to move on a single key press
Player.height = 20
Player.width = 100
Player.imagePath = 'spaceship.png'
Player.image = nil
Player.score = 0
Player.scoreSinceLastBoss = 0
function Player:new(object, xPos, yPos)
local xPos = xPos or 0
local yPos = yPos or 0
local object = object or {}
setmetatable(object, self)
object.__index = self
Player.setXPos(object, xPos)
Player.setYPos(object, yPos)
return object
end
function Player:moveDown(dt)
if self.yPos + self.height + self.moveStep*dt <= love.graphics.getHeight() then
Player:moveYPos(self.moveStep*dt)
end
end
function Player:moveUp(dt)
if self.yPos - self.moveStep*dt > 0 then
Player:moveYPos(-self.moveStep*dt)
end
end
function Player:moveXPos(moveBy)
self.xPos = self.xPos + moveBy
end
function Player:moveYPos(moveBy)
self.yPos = self.yPos + moveBy
end
function Player:setXPos(xPos)
self.xPos = xPos
end
function Player:setYPos(yPos)
self.yPos = yPos
end
function Player:getXPos()
return self.xPos
end
function Player:getYPos()
return self.yPos
end
function Player:fire()
love.audio.play(shootingSound)
if self.timeSinceLastBullet < 1 / self.bulletsPerSecond then
return
end
local bulletX = self.xPos + self.width
local bulletY = self.yPos + self.height / 2
local bullet = Bullet.new(bulletX, bulletY, 1, 400)
self.bullets[bullet]=true
self.timeSinceLastBullet = 0
end
function Player:getRectangle ()
return {self.xPos, self.yPos, self.width, self.height}
end
function Player:changeScore (delta)
if self.score + delta <= 0 then
gameOver()
else
self.score = self.score + delta
end
end