Game Development Community

TGB Pro Random Tile Placement

by Eric Armstrong · 09/05/2006 (11:22 pm) · 0 comments

There are a number of code and script changes to make this all work. We will start on the code side. All changes are made in the levelBuilderTileMapEditTool.h and levelBuilderTileMapEditTool.cc. We will start with the header file:

Around line 80, after the definition of mCollision you want to add the following:
bool mRandomTiles;
U32 mStartFrame;
U32 mEndFrame;

Now, jump down to around line 160, after the declaration of setCollision and add these lines:
void setRandomTiles(bool enabled);
void setStartFrame(U32 frame);
void setEndFrame(U32 frame);

That's it for the header. Now onto the .cc file.

Around line 44, after the mUpdateCollisionPoly = false; line, add the following:
mRandomTiles = false;
mStartFrame = -1;
mEndFrame = -1;

Now you want to jump down around line 539, in the drawTile method. You want to update the code inside the if(imageMap) to look like the following:
if(mRandomTiles)
{
   if(mStartFrame < 0 || mEndFrame < 0)
   {
      mFrame = 0;
   }
   else
   {
      mFrame = gRandGen.randI(mStartFrame, mEndFrame);
   }
}
else
{
   if ((mFrame < 0) || (mFrame >= imageMap->getImageMapFrameCount()))
      mFrame = 0;
}

mTileLayer->setStaticTile(tile.x, tile.y, mImageMap, mFrame);

Finally, somewhere towards the bottom of the file, you want to add the following code:
ConsoleMethod(LevelBuilderTileMapEditTool, setRandomTiles, void, 3, 3, "")
{
   object->setRandomTiles(dAtob(argv[2]));
}

void LevelBuilderTileMapEditTool::setRandomTiles(bool enabled)
{
   mRandomTiles = enabled;
}

ConsoleMethod(LevelBuilderTileMapEditTool, setStartFrame, void, 3, 3, "")
{
   object->setStartFrame(dAtoi(argv[2]));
}

void LevelBuilderTileMapEditTool::setStartFrame(U32 frame)
{
   mStartFrame = frame;
}

ConsoleMethod(LevelBuilderTileMapEditTool, setEndFrame, void, 3, 3, "")
{
   object->setEndFrame(dAtoi(argv[2]));
}

void LevelBuilderTileMapEditTool::setEndFrame(U32 frame)
{
   mEndFrame = frame;
}

That's it for the code changes. Make sure the above compiles fine, and you are now ready for the script changes.

First thing we need to do, is to add some GUI elements to allow us to set the frame range and mark that we want to place tiles randomly. Open up t2dTileLayer.ed.cs

Around line 62 or so, just after the check boxes for the flipX, flipY, and collision add the following:
%brushRollout.createCheckBox("randomTiles", "Random Tiles", "", "", false, false);
%brushRollout.createTextEdit2("startFrame", "endFrame", 0, "Random Range", "Start", "End", "Random Range");

OK, now we need to add some accessor methods to allow the GUI to update the screen with the values we enter. Open up main.cs inside of tools/tileLayerEditor.

Someplace within that file, add the following code:
function ActiveBrush::setRandomTiles(%this, %random)
{
   Parent::setRandomTiles(%this, %random);
   LevelEditorTileMapEditTool.setRandomTiles(%random);
   
   if( isObject( $TileEditor::QuickEditPane ) )
      $TileEditor::QuickEditPane.refresh();
}

function ActiveBrush::setStartFrame(%this, %frame)
{
   Parent::setStartFrame(%this, %frame);
   LevelEditorTileMapEditTool.setStartFrame(%frame);
   
   if( isObject( $TileEditor::QuickEditPane ) )
      $TileEditor::QuickEditPane.refresh();
}

function ActiveBrush::setEndFrame(%this, %frame)
{
   Parent::setEndFrame(%this, %frame);
   LevelEditorTileMapEditTool.setEndFrame(%frame);
   
   if( isObject( $TileEditor::QuickEditPane ) )
      $TileEditor::QuickEditPane.refresh();
}

function TileBrush::setRandomTiles(%this, %random)
{
   %this.randomTiles = %random;
}

function TileBrush::setStartFrame(%this, %frame)
{
   %this.startFrame = %frame;
}

function TileBrush::setEndFrame(%this, %frame)
{
   %this.endFrame = %frame;
}

function TileBrush::getRandomTiles(%this)
{
   return %this.randomTiles;
}

function TileBrush::getStartFrame(%this)
{
   return %this.startFrame;
}

function TileBrush::getEndFrame(%this)
{
   return %this.endFrame;
}

That's it for the changes. At this point you should now be able to add a tile layer to your level, and select to use random tiles. You will want to make sure that your image files have the tiles of similar type positioned in order, so that you can specify a range of all the same. A quick and easy test is to supply a range that covers different ground types and flood fill the layer; you should then see the random selection of tiles. This will also work with placing individual tiles though, you will get a random tile form the range each time you click to place one.

I hope some of you find this useful.