jump around

jump around
This commit is contained in:
Tyrel Souza 2018-01-17 23:17:31 -05:00
parent 7b30c93128
commit ae5da288ca
2 changed files with 38 additions and 6 deletions

View File

@ -1,6 +1,7 @@
--[[ LOVE STUFF ]] --[[ LOVE STUFF ]]
function love.load() function love.load()
myWorld = love.physics.newWorld(0, 100) myWorld = love.physics.newWorld(0, 500)
myWorld:setCallbacks(beginContact, endContact, preSolve, postSolve)
sprites = {} sprites = {}
sprites.coin_sheet = love.graphics.newImage('sprites/coin_sheet.png') sprites.coin_sheet = love.graphics.newImage('sprites/coin_sheet.png')
@ -16,21 +17,28 @@ end
function love.update(dt) function love.update(dt)
myWorld:update(dt) myWorld:update(dt)
playerUpdate(dt)
end end
function love.draw() function love.draw()
love.graphics.draw( love.graphics.draw(
sprites.player_stand, sprites.player_stand,
player.body:getX(), player.body:getX(),
player.body:getY(), player.body:getY(),
nil, nil, nil, nil, nil, nil,
sprites.player_stand:getWidth()/2, sprites.player_stand:getHeight()/2) sprites.player_stand:getWidth()/2, sprites.player_stand:getHeight()/2)
for i, p in ipairs(platforms) do for i, p in ipairs(platforms) do
love.graphics.rectangle("fill", p.body:getX(), p.body:getY(), p.width, p.height) love.graphics.rectangle("fill", p.body:getX(), p.body:getY(), p.width, p.height)
end end
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) function spawnPlatform(x, y, width, height)
local platform = {} local platform = {}
@ -42,3 +50,11 @@ function spawnPlatform(x, y, width, height)
table.insert(platforms, platform) table.insert(platforms, platform)
end end
function beginContact(a, b, coll)
player.grounded = true
end
function endContact(a, b, coll)
player.grounded = false
end

View File

@ -1,4 +1,20 @@
player = {} player = {}
-- Physics Engine
player.body = love.physics.newBody(myWorld, 100, 100, 'dynamic') player.body = love.physics.newBody(myWorld, 100, 100, 'dynamic')
player.shape = love.physics.newRectangleShape(66, 92) player.shape = love.physics.newRectangleShape(66, 92)
player.fixture = love.physics.newFixture(player.body, player.shape) 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