Mouvement and Looking

Added the ability to move left and right
Added the ability to sprint / run
Added Gravity
Added Jumping (Might remove later)
This commit is contained in:
Zaponium 2023-08-29 02:05:12 +02:00
commit 1c2fb970bb
2 changed files with 129 additions and 0 deletions

View file

@ -0,0 +1,86 @@
function love.load()
physics = require('physics')
window = {}
window.x, window.y = love.window.getDesktopDimensions()
love.window.setMode(window.x, window.y)
player = {
x = window.x/2-window.x/20,
y = window.y/2-window.y/20,
width = 20,
height = 80,
physics = {
velocityX = 0,
velocityY = 0,
airtime = 0,
terminalVelocity = 53
},
bools = {
isAirborne = true,
isWalking = false,
isRunning = false,
isFacingRight = true
},
attributes = {
maxRunningSpeed = 2
},
functions = {}
}
ground = {
x = 0,
y = window.y,
width = window.x,
height = 1
}
mouse = {}
function player.functions.getRunningSpeed(runningStatus) if runningStatus then return player.attributes.maxRunningSpeed*2 else return player.attributes.maxRunningSpeed end end
end
function love.update(dt)
input(player, dt)
gravity(player, dt)
mouvement(player, dt)
end
function love.draw()
love.graphics.rectangle("line", player.x, player.y, player.width, player.height)
if player.bools.isFacingRight then
love.graphics.rectangle("line", player.x+10, player.y-20, 20, 20)
else
love.graphics.rectangle("line", player.x-10, player.y-20, 20, 20)
end
end
function isTouching(hitbox1, hitbox2)
end
function input(object, dt)
object.bools.isWalking = false
object.bools.isRunning = false
mouse.x, mouse.y = love.mouse.getPosition
if love.keyboard.isDown("lshift") then
object.bools.isRunning = true
end
if love.keyboard.isDown("a") and love.keyboard.isDown("d") then
-- you're stopped now :3c
elseif love.keyboard.isDown("a") then
object.bools.isWalking = true
if math.abs(object.physics.velocityX) + 0.1 < object.functions.getRunningSpeed(object.bools.isRunning) then object.physics.velocityX = object.physics.velocityX - 0.1 else object.physics.velocityX = object.functions.getRunningSpeed(object.bools.isRunning)* -1 end
elseif love.keyboard.isDown("d") then
object.bools.isWalking = true
if math.abs(object.physics.velocityX) < object.functions.getRunningSpeed(object.bools.isRunning) then object.physics.velocityX = object.physics.velocityX + 0.1 else object.physics.velocityX = object.functions.getRunningSpeed(object.bools.isRunning) end
end
end
function love.mousemoved(x, y)
if x > player.x then
player.bools.isFacingRight = true
else
player.bools.isFacingRight = false
end
end
function love.keypressed(key)
if key == "space" then player.physics.velocityY = player.physics.velocityY - 3 end
end

View file

@ -0,0 +1,43 @@
function gravity(object, dt)
if object.bools.isAirborne == true then
object.physics.airtime = object.physics.airtime + dt
end
if clearanceCheck(object, dt) == "ground" then
object.y = ground.y - (object.height + 1)
object.physics.velocityY = 0
object.physics.airtime = 0
object.bools.isAirborne = false
elseif object.y ~= ground.y - (object.height + 1) then
object.bools.isAirborne = true
end
object.physics.gravity = 4.91*object.physics.airtime
if object.physics.velocityY + object.physics.gravity > object.physics.terminalVelocity then
object.physics.velocityY = object.physics.terminalVelocity
else
object.physics.velocityY = object.physics.velocityY + object.physics.gravity
end
end
function mouvement(object, dt)
object.y = object.y + object.physics.velocityY
object.x = object.x + object.physics.velocityX
if object.bools.isWalking == false and object.physics.velocityX ~=0 then
object.physics.velocityX = 0
end
if not object.bools.isRunning and math.abs(object.physics.velocityX) > object.attributes.maxRunningSpeed + 0.1 then
if object.physics.velocityX > 0 then
object.physics.velocityX = object.attributes.maxRunningSpeed
else
object.physics.velocityX = object.attributes.maxRunningSpeed * -1
end
end
end
function clearanceCheck(object, dt)
if ground.y <= object.y + object.height + object.physics.velocityY + 1 then
return "ground"
else
return "GTG"
end
end