I've spent most of today learning about Lua and Löve syntax, but I just can't figure out the proper usage of Bump. I've read a bunch of guides, both on this forum and the entire GitHub page, but it's just not really clicking with me. What I'm used to, from using Gamemaker for so long, is checking collision with place_meeting. For people that haven't used GML, place_meeting's usage is like this:
Code: Select all
if place_meeting(x,y,object){ }
Now that that's all out of the way, onto my confusion with Bump.
I somehow managed to get basic collisions working with Bump, but I don't completely understand how they work still? What I assume is happening, at least in my usage of it (below), is that it stops player.x and player.y from being changed if collision is detected, but I'm not completely understanding *how* collision is BEING detected.
main.lua
Code: Select all
local STI = require "sti"
local bump = require "bump"
require("player")
function lerp(pos1, pos2, perc)
return (1-perc)*pos1 + perc*pos2
end
function bool_to_number(value)
return value and 1 or 0
end
function love.load()
map = STI("map/map1.lua", {"bump"})
world = bump.newWorld(16)
map:bump_init(world)
player:load()
world:add(player, player.x, player.y, player.w, player.h)
end
function love.update(dt)
map:update(dt)
player:update(dt)
end
function love.draw()
love.graphics.setBackgroundColor(0.5,0.5,0.5)
map:draw(0,0,1,1)
player:draw()
end
player.lua
Code: Select all
player = {}
function player:load()
self.x = 40
self.y = 40
self.w = 12
self.h = 12
self.hspd = 0
self.vspd = 0
self.movespd = 40
self.fric = 30
self.spdmax = 125
end
function player:update(dt)
if love.keyboard.isDown("left") or love.keyboard.isDown("right") then
if math.abs(self.hspd) <= (self.spdmax)*dt then
self.hspd = self.hspd+((self.movespd*dt)*(bool_to_number(love.keyboard.isDown("right"))-bool_to_number(love.keyboard.isDown("left"))))
else
self.hspd = ((self.spdmax*dt)*(bool_to_number(love.keyboard.isDown("right"))-bool_to_number(love.keyboard.isDown("left"))))
end
else
self.hspd = lerp(self.hspd,0,self.fric*dt)
end
self.x, self.y = world:move(self,self.x+self.hspd,self.y+self.vspd)
end
function player:draw()
love.graphics.setColor(1,0,0)
love.graphics.rectangle("fill",self.x,self.y-self.h,self.w,self.h)
end
Specifically, I'm trying to write code that will check if there is a wall to the left/right of the player while they're moving, so I can set the player.hspd/player.vspd variables to 0, but I don't understand how to check for that?
In Gamemaker, I would just do:
Code: Select all
if place_meeting(x+hspd+(1*sign(hspd)),y,solid){
hspd = 0
}
Apologies for such a long post, I wanted to be as verbose as possible with my understanding of Bump, and my history of coding to explain *why* I'm having a hard time understanding it.
I don't understand what .love files are for, if they are a way to access your source code, or if they're just a standard build of the game with obfuscated code, but I've added one anyway just in case. Either way, ALL the code for my game (excluding STI and Bump obviously) is included above.