Clan-McKenna.Net

Creating a 2-D Tile-Based Map

The first step in creating the game world is to create data structures that will represent the world. In this case I used four primary arrays:
WorldMap, to hold the map data of all areas (10 total)
Map, to hold the map data of the area the player is currently in (10 x 10)
MapTiles, a hash mapping map data characters to image tiles
Tiles, to represent the tile images as objects

The arrays separate the representation of the world as a spatial dimension with content versus the representation of the content itself (image tiles).

The tiles represent different types of traversable terrain (floors, ground, grass, etc) and non-traversable terrains (walls, doors, etc).

The world map array holds the map data of all areas the player can travel to (village, forest, dungeon or dungeon levels, etc). In this case, we are creating a world map with ten possible areas:

var WorldMap = new Array(10); WorldMap[0] = "0000100000" +"0001100000" +"0001011110" +"0101011010" +"0101001010" +"0101111010" +"0100000010" +"0100111110" +"0111100000" +"0000000000";

In this instance we have created one area map and filled it with 1's and 0's to represent traversable and non-traversable terrain. Later we will map these to actual image tiles that will be the visual representation of the terrain.

The area that the player is currently in is represented by a single 2-D array that will always be refilled whenever the player changes location. The new map data will be used to fill the screen with the new image tiles.

The function MakeMap() will be called once at initialization to create the 2-D array.

var Map = new Array(10); function MakeMap() { var x = 0; for(x = 0; x < 10; x++) { Map[x] = new Array(10); } }

The function LoadMap() will be called whenever a player changes location. The function is called as LoadMap(WorldMap[x]) where x represents the player's new location. The function definition is below:

function LoadMap(MapData) { var x = 0; var y = 0; var z = 0; for(x = 0; x < 10; x++) { for(y = 0; y < 10; y++) { Map[x][y] = MapData[z]; z++; } } }

* The code so far is the end-result from when I began designing this game engine. It is purposefully more abstract to hold multiple maps and make the process of changing locations more concise.

Once we have the maps filled with ascii characters we need to map these characters to specific image files (in this case we are using the simplest scenario of two types of terrain: traversable (1) and non-traversable (0)).

To do this we use a hash to map the character to a particluar (64px x 64px) image file with the appropriate texture for the terrain.

var MapTiles = new Array(); MapTiles['0'] = "tiles/wall-blackstone.gif"; MapTiles['1'] = "tiles/floor-marble-white.gif";

MapTiles['0'] =

MapTiles['1'] =

The tiles that will be drawn to the browser screen will be represented as Javascript objects with the Tile array and executed with the DrawMap() function. This function is only called during initialization since tiles can now be swapped out when the player changes areas. This is accomplished with the FillMap() function (next).

var Tile = new Array(100); function DrawMap() { var x = 0; var y = 0; var z = 0; for(x = 0; x < 15; x++) { for(y = 0; y < 15; y++) { Tile[z] = document.createElement('IMG'); Tile[z].id = z; document.getElementById('pov').appendChild(Tile[z]); z++; } document.write("<br>"); } }

Generating the final map is accomplished using the FillMap() function. During initialization we call:
MakeMap();
LoadMap(WorldMap[Player.location]);
DrawMap();
FillMap();
but when the player changes locations we only need call:
LoadMap(WorldMap[Player.location]);
FillMap();

function FillMap() { var z = 0; for(var x = 0; x < 15; x++) { for(var y = 0; y < 15; y++) { Tile[z].src = MapTiles[Map[x][y]]; z++; } } }

Overview
World Map
Current Map
Loading the Map
Tile Images for Textures
Tile Map Representation
Generating the Map
View the Result
~Return~