D'oh. I don't know how I missed it but those variables were indeed out of scope at that point.
I am running into another issue, though I'm not sure if it's related to Bump. In my little test, I'm trying to offset the collision box for the character. The initial position is correct (although very messily coded at the moment), but as soon as the character moves, the hitbox snaps to the character's origin.
I suspect it's because I'm not updating something correctly with the character movement, but I don't know if it's because I should add a separate item to the Bump world or I'm just overlooking some math when moving the character... or something else?
Code: Select all
local p -- player
local spritesheet
local anim8 = require 'lib.anim8.anim8'
local bump = require 'lib.bump.bump'
local sti = require 'lib.sti'
local map
local world
function love.load()
map = sti.new('map')
world = bump.newWorld()
love.graphics.setDefaultFilter('nearest', 'nearest')
spritesheet = love.graphics.newImage('placeholderguy.png')
local grid = anim8.newGrid(50, 48, spritesheet:getWidth(), spritesheet:getHeight())
p = {
spritesheet = spritesheet,
-- set initial player position to center
x = (love.graphics.getWidth() / 2) - 50,
y = (love.graphics.getHeight() / 2) - 48,
speed = 200,
width = 50, -- frame width
height = 48, -- frame height
hitX = ((love.graphics.getWidth() / 2) - 36), -- offset hitbox
hitY = ((love.graphics.getHeight() / 2) - 26),
hitW = 22, -- hitbox width
hitH = 19, -- hitbox height
animations = {
idle = anim8.newAnimation(grid(2, 1), 0.2), -- default state (single frame)
up = anim8.newAnimation(grid('5-8', 1), 0.2), -- play the first row of columns 5-8 for 0.2
down = anim8.newAnimation(grid('1-4', 1), 0.2),
left = anim8.newAnimation(grid('5-8', 2), 0.2),
right = anim8.newAnimation(grid('1-4', 2), 0.2)
}
}
p.animation = p.animations.idle -- set default to idle
-- testing collision walls
box = {x = 349, y = 115, w = 41, h = 144}
box2 = {x = 350, y = 115, w = 288, h = 23}
box3 = {x = 349, y = 322, w = 37, h = 156}
box4 = {x = 0, y = 0, w = 219, h = 52}
world:add(box, box.x, box.y, box.w, box.h)
world:add(box2, box2.x, box2.y, box2.w, box2.h)
world:add(box3, box3.x, box3.y, box3.w, box3.h)
world:add(p, p.hitX, p.hitY, p.hitW, p.hitH)
end
function love.draw()
map:draw()
p.animation:draw(p.spritesheet, p.x, p.y)
-- outline player frame
love.graphics.setColor(0,255,0)
love.graphics.rectangle('line', p.x, p.y, p.width, p.height)
-- show collision rectangles for debugging
love.graphics.setColor(255,0,0)
love.graphics.rectangle('line', world:getRect(p))
love.graphics.rectangle('line', world:getRect(box))
love.graphics.rectangle('line', world:getRect(box2))
love.graphics.rectangle('line', world:getRect(box3))
end
function love.update(dt)
map:update()
-- Player movement using Bump
if love.keyboard.isDown("a") then
local goalX, goalY
goalX = p.x - p.speed * dt
goalY = p.y
local actualX, actualY = world:move(p, goalX, goalY)
p.x, p.y = actualX, actualY
p.animation = p.animations.left
elseif love.keyboard.isDown("d") then
local goalX, goalY
goalX = p.x + p.speed * dt
goalY = p.y
local actualX, actualY = world:move(p, goalX, goalY)
p.x, p.y = actualX, actualY
p.animation = p.animations.right
elseif love.keyboard.isDown("w") then
local goalX, goalY
goalX = p.x
goalY = p.y - p.speed *dt
local actualX, actualY = world:move(p, goalX, goalY)
p.x, p.y = actualX, actualY
p.animation = p.animations.up
elseif love.keyboard.isDown("s") then
local goalX, goalY
goalX = p.x
goalY = p.y + p.speed *dt
local actualX, actualY = world:move(p, goalX, goalY)
p.x, p.y = actualX, actualY
p.animation = p.animations.down
else
p.animation = p.animations.idle
end
-- Collision detection to keep within window
if p.x < 0 then
p.x = 0
end
if p.x > love.graphics.getWidth() - p.width then
p.x = love.graphics.getWidth() - p.width
end
if p.y < 0 then
p.y = 0
end
if p.y > love.graphics.getHeight() - p.height then
p.y = love.graphics.getHeight() - p.height
end
p.animation:update(dt)
end