Game Development Community

Hours saved in creating tile layers :)

by Philip Mansfield · in Torque Game Builder · 09/14/2006 (1:05 pm) · 0 replies

In my current project, I have a need to take an imagemap, split it up into 32x8 pixel sections and then make a tilemap out of it. Needless to say, this is rather time consuming and looking at a load of cropped brushes in the tilemap editor makes it a little hit and miss to get the tiles in the right spaces (some of the imagemap is transparent leaving empty brushes).

So I knocked up a little bit of code that will take my imagemap and do all the work for me :) It's pretty straightforward, but you never know, someone else might have a need to do something similar.

First I define the image maps as standard:
new t2dImageMapDatablock(okeanImageMap) {
      imageName = "~/data/images/okean";
      imageMode = "CELL";
      filterMode = "SMOOTH";
      filterPad = "1";
      cellRowOrder = "1";
      cellWidth = "32";
      cellHeight = "8";
      preload = "0";
      allowUnload = "1";
};
Then comes the tile layer bit:
// create tilemap from script
function createTileMap()
{
   // create a new map
   %newMap = new t2dTileMap() { scenegraph = menuScene; };
   // add a tile layer to it of the correct dimensions
   %newLayer = %newMap.createTileLayer ( "14 12 32 8" );

   // initialise the imagemap frame counter
   %frame = 0;

   // loop through the layer a row at a time
   for (%y = 0; %y < getWord(%newLayer.getTileCount(),1); %y++)
   {
      // loop through the layer a column at a time
      for (%x = 0; %x < getWord(%newLayer.getTileCount(),0); %x++)
      {
         // lay down our image in the current tile
	 %newLayer.setStaticTile(%x, %y, okeanImageMap, %frame);
	 // increment the imagemap frame counter
	 %frame++;
      }
   }
   // write out the tile layer to disk
   %newLayer.saveTileLayer ("~/data/tilemaps/okeanLayer.lyr");
}
Then I just need to run the game, pull down the console and run the function :) Once I get the graphics done for the other levels, I'll change the function a bit so I can pass in different tilecount values and the name of the imagemap and the name of the tilelayer.

Collisions I intend to sort out manually in the tile editor as not all tiles will be collision enabled, and some will have non-square polys.

Should save me hours of painstaking work in the tile editor.