It's hard to know where the difficulty lies. And in absence of a map structure, it's hard to give hints on how to turn it into actual code.
The basic idea is to read the file line by line, and then each of the lines character by character, and fill in the map according to the character found. But that's straightforward.
Note that I wouldn't recommend having the player and the boxes as part of the map, but better as separate entities. That would allow more flexibility like animating the character as it changes squares, rather than making it disappear from one square and appear on the next, for example.
Here's an example of how to read it into three structures: map, boxes, and player. The boxes table is assumed to have pairs {x=x, y=y} indicating where the boxes are. The player is just x and y.
Code: Select all
boxes = {}
local y = 1
for line in love.filesystem.lines(levelfilename) do
for x = 1, #line do
c = line:sub(x, 1)
if c == "#" then
-- wall - store in map
map[y][x] = wall_sprite
elseif c == "." then
-- target - store in map
map[y][x] = target_sprite
elseif c == "*" then
-- box on target - store target in map, box in boxes
map[y][x] = target_sprite
boxes[#boxes+1] = {x=x, y=y}
elseif c == "@" then
map[y][x] = empty_sprite
player = {x=x, y=y}
-- and so on
end
end
y = y + 1
end
The complete level format is at
http://sokobano.de/wiki/index.php?title=Level_format
I've omitted sanity checks, like having only one player, checking that the number of targets equals the number of boxes, and more sophisticated ones such as ensuring that the room is closed or that all boxes not already on a target are reachable. Checks for level separators are also omitted.