Grid based movement
Posted: Tue Apr 23, 2024 1:02 pm
Hey there! I want to try to make a simple roguelike game, like Shiren The Wanderer. I want to make my character move grid for grid. And i thought about an implementation like this:
But for some reason, it does'nt work. Do i do something wrong? And would it be correct in its procedure?
Code: Select all
function love.load()
player = {
grid_x = 0,
grid_y = 0,
pos_x = 0,
pos_y = 0,
speed = 10,
size = 32,
moving = false
}
end
function love.update(dt)
if player.moving then
player.pos_x = player.pos_x + ((player.grid_x * player.size) - player.pos_x) * player.speed * dt
player.pos_y = player.pos_y + ((player.grid_y * player.size) - player.pos_y) * player.speed * dt
if math.abs(player.pos_x - player.grid_x * player.size) < 1 and math.abs(player.pos_y - player.grid_y * player.size) < 1 then
player.moving = false
player.pos_x = player.grid_x * player.size
player.pos_y = player.grid_y * player.size
end
end
end
function love.draw()
love.graphics.rectangle("fill", player.pos_x, player.pos_y, player.size, player.size)
end
function love.keypressed(key, scancode)
if not player.moving then
if key == "w" and player.grid_y > 0 then
player.grid_y = player.grid_y - 1
player.moving = true
elseif key == "s" and player.grid_y < 9 then
player.grid_y = player.grid_y + 1
player.moving = true
elseif key == "a" and player.grid_x > 0 then
player.grid_x = player.grid_x - 1
player.moving = true
elseif key == "d" and player.grid_x < 9 then
player.grid_x = player.grid_x + 1
player.moving = true
end
end
end