string map

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
Saegor
Party member
Posts: 119
Joined: Thu Nov 08, 2012 9:26 am
Location: Charleroi

string map

Post by Saegor »

first of all : thanks kikito
you made my day with your tutorial (https://github.com/kikito/love-tile-tutorial) about tiles maps with löve

i strongly appreciate the way you load map stored in a string so i dived a little into the code and i have now a minimal tech demo to share. it support tabbing the strings so it's a little cleaner but all credits goes to you

try the .love at the bottom ; press tab to convert cells

here is the code :

Code: Select all

function love.load()

	gr = love.graphics
	gr.setFont(gr.newFont(32))

	local map_string = [[
	
		 #####
		##.b.######
		#....+e...#
		#o...#....#
		#....#...##
		#.@..###y#
		#....# ###
		######
	]]

	map = map_load(map_string)
end

function love.draw()

	map_print(map)
end


function map_load(map_string)

	-- init local map and row/column counters
	local map, gx, gy = {}, 1, 1
	
	-- for each line readed
	for this_line in map_string:gmatch("[^\t\n]+") do

		-- restart the column counter
		gx = 1

		-- fill map with empty rows
		map[gy] = {}
		
		-- for each char
		for this_char in this_line:gmatch(".") do

			-- store it in map
			map[gy][gx] = this_char
			
			-- go to next table column
			gx = gx + 1
		end

		-- go to next table row
		gy = gy + 1
	end

	return map
end

function map_print(map)

	-- for each row
	for gy in ipairs(map) do
	
		-- for each column
		for gx, v in ipairs(map[gy]) do
		
			-- if tab is pressed, convert
			if love.keyboard.isDown("tab") then
				
				v =
				v == "." and 0 or
				v == "#" and 1 or 
				v == "+" and -7 or 
				v == "@" and 42 or
				v == "o" and 4 or
				v == "b" and 5 or
				v == "e" and 6 or
				v == "y" and 7 or v
			end

			-- print map
			gr.print(v, gx * 0x40, gy * 0x40)
		end
	end
end
EDIT : man thanks you again (and again)
my isömaps are so clean now !

see that !

Code: Select all

return { [[

	..................
	..................
	..................
	..................
	..................
	..................
	..................
	..................
	..................
	..................
	..................
	..................
	..................
	..................
	..................

]], [[

	..................
	..................
	..................
	..................
	..................
	..................
	~~~~~~~~~~~~~~~~~~
	~~~~~~~~~~~~~~~~~~
	~~~~~~~~~~~~~~~~~~
	~~~~~~~~~~~~~~~~~~
	..................
	..................
	..................
	..................
	..................

]], [[

	..................
	..................
	..................
	..................
	..................
	..................
	~~~~$$~~~~~~~$$~~~
	~~~~$$~~~~~~~$$~~~
	~~~~$$~~~~~~~$$~~~
	~~~~$$~~~~~~~$$~~~
	..................
	..................
	..................
	..................
	..................

]], [[	

	###############_##
	______________#_#.
	__________________
	__$________$______
	__________________
	__________________
	__________________
	__________________
	__________________
	__________________
	__________________
	__________________
	__$________$______
	__________________
	__________________

]], [[

	###############_##
	______________#_#_
	__________________
	__$________$______
	__________________
	__________________
	__________________
	__________________
	__________________
	__________________
	__________________
	__________________
	__$________$______
	__________________
	__________________

]], [[

	##################
	______________###_
	__________________
	__$________$______
	__________________
	__________________
	__________________
	__________________
	__________________
	__________________
	__________________
	__________________
	__$________$______
	__________________
	__________________

]], [[

	##################
	__________________
	__%________%______
	_%$%______%$%_____
	__%________%______
	__________________
	__________________
	__________________
	__________________
	__________________
	__________________
	__%________%______
	_%$%______%$%_____
	__%________%______
	__________________

]], [[

	##################
	__________________
	_%%%______%%%_____
	_%$%______%$%_____
	_%%%______%%%_____
	__________________
	__________________
	__________________
	__________________
	__________________
	__________________
	_%%%______%%%_____
	_%$%______%$%_____
	_%%%______%%%_____
	__________________

]], [[

	__________________
	__________________
	__%________%______
	_%%%______%%%_____
	__%________%______
	__________________
	__________________
	__________________
	__________________
	__________________
	__________________
	__%________%______
	_%%%______%%%_____
	__%________%______
	__________________

]] }
Attachments
map_string.love
(720 Bytes) Downloaded 74 times
Last edited by Saegor on Fri Feb 08, 2013 12:50 pm, edited 2 times in total.
Current work : Isömap
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: string map

Post by kikito »

My pleasure, I'm glad you liked it :awesome:

I wish I had more time to write tutorials, but writing code & libraries is more prioritary for me. Good luck with your project!
When I write def I mean function.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: string map

Post by Roland_Yonaba »

Awesome.
I used a similar technique in Jumper (well that's quite the same code), see the parseStringMap function.

Code: Select all

local function parseStringMap(str)
  local map = {}
  local w, h
    for line in str:gmatch('[^\n\r]+') do
      if line then
        w = not w and #line or w
        assert(#line == w, 'Error parsing map, rows must have the same size!')
        h = (h or 0) + 1
        map[h] = {}
        for char in line:gmatch('.') do
          map[h][#map[h]+1] = char
        end
      end
    end
    return map
  end
User avatar
Saegor
Party member
Posts: 119
Joined: Thu Nov 08, 2012 9:26 am
Location: Charleroi

Re: string map

Post by Saegor »

thanks you two for your replies, you are so nice :)

your libraries are very advanced for me but your support is always so handy and friendly !


ps : in the code you pasted here roland, you did "w = not w and #line or w", is there a difference with "w = w or #line" ?
Current work : Isömap
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: string map

Post by Roland_Yonaba »

Saegor wrote: ps : in the code you pasted here roland, you did "w = not w and #line or w", is there a difference with "w = w or #line" ?
Hi saegor,
Actually, there's no difference at all. Yours is even better, because shorter.
Hum, think I have to try another coffee brand...
Post Reply

Who is online

Users browsing this forum: No registered users and 61 guests