lua-love-platformer/main.lua

126 lines
2.9 KiB
Lua
Raw Normal View History

2018-01-18 03:51:14 +00:00
--[[ LOVE STUFF ]]
function love.load()
2018-01-18 05:30:01 +00:00
gameState = "menu"
myFont = love.graphics.newFont(30)
2018-01-18 05:41:26 +00:00
timer = 0
2018-01-18 05:30:01 +00:00
2018-01-18 05:41:26 +00:00
love.window.setMode(700, 700)
2018-01-18 04:38:37 +00:00
myWorld = love.physics.newWorld(0, 500, false)
2018-01-18 04:17:31 +00:00
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 05:41:26 +00:00
resetPlayerPosition()
2018-01-18 04:38:37 +00:00
require('coin')
anim8 = require 'anim8'
2018-01-18 04:49:29 +00:00
sti = require 'sti'
2018-01-18 05:30:01 +00:00
camera = require 'camera'
2018-01-18 04:06:17 +00:00
platforms = {}
2018-01-18 05:30:01 +00:00
cam = camera()
2018-01-18 05:19:17 +00:00
gameMap = sti("maps/map.lua", { "box2d" })
for i, obj in pairs(gameMap.layers['platforms'].objects) do
spawnPlatform(obj.x, obj.y, obj.width, obj.height)
end
2018-01-18 05:41:26 +00:00
spawnCoins()
2018-01-18 05:19:17 +00:00
2018-01-18 04:21:42 +00:00
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 05:19:17 +00:00
gameMap:update(dt)
coinUpdate(dt)
2018-01-18 04:38:37 +00:00
2018-01-18 05:30:01 +00:00
cam:lookAt(player.body:getX(), love.graphics.getHeight()/2)
2018-01-18 04:38:37 +00:00
for i,c in ipairs(coins) do
c.animation:update(dt)
end
2018-01-18 05:41:26 +00:00
if gameState == "game" then
timer = timer + dt
end
if #coins == 0 and gameState == "game" then
gameState = "menu"
resetPlayerPosition()
spawnCoins()
end
2018-01-18 03:51:14 +00:00
end
function love.draw()
2018-01-18 05:30:01 +00:00
cam:attach()
2018-01-18 05:19:17 +00:00
gameMap:drawLayer(gameMap.layers['Tile Layer 1'])
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
2018-01-18 04:38:37 +00:00
for i, c in ipairs(coins) do
2018-01-18 05:19:17 +00:00
c.animation:draw(sprites.coin_sheet, c.x, c.y, nil, nil, nil, 20.5, 21)
2018-01-18 04:38:37 +00:00
end
2018-01-18 05:30:01 +00:00
cam:detach()
if gameState == "menu" then
love.graphics.setFont(myFont)
love.graphics.printf("Press any key to begin!", 0, 50, love.graphics.getWidth(), "center")
end
2018-01-18 05:41:26 +00:00
if gameState == "game" then
love.graphics.print("Time:" .. math.ceil(timer), 10, 660)
end
2018-01-18 04:06:17 +00:00
end
2018-01-18 04:17:31 +00:00
function love.keypressed(key, scancode, isrepeat)
if key == "space" and player.grounded then
2018-01-18 05:19:17 +00:00
player.body:applyLinearImpulse(0, -2800)
2018-01-18 04:17:31 +00:00
end
2018-01-18 05:30:01 +00:00
if gameState == "menu" then
gameState = "game"
2018-01-18 05:41:26 +00:00
timer = 0
2018-01-18 05:30:01 +00:00
end
2018-01-18 04:17:31 +00:00
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
2018-01-18 05:19:17 +00:00
function distanceBetweenObjects(obj1, obj2)
return distanceBetween(obj1.x, obj1.y, obj2.x, obj2.y)
end
function distanceBetween(x1, y1, x2, y2)
return math.sqrt((y2 - y1)^2 + (x2 - x1)^2)
end