Tuesday, June 14, 2011

Tile Map

Every game needs some kind of background or even environment, in the case of 2D games that would make 2D pictures. In the early days it was rather hard to draw and save pictures big enough to fill the entire screen, not to mention that have one for every Level in a game. There were and are games with hundreds and thousand of such screens after all.
So quite early someone had the brilliant idea to not use one big picture for the whole screen, but little ones that would repeat them self in patterns using a 2D map. The Tile Maps where born.

Even if modern PCs and Consoles have enough power to store and handle pictures for the whole screen without much ado, Tile Maps a still quite popular with game designers, as they present a way to quickly, without many resources, create huge and individual Levels.
That is also why I use it.

For a Tile Map you need two things:
First a Content, or how I call them Assets (Content was already taken in the code).
Second you need the Map, a 2D Array of information (later called Grid).


Assets:

The Assets are the little tiles that will be drawn on screen.
Not always are the Assets stored in single pictures also can they have other special behaviour like offsets. Also can it happen that when you use a Editor (like me) that you may have one source picture for one and the same tile, but the Editor and the Game treat them differently.
For that it is quite useful to have a separate entity that holds those informations. Its is also good that this information is separately to the picture as you can safe it separately using less memory, and you can vary easier (without gaining much memory usage).
At the moment this results that I can have one source picture for the whole Level (Asset1) and multiple others that point to this picture as their Source, but showing only one part of it (Asset2 to 5). Then again I can have another Asset (6) that points at 5, but draws it with a offset.
Using this you can have a Stone centred on the grid at one place and then have the same Stone off the grid in a different place.
Creating such variations in a Tile Map is important as you don't necessary want the player to know that you use a Tile Map.

// In Code:
One Dictionary of 2DTextures that holds the actual pictures, and one that holds the AssetInformation. The two are linked together using unique Tags as Keys, like the file name of the picture.


Grid:

This is the same Grid about which I've posted earlier.
It is basically a 2D Array of information. Theoretically can a Gird for a Tile Map hold any kind of information, from Numbers up to Text. All it needs is a clear way to point out what tile it wants to draw where.
I use a 2D Text Array (string) for this, as I plan not only to store graphic information in it, but also scripting. Like the Dictionaries of the Assets, I use here the Tag of the Asset to point out what to draw.

//In Code:
There are two ways to implement a 2D array in a modern programming language: First a multidimensional Array (string[x,y]) and second a jagged Array (string[x][y], or a Array of Arrays).
Because it is easier to handle in some code (safeing, copying, changing dimension, etc) I decided to go with the jagged array approach.


So this is how a Tile Map is build up, in the next post I will write how to draw them, as this can be its own little problem.

No comments:

Post a Comment