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