edit again: ok nvm that! now there seems to be a weird drop in fps when colliding with things and moving either left or right while trying to collide with them - here's my code:
Code: Select all
local bump = require 'bump'
local cols_len = 0
local world = bump.newWorld()
local allowmove = false
local sprite = {}
local consoleBuffer = {}
local consoleBufferSize = 15
for i=1,consoleBufferSize do consoleBuffer[i] = "" end
local function consolePrint(msg)
table.remove(consoleBuffer,1)
consoleBuffer[consoleBufferSize] = msg
end
-- helper function
local function drawBox(box, r,g,b)
love.graphics.setColor(r,g,b,70)
love.graphics.rectangle("fill", box.x, box.y, box.w, box.h)
love.graphics.setColor(r,g,b)
love.graphics.rectangle("line", box.x, box.y, box.w, box.h)
love.graphics.setColor(255,255,255,100)
end
local player = { x=50,y=50,w=20,h=20, speed = 80 }
local function updatePlayer(dt)
local dx, dy = 0, 0
local speed = player.speed
--local goalX, goalY = player.x + dx * dt, player.y + dy * dt
--local actualX, actualY, cols, len = world:move(player, goalX, goalY)
player.x, player.y, cols, cols_len = world:move(player, player.x + dx, player.y + dy)
--player.x, player.y = actualX, actualY
--for i=1,len do
--print('collided with ' .. tostring(cols[i].other))
--end
if love.keyboard.isDown('right') then
dx = speed * dt
elseif love.keyboard.isDown('left') then
dx = -speed * dt
end
if love.keyboard.isDown('down') then
dy = speed * dt
elseif love.keyboard.isDown('up') then
dy = -speed * dt
end
if dx ~= 0 or dy ~= 0 then
player.x, player.y, cols, cols_len = world:move(player, player.x + dx, player.y + dy)
--player.x, player.y = actualX, actualY
end
end
local function drawPlayer()
drawBox(player, 0, 255, 0)
end
-- Block functions
local blocks = {}
local function drawBlocks()
for _,block in ipairs(blocks) do
drawBox(block, 255,0,0)
end
end
function sprite:load()
world:add(player, player.x, player.y, player.w, player.h)
end
function sprite:update(dt)
updatePlayer(dt)
end
function sprite:draw()
drawPlayer()
end
function sprite:keypressed(k)
if k=="escape" then love.event.quit() end
end
function sprite:add_collision(item, x, y, w ,h)
world:add(item, x, y, w, h)
end
function sprite:move_false()
allowmove = false
end
function sprite:move_true()
allowmove = true
end
function sprite:playery()
return player.y
end
function sprite:playerx()
return player.x
end
return sprite
---local sprite = {}
---local allowmove = false
---function sprite:move_false()
--- allowmove = false
---end
---function sprite:move_true()
--- allowmove = true
---end
---function sprite:playery()
--- return player.y
---end
---function sprite:playerx()
--- return player.x
---end
---function sprite:load()
---Camera = require 'camera'
---camera = Camera()
---camera:setFollowLerp(0.2)
---camera:setFollowLead(0)
---camera:setFollowStyle('TOPDOWN')
---love.mouse.setCursor(love.mouse.getSystemCursor("crosshair"))
---end
---function sprite:update(dt)
---camera:follow(player.x, player.y, -0, 1, 1, player.image:getWidth()/2, player.image:getHeight()/2)
---camera:update(dt)
---if allowmove == true then
---if love.keyboard.isDown("s") then
---camera:shake(2, 1, 100)
---end
---if love.keyboard.isDown("left") then
---dx = dx - 1
--player.x = player.x - 80 * dt
--themap:moveMap(90 * dt, 0)
---elseif love.keyboard.isDown("right") then
---dx = dx + 1
--player.x = player.x + 80 * dt
--themap:moveMap(-90 * dt, 0)
---end
---if love.keyboard.isDown("up") then
---dy = dy - 1
--player.y = player.y - 80 * dt
--themap:moveMap(0, 90 * dt)
---elseif love.keyboard.isDown("down") then
---dy = dy + 1
--player.y = player.y + 80 * dt
--themap:moveMap(0, -90 * dt)
---end
---end
---function sprite:draw()
---camera:attach()
---love.graphics.draw(tilesetBatch)
---love.graphics.print("FPS: "..love.timer.getFPS(), 10, 20)
---camera:detach()
---camera:draw()
---end
---end