Grid based movement

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Todespreis
Prole
Posts: 40
Joined: Sat Apr 01, 2023 9:30 pm

Grid based movement

Post by Todespreis »

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:

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
But for some reason, it does'nt work. Do i do something wrong? And would it be correct in its procedure?
User avatar
BrotSagtMist
Party member
Posts: 659
Joined: Fri Aug 06, 2021 10:30 pm

Re: Grid based movement

Post by BrotSagtMist »

What do you even mean with not work?
Runs here, moves.
obey
User avatar
darkfrei
Party member
Posts: 1204
Joined: Sat Feb 08, 2020 11:09 pm

Re: Grid based movement

Post by darkfrei »

Your code works, but try this code:

Code: Select all

function love.load()
	love.window.setTitle ('press WASD to move')
	gridSize = 64
	player = {
		-- Initial grid position of the player
		gridPos = {x=1, y=1},
		-- Change in grid position (due to movement)
		deltaGrid = {x=0, y=0},
		-- Movement speed of the player (cells per second)
		speed = 1,
		-- Flag to track if the player is currently moving
		moving = false,
	}
end

function love.update(dt)
	if player.moving then
		-- Calculate movement distance for this frame
		local delta = player.speed * dt
		-- Adjust deltaGrid values based on direction of movement
		if player.deltaGrid.x < 0 then
			player.deltaGrid.x = math.min(0, player.deltaGrid.x + delta)
		elseif player.deltaGrid.x > 0 then
			player.deltaGrid.x = math.max(0, player.deltaGrid.x - delta)
		end
		if player.deltaGrid.y < 0 then
			player.deltaGrid.y = math.min(0, player.deltaGrid.y + delta)
		elseif player.deltaGrid.y > 0 then
			player.deltaGrid.y = math.max(0, player.deltaGrid.y - delta)
		end

		-- Check if player has reached the target grid position
		if player.deltaGrid.x == 0 and player.deltaGrid.y == 0 then
			-- Player has stopped moving
			player.moving = false
		end
	end
end

function love.draw()
	-- Draw player's current position
	love.graphics.setColor (0.5,0.5,0.5)
	love.graphics.rectangle("fill", 
		(player.gridPos.x - 1)*gridSize, 
		(player.gridPos.y - 1)*gridSize, 
		gridSize, gridSize)

	-- Draw player's interpolated position during movement
	love.graphics.setColor (1,1,1)
	love.graphics.rectangle("fill", 
		(player.gridPos.x - player.deltaGrid.x - 1)*gridSize, 
		(player.gridPos.y - player.deltaGrid.y - 1)*gridSize, 
		gridSize, gridSize)
end

function love.keypressed(key, scancode)
	if key == "escape" then love.event.quit() end
	if player.moving then return end
	local scancodes = {
		w = {x=0,  y=-1},
		a = {x=-1, y=0},
		s = {x=0,  y=1},
		d = {x=1,  y=0},
	}
	local deltaGrid = scancodes[scancode]
	if deltaGrid then
		print ('scancode', scancode, 'deltaGrid', deltaGrid.x, deltaGrid.y)
		if player.deltaGrid.x == 0 then
			player.deltaGrid.y = player.deltaGrid.y + deltaGrid.y
			player.gridPos.y = player.gridPos.y + deltaGrid.y
		end
		if player.deltaGrid.y == 0 then
			player.deltaGrid.x = player.deltaGrid.x + deltaGrid.x
			player.gridPos.x = player.gridPos.x + deltaGrid.x
		end
		player.moving = true
	end
end
Animation (84).gif
Animation (84).gif (284.16 KiB) Viewed 1545 times
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Todespreis
Prole
Posts: 40
Joined: Sat Apr 01, 2023 9:30 pm

Re: Grid based movement

Post by Todespreis »

@darkfrei thanks! I'll try it out :D
@BrotSagtMist before putting it into my handheld, i'm trying it out on https://replit.com/@Todespreis/PlasticG ... s#main.lua
But there were no response for pressing the buttons, so i thought, i did something wrong
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 5 guests