-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.lua
289 lines (242 loc) · 7.64 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
local Player = {}
function Player:load()
self.x = 100
self.y = 0
self.startX = self.x
self.startY = self.y
self.width = 24
self.height = 30
self.xVel = 0
self.yVel = 100
self.maxSpeed = 200
self.acceleration = 4000 -- it will take 0.05 seconds to reach max speed - 200 / 4000 = 0.05
self.friction = 3500 -- it will take 0.057 seconds to stop from max speed - 200 / 3500 = 0.057
self.gravity = 1500 -- 1500 pixels per second squared
self.direction = "right"
self.state = "idle"
self.coins = 0
self.hearts = {
current = 3;
max = 3;
}
self.color ={
red = 255,
green = 255,
blue = 255,
speed = 300 -- this is for love2d 10, for 11.5 we use 3 and that is all
}
self.grounded = false
self.hasDoubleJump = true
self.jumpAmount = -600
self.graceTime = 0
self.graceDuration = 0.1
self.alive = true
self:loadAssets()
self.physics = {}
self.physics.body = love.physics.newBody(World, self.x, self.y, "dynamic")
self.physics.body:setFixedRotation(true) -- prevent player from rotating
self.physics.shape = love.physics.newRectangleShape(self.width, self.height)
self.physics.fixture = love.physics.newFixture(self.physics.body, self.physics.shape)
self.physics.body:setGravityScale(0) -- prevent player from falling
end
function Player:incrementCoins()
self.coins = self.coins + 1
end
function Player:update(dt)
self:unTint(dt)
self:respawn()
self:setState()
self:setDirection()
self:animate(dt)
self:decreaseGraceTime(dt)
self:syncPhysics()
self:move(dt)
self:applyGravity(dt)
end
function Player:applyGravity(dt)
if not self.grounded then
self.yVel = self.yVel + self.gravity * dt
end
end
function Player:applyFriction(dt)
if self.xVel > 0 then
self.xVel = math.max(self.xVel - self.friction * dt, 0)
elseif self.xVel < 0 then
self.xVel = math.min(self.xVel + self.friction * dt, 0)
end
end
function Player:syncPhysics()
self.x, self.y = self.physics.body:getPosition()
self.physics.body:setLinearVelocity(self.xVel, self.yVel)
end
function Player:move(dt)
if love.keyboard.isDown("right", "d") then
self.xVel = math.min(self.xVel + self.acceleration * dt, self.maxSpeed)
elseif love.keyboard.isDown("left", "a") then
self.xVel = math.max(self.xVel - self.acceleration * dt, -self.maxSpeed)
else
self:applyFriction(dt)
end
end
function Player:takeDamage(amount)
self.hearts.current = self.hearts.current - amount
self:tintRed()
if self.hearts.current <= 0 then
self.hearts.current = 0
self:die()
end
end
function Player:die()
self.alive = false
end
function Player:respawn()
if not self.alive then
self:resetPosition()
self.hearts.current = self.hearts.max
self.alive = true
end
end
function Player:resetPosition()
self.physics.body:setPosition(self.startX, self.startY)
end
function Player:beginContact(a, b, collision)
if (self.grounded) then return end
local nx, ny = collision:getNormal() -- returns coordinates of the collision normal
if a == self.physics.fixture then
if ny > 0 then
self:land(collision)
elseif ny < 0 then
self.yVel = 0
end
elseif b == self.physics.fixture then
if ny < 0 then
self:land(collision)
elseif ny > 0 then
self.yVel = 0
end
end
end
function Player:endContact(a, b, collision)
if a == self.physics.fixture or b == self.physics.fixture then
if(self.currentGroundCollision == collision) then
self.grounded = false
end
end
end
function Player:land(collision)
self.currentGroundCollision = collision
self.yVel = 0
self.grounded = true
self.hasDoubleJump = true
self.graceTime = self.graceDuration
end
function Player:jump(key)
if key == "up" or key == "w" then
if self.grounded or self.graceTime > 0 then
self.yVel = self.jumpAmount
self.grounded = false
self.graceTime = 0
SFX:playJump() -- Play jump sound
elseif self.hasDoubleJump then
self.yVel = self.jumpAmount * 0.75
self.hasDoubleJump = false
SFX:playJump() -- Play jump sound
end
end
end
function Player:decreaseGraceTime(dt)
if not self.grounded then
self.graceTime = self.graceTime - dt
end
end
function Player:loadAssets()
self.animation = {
timer = 0, -- time since the animation started
rate = 0.2 -- time between frames
}
self.animation.run = {
total = 4, -- number of frames in the animation
current = 1, -- current frame being displayed
img = {}
}
for i=1, self.animation.run.total do
self.animation.run.img[i] = love.graphics.newImage("assets/player/run/walk"..i..".png")
end
self.animation.air = {
total = 4, -- number of frames in the animation
current = 1, -- current frame being displayed
img = {}
}
for i=1, self.animation.air.total do
self.animation.air.img[i] = love.graphics.newImage("assets/player/idle/idle"..i..".png")
end
self.animation.idle = {
total = 4, -- number of frames in the animation
current = 1, -- current frame being displayed
img = {}
}
for i=1, self.animation.idle.total do
self.animation.idle.img[i] = love.graphics.newImage("assets/player/idle/idle"..i..".png")
end
self.animation.draw = self.animation.idle.img[1]
self.animation.width = self.animation.draw:getWidth()
self.animation.height = self.animation.draw:getHeight()
end
function Player:draw()
local scaleX = 1.5 -- Default scale for x is now 2
local scaleY = 1.5 -- Default scale for y is now 2
if self.direction == "left" then
scaleX = -1.5 -- Flipping and scaling the character in the x direction
end
-- Adjust the origin of scaling and rotation to be the center of the sprite
local originX = self.animation.width / 2
local originY = self.animation.height / 2
love.graphics.setColor(self.color.red, self.color.green, self.color.blue)
love.graphics.draw(self.animation.draw, self.x, self.y, 0, scaleX, scaleY, originX, originY )
love.graphics.setColor(255, 255, 255, 255)
end
function Player:tintRed()
self.color.red = 255
self.color.green = 0
self.color.blue = 0
end
function Player:unTint(dt)
self.color.red = math.min(self.color.red + self.color.speed * dt, 255)
self.color.green = math.min(self.color.green + self.color.speed * dt, 255)
self.color.blue = math.min(self.color.blue + self.color.speed * dt, 255)
end
function Player:animate(dt)
self.animation.timer = self.animation.timer + dt
if self.animation.timer > self.animation.rate then
self.animation.timer = 0
self:setNextFrame()
end
end
function Player:setNextFrame()
local anim = self.animation[self.state] -- this is a reference, it will mirror the changes made to self.animation
if(anim.current < anim.total) then
anim.current = anim.current + 1
else
anim.current = 1
end
self.animation.draw = anim.img[anim.current]
end
function Player:setDirection()
if self.xVel > 0 then
self.direction = "right"
elseif self.xVel < 0 then
self.direction = "left"
end
end
function Player:setState()
if self.grounded then
if self.xVel == 0 then
self.state = "idle"
else
self.state = "run"
end
else
self.state = "air"
end
end
return Player