lua-love-platformer/main.lua

62 lines
1.6 KiB
Lua
Raw Normal View History

2018-01-18 03:51:14 +00:00
--[[ LOVE STUFF ]]
function love.load()
2018-01-18 04:17:31 +00:00
myWorld = love.physics.newWorld(0, 500)
myWorld:setCallbacks(beginContact, endContact, preSolve, postSolve)
2018-01-18 03:51:14 +00:00
sprites = {}
sprites.coin_sheet = love.graphics.newImage('sprites/coin_sheet.png')
sprites.player_jump = love.graphics.newImage('sprites/player_jump.png')
sprites.player_stand = love.graphics.newImage('sprites/player_stand.png')
require('player')
2018-01-18 04:06:17 +00:00
platforms = {}
2018-01-18 04:21:42 +00:00
spawnPlatform(50, 500, 300, 30)
DIRECTION_LEFT = -1
DIRECTION_RIGHT = 1
2018-01-18 03:51:14 +00:00
end
function love.update(dt)
2018-01-18 04:06:17 +00:00
myWorld:update(dt)
2018-01-18 04:17:31 +00:00
playerUpdate(dt)
2018-01-18 03:51:14 +00:00
end
function love.draw()
2018-01-18 04:06:17 +00:00
love.graphics.draw(
2018-01-18 04:21:42 +00:00
player.sprite,
2018-01-18 04:17:31 +00:00
player.body:getX(),
player.body:getY(),
2018-01-18 04:21:42 +00:00
nil, player.direction, 1,
2018-01-18 04:17:31 +00:00
sprites.player_stand:getWidth()/2, sprites.player_stand:getHeight()/2)
2018-01-18 04:06:17 +00:00
for i, p in ipairs(platforms) do
love.graphics.rectangle("fill", p.body:getX(), p.body:getY(), p.width, p.height)
end
end
2018-01-18 04:17:31 +00:00
function love.keypressed(key, scancode, isrepeat)
if key == "space" and player.grounded then
player.body:applyLinearImpulse(0, -2500)
end
end
2018-01-18 04:06:17 +00:00
function spawnPlatform(x, y, width, height)
local platform = {}
platform.body = love.physics.newBody(myWorld, x, y, 'static')
platform.shape = love.physics.newRectangleShape(width/2, height/2, width, height)
platform.fixture = love.physics.newFixture(platform.body, platform.shape)
platform.width = width
platform.height = height
2018-01-18 03:51:14 +00:00
2018-01-18 04:06:17 +00:00
table.insert(platforms, platform)
2018-01-18 03:51:14 +00:00
end
2018-01-18 04:17:31 +00:00
function beginContact(a, b, coll)
player.grounded = true
end
function endContact(a, b, coll)
player.grounded = false
end