RPG Mapping System

Dhashar RPG Map
  • Nov 12, 2009 | phpRogue | Development, Games

For my Legends of Dhashar PBBG, I decided to go with a hex-based map for maximum flexibility of character movement. These types of maps were the foundation of wargames that I played many years ago and they still appeal to me.

The map is divided into regions, each region being the size of one screen. When the character moves off the edge of the screen, a new region loads and the movement continues. The map starts with a simple map text file. There is a separate text file for each region which makes it a breeze to add new regions. Each file holds all of the data for a particular region. The first line lists the region ID numbers for all adjacent regions. The next eight lines are a grid of numbers representing the terrain for each hexagon in the region. The last line describes the sole town in that region. The current game only supports one town per region, but this can easily be changed to support any number.

Sample map text file:

Elandria
000,003,002,000
0,2,2,2,2,2,1,1,1,1
0,2,2,2,2,1,1,1,1,1
0,2,2,2,1,1,1,1,1,1
0,1,1,1,1,1,1,1,1,1
0,1,1,1,3,3,1,1,1,1
0,3,3,3,3,3,1,1,1,1
0,3,3,3,3,0,0,1,1,1
0,0,0,0,0,0,0,0,0,0
Bartertown,town,6,2,1.1,.8,weapons,armor,potions

The first step is to load a region. This is done with a Region class that I have already written. It stores the entire region map into an array which is a simple thing since the map is only 10 hexes wide by 8 hexes high. Once loaded, I can now draw the region. To do this, I run a nested loop to cover each row and column of the map. At each iteration of the loop, we "echo" [PHP] an <img> tag where we display the proper map tile based on the terrain number saved in the map array.

Remember, hexagons are a little more difficult than squares - each column must be offset from the one before, so they take on a staggered appearence.

After the map is drawn it is a simple thing to add characters and monsters and anything else you want, which we will cover later.

Comments