Rougelike is a interesting game genre, the most important thing in Roguelike game maybe is the procedural content generation(PCG), which provide player tons of game content. From triple A game Diablo series to the game seldom known to mass Dwarf Fortress, one similar point is they all use PCG.
My target is make a Roguelike game on Android with libgdx. This game use tile map to present game content. Luckily, the powerful libgdx support tile map. I only have to do two things on dungeon generation, generate dungeon map then use tile map present it.
1.Generate dungeon map data.
There’re a lot of resource about dungeon generation, I recommend below ones.
Random Dungeon Generator It explain the algorithm and provide simple source code(Perl) and online demo.
Unangband Dungeon Generation It has 9 parts and source code can get from Unangband.
2.Tile map
The current libgdx 0.92 only support read map data from TMX file, new version of TiledLoader can read map data from String. Because we generate the map data and don’t have TMX file, so we should build one.
TiledMap map;
public void writeTMX(String pFileName){
StringWriter writer = new StringWriter();
XmlWriter xml = new XmlWriter(writer);
//in libgdx 0.92, TileLoader only can read tmx file from FileHander, future version will support tmx string
//this implement is NOT use on Android version, just for test
FileHandle tmxFile = new FileHandle(pFileName);
try {
//map's start
xml.element("map")
.attribute("orientation", "orthogonal")
.attribute("version", "1.0")
.attribute("width", tileIds[0][0].length)
.attribute("height", tileIds[0].length)
.attribute("tilewidth", tilewide)
.attribute("tileheight", tilehigh);
//tileset
xml.element("tileset")
.attribute("name", tilesetName)
.attribute("firstgid", tilesetFirstId)
.attribute("tilewidth", tilesetTilewide)
.attribute("tileheight", tilesetTilehigh)
.element("image")
.attribute("source", tilesetSource)
.pop()
.pop();
//layer
for(int layer = 0;layer < tileIds.length;layer++){
String csv = "";
for(int row = 0;row < tileIds[layer].length;row++){
csv += toCSV(tileIds[layer][row]) + "\n\t\t\t";
}
xml.element("layer")
.attribute("name", "layer"+(layer+1))
.attribute("width", tileIds[0][0].length)
.attribute("height", tileIds[0].length)
.element("data")
.attribute("encoding", "csv")
.text(csv)
.pop()
.pop();
}
//map's end
xml.pop();
//write to a tmp file
Writer fileWriter = tmxFile.writer(false);
fileWriter.write(writer.toString());
fileWriter.close();
} catch(IOException e){
throw new RuntimeException(e);
}
map = TiledLoader.createMap(tmxFile);
}
Then use TileMapRenderer draw the tile map.
The full code of this sample you can find on Github. As I said it’s just a sample, to be a real tile dungeon generator, it still have lots of work to do like different room type, monsters and other game objects.

