From ae5da288ca98325ad9f1e0b1fa5369c991ab0155 Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Wed, 17 Jan 2018 23:17:31 -0500 Subject: [PATCH] jump around jump around --- main.lua | 28 ++++++++++++++++++++++------ player.lua | 16 ++++++++++++++++ 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/main.lua b/main.lua index d6dc6ff..ad1dd8c 100644 --- a/main.lua +++ b/main.lua @@ -1,6 +1,7 @@ --[[ LOVE STUFF ]] function love.load() - myWorld = love.physics.newWorld(0, 100) + myWorld = love.physics.newWorld(0, 500) + myWorld:setCallbacks(beginContact, endContact, preSolve, postSolve) sprites = {} sprites.coin_sheet = love.graphics.newImage('sprites/coin_sheet.png') @@ -16,21 +17,28 @@ end function love.update(dt) myWorld:update(dt) + playerUpdate(dt) end function love.draw() love.graphics.draw( - sprites.player_stand, - player.body:getX(), - player.body:getY(), - nil, nil, nil, - sprites.player_stand:getWidth()/2, sprites.player_stand:getHeight()/2) + sprites.player_stand, + player.body:getX(), + player.body:getY(), + nil, nil, nil, + sprites.player_stand:getWidth()/2, sprites.player_stand:getHeight()/2) for i, p in ipairs(platforms) do love.graphics.rectangle("fill", p.body:getX(), p.body:getY(), p.width, p.height) end end +function love.keypressed(key, scancode, isrepeat) + if key == "space" and player.grounded then + player.body:applyLinearImpulse(0, -2500) + end +end + function spawnPlatform(x, y, width, height) local platform = {} @@ -42,3 +50,11 @@ function spawnPlatform(x, y, width, height) table.insert(platforms, platform) end + + +function beginContact(a, b, coll) + player.grounded = true +end +function endContact(a, b, coll) + player.grounded = false +end diff --git a/player.lua b/player.lua index bf146c6..999ca5e 100644 --- a/player.lua +++ b/player.lua @@ -1,4 +1,20 @@ player = {} +-- Physics Engine player.body = love.physics.newBody(myWorld, 100, 100, 'dynamic') player.shape = love.physics.newRectangleShape(66, 92) player.fixture = love.physics.newFixture(player.body, player.shape) +-- +player.speed = 200 +player.grounded = false + + +function playerUpdate(dt) + + if love.keyboard.isDown('a') then + player.body:setX(player.body:getX() - player.speed * dt) + end + + if love.keyboard.isDown('d') then + player.body:setX(player.body:getX() + player.speed * dt) + end +end