Below I've attached the player.lua file that we found on a youtube tutorial and modified. The tutorial abruptly ended after implementing moving left and right and it barely touched on the physics side of things, but I did a good amount of research on the subject, yet it just doesn't click in my mind.
From what I understand, I know that the physics is being constantly updated therefore producing the gravity and the updating formuale for moving left and right, but what do I need to do for jumping for a set time/length? Do I need to have it execute a thread/timer/co-routine? I've been trying to mess around with a canJump boolean, but I'm not hitting the mark.
Code: Select all
player = {}
function player.load()
player.x = 5
player.y = 5
player.width = 50
player.height = 50
player.xvel = 0
player.yvel = 0
player.friction = 7
player.speed = 2250
playerCanJump = true
hitFloor = false
end
function player.draw()
love.graphics.setColor(255,0,0)
love.graphics.rectangle("fill", player.x, player.y, player.width, player.height)
end
function player.physics(dt)
player.x = player.x + player.xvel * dt
player.y = player.y + player.yvel * dt
player.yvel = player.yvel + gravity * dt
player.xvel = player.xvel * (1 - math.min(dt*player.friction, 1))
--player.yvel = player.yvel * (1 - math.min(dt*player.friction, 1))
end
function player.move(dt)
if love.keyboard.isDown('right') and
player.xvel < player.speed then
player.xvel = player.xvel + player.speed * dt
end
if love.keyboard.isDown('left') and
player.xvel > -player.speed then
player.xvel = player.xvel - player.speed * dt
end
if love.keyboard.isDown('up') and playerCanJump then
player.yvel = player.yvel + player.speed * dt
playerCanJump = false
end
end
--[[function player.jump(dt)
if love.keyboard.isDown('up')
end
end]]
function player.boundary()
if player.x < 0 then
player.x = 0
player.xvel = 0
end
if player.x > 750 then
player.x = 750
player.xvel = 0
end
if player.y + player.height > groundlevel then
player.y = groundlevel - player.height
player.yvel = 0
playerCanJump = true
end
end
function UPDATE_PLAYER(dt)
player.physics(dt)
player.move(dt)
player.boundary()
end
function DRAW_PLAYER()
player.draw()
end