Trouble figuring out collisions for array-based maps
Posted: Sat Jan 27, 2018 7:11 pm
I'm working on grid-based movement and can't figure out collisions for the way I generate maps.
Here is the code for the game, it's basically a simple mock-up for movement and map creation:
I looked at the grid-locked movement tutorial, but I didn't like the way it handled collisions since the game crashes if the player leaves the map. Any ideas on the best way to implement simple collisions into this system? Thanks!
Here is the code for the game, it's basically a simple mock-up for movement and map creation:
Code: Select all
function love.load()
player = {
grid_x = 256,
grid_y = 256,
spd = 10,
walkable = false
}
map1 = {
{1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1}
}
end
function drawMap(map)
for y=1, #map do
for x=1, #map[y] do
if map[y][x] == 1 then
love.graphics.rectangle("line", x * 32, y * 32, 32 ,32)
end
end
end
end
function love.update(dt)
end
function love.draw()
drawMap(map1)
love.graphics.rectangle("fill", player.grid_x, player.grid_y, 32, 32)
end
function love.keypressed(key)
if key == "up" or key == "w" then
player.grid_y = player.grid_y - 32
elseif key == "down" or key == "s" then
player.grid_y = player.grid_y + 32
elseif key == "left" or key == "a" then
player.grid_x = player.grid_x - 32
elseif key == "right" or key == "d" then
player.grid_x = player.grid_x + 32
end
end