Map-related embarrassment time...

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
AWizardDidIt
Prole
Posts: 14
Joined: Wed Sep 16, 2009 9:55 am

Map-related embarrassment time...

Post by AWizardDidIt »

Heya! It's been awhile, but at one point I asked about a tile-map editor and thought I'd try out Tiled. Anyway, having said that, I'm curious: Is there a tutorial, somewhere, which covers making a tilemap without an editor? I feel like a complete idiot, to be honest, but I'm seriously in need. If anyone can direct me, I'd be seriously in their debt! :oops:
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Map-related embarrassment time...

Post by kikito »

The general consensus is that the simplest way to create maps without using an editor is directly writing them in Lua. (Some weirdos have expressed their preference for a xml-based solution with luaxml)

There are some questions in the forum regarding map coding, although not something as specific as a tutorial. Let me try to synthesise my views on that.

The first thing you need to decide is: how do you want to model your levels? You must try to think about the kind of questions you will want to ask your map. Will there be solid tiles? Lava tiles? Walls? How will the initial creatures be placed?

With that in mind, you must decide what sort of structure you need. Most probably you will decide that you will need a table, possibly composed of other tables.

This very simple table could be used to model a small level with some walls (1), some ground (0) and a central lake (2):

Code: Select all

return {
  { 1, 1, 1, 1, 1, 1, 1, 1 },
  { 1, 0, 0, 0, 0, 0, 0, 1 },
  { 1, 0, 0, 2, 2, 0, 0, 1 },
  { 1, 0, 2, 2, 2, 2, 0, 1 },
  { 1, 0, 2, 2, 2, 2, 0, 1 },
  { 1, 0, 0, 2, 2, 0, 0, 1 },
  { 1, 0, 0, 0, 0, 0, 0, 1 },
  { 1, 1, 1, 1, 1, 1, 1, 1 }
}
The numbers where chosen at random. You could use letters if you wanted to, but then you would have to use a lot of quotes. I don't recommend using strings for the rows since lua's string manipulation functions are not the best ones.

In order to use this map, you just have to save it to map1.lua and use this code to load it:

Code: Select all

local map = require('map1.lua')
And iterate over the map variable in order to get the tiles.

Say that you have managed to read and draw the map, but you need to add some objects or enemies on top of the tiles. There are several ways you can do this, but the most straightforward is to split the map into two different tables: one for the tiles themselves and another one for the entities. Like so:

Code: Select all

return {
  tiles = {
    { 1, 1, 1, 1, 1, 1, 1, 1 },
    { 1, 0, 0, 0, 0, 0, 0, 1 },
    { 1, 0, 0, 2, 2, 0, 0, 1 },
    { 1, 0, 2, 2, 2, 2, 0, 1 },
    { 1, 0, 2, 2, 2, 2, 0, 1 },
    { 1, 0, 0, 2, 2, 0, 0, 1 },
    { 1, 0, 0, 0, 0, 0, 0, 1 },
    { 1, 1, 1, 1, 1, 1, 1, 1 }
  },
  entities = {
    { 'zombie',1,1 },
    { 'zombie',1,3 },
    { 'duck',4,4 }
  }
}
This means "place two zombies in positions (1,1) and (1,3) and one duck in (4,4)".

This illustrates well how you can differently encode sparse tables (such as the entities subtable) vs dense tables (the tiles one). Sparse tables with only a handful of objects are better encoded sequentially, with coordinates, while dense tables are better just 'drawn' on with one number per tile.

If you do this now:

Code: Select all

local map = require('map1.lua')
You will have the tiles in map.tiles and the entities in map.entities, which is very convenient for parsing.

Notes:
  • Keep in mind that this is just an example. You will probably need a different structure for your particular game. But the process should be similar to this one.
  • Due to the particular idiosyncrasies of tables in lua, the map.tiles table is indexed with the Y component before the X; map.tile[3][4] will return the tile that is on the third row, fourth column (y=3, x=4)
When I write def I mean function.
AWizardDidIt
Prole
Posts: 14
Joined: Wed Sep 16, 2009 9:55 am

Re: Map-related embarrassment time...

Post by AWizardDidIt »

Wow... That was incredibly quick... Thanks, kikito! If this is as easy as it looks, you just saved me a ton of headaches with Linux/WINE-friendly map editors! :awesome:

To think... all I even expected was a link to a tutorial page... :o
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 65 guests