-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.lua
146 lines (120 loc) · 2.83 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
-- player.lua
require "colors"
Player = { }
Player.__index = Player;
function Player:new( )
local this = {
paddle = {
height = 80,
width = 20,
position = {
x = 0,
y = 0
}
},
control = {
keyboard = {
up = "w",
down = "s",
repair = "x"
}
},
speed = 600, -- pixels per second
health = 100,
repair_timer = 0,
repair_delay = 2, -- in seconds
repair_in_cooldown = false,
score = 0,
color = Colors.Green
}
setmetatable(this, self)
return this
end
function Player:getDimensions( )
return self.paddle.width, self.paddle.height
end
function Player:setPosition( x, y )
self.paddle.position.x = x;
self.paddle.position.y = y;
end
function Player:getPosition( )
return self.paddle.position.x, self.paddle.position.y
end
function Player:setUpKey( up )
self.control.keyboard.up = up
end
function Player:getUpKey( )
return self.control.keyboard.up
end
function Player:setDownKey( down )
self.control.keyboard.down = down
end
function Player:getDownKey( )
return self.control.keyboard.down
end
function Player:setRepairKey( repair )
self.control.keyboard.repair = repair
end
function Player:setColor( t )
self.color = t;
end
function Player:getColor( )
return self.color
end
function Player:draw( )
love.graphics.setColor( self.color )
love.graphics.rectangle( 'fill', self.paddle.position.x, self.paddle.position.y, self.paddle.width, self.paddle.height)
end
function Player:update( dt )
local x, y = self:getPosition()
if not self.repair_in_cooldown then
if self.health > 75 then
self:setColor( Colors.Green )
elseif (self.health < 75 and self.health > 35) then
self:setColor( Colors.Yellow )
else
self:setColor( Colors.Red )
end
if love.keyboard.isDown(self.control.keyboard.up) then
y = y - self.speed*dt
end
if love.keyboard.isDown(self.control.keyboard.down) then
y = y + self.speed*dt
end
end
-- For testing cooldown mechanic
if self.repair_in_cooldown then
self.repair_timer = self.repair_timer + dt
if self.repair_timer >= self.repair_delay then
self.health = 100
self.repair_in_cooldown = false
self.repair_timer = 0
self:setColor(Colors.Green)
end
-- print("Timer " .. self.repair_timer)
end
if love.keyboard.isDown(self.control.keyboard.repair) and not self.repair_in_cooldown then
self:repair()
end
-- Check for boundaries
if y < 0 then
y = 0
elseif y + self.paddle.height > love.graphics.getHeight() then
y = love.graphics.getHeight() - self.paddle.height
end
self:setPosition(x, y)
end
function Player:repair( )
self:setColor(Colors.Blue)
self.repair_timer = 0
self.repair_in_cooldown = true
end
function Player:applyDamage( dmg )
self.health = self.health - dmg
end
function Player:getHealth( )
return self.health
end
function Player:getScore( )
return self.score
end