-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.lua
91 lines (70 loc) · 1.96 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
Player = {}
Player.__indes = Player
function Player.new(x,y)
local pl = {}
setmetatable(pl, Player)
pl.x = love.graphics.getWidth()/2
pl.y = love.graphics.getHeight()/2
pl.image = love.graphics.newImage("/assets/images/player/player.png")
pl:setColor(1)
pl.facingDirection = 0
pl.playerWidth, pl.playerHeight = pl:getCurrentImage():getDimensions()
return pl
end
function Player:getX()
return self.x
end
function Player:getY()
return self.y
end
function Player:moveX(p)
-- 0 = right, 1 = down, 2 = left, 3 = up
if p < 0 then
self.facingDirection = 2
else
self.facingDirection = 0
end
self.x = self.x + p
-- Stop at borders
if self.x < 0 then
self.x = 0
end
if (self.x + self.playerWidth) > love.graphics.getWidth() then
self.x = love.graphics.getWidth() - self.playerWidth
end
end
function Player:moveY(pr)
-- 0 = right, 1 = down, 2 = left, 3 = up
if pr < 0 then
self.facingDirection = 3
else
self.facingDirection = 1
end
self.y = self.y + pr
-- Stop at borders
if self.y < 0 then
self.y = 0
end
if (self.y + self.playerHeight) > love.graphics.getHeight() then
self.y = love.graphics.getHeight() - self.playerHeight
end
end
function Player:draw()
love.graphics.setColor(self.color.r, self.color.g, self.color.b, self.color.a)
love.graphics.draw(self.x, self.y)
love.graphics.reset()
self.playerWidth, self.playerHeight = self:getDimensions()
end
function Player:setColor(currentElement)
local elementColors = {
-- red
{r = 1, g = 0, b = 0, a = 1},
-- blue
{r = 0, g = 0.8, b = 1, a = 1},
-- grey
{r = 0.5, g = 0.5, b = 0.5, a = 1},
-- yellow
{r = 1, g = 1, b = 0, a = 1}
}
self.color = elementColors[currentElement]
end