Can Stencyl do random/procedural level generation?


  • Posts: 18
I know this is an old topic, but I've been working on a project that uses procedural generation to create levels and I think it'd help to note some of the things I've encountered.
check out WIP so far:

I was able to get this far using code mode and creating a scene behavior. 

Using multi-dimensional arrays in AS3 is not as hard as it sounds  - basically I'm creating an array thats as long as the number of grid rows in the scene. Then you store an array in each of the indexes of that rows array that is as long as the number of columns in the scene.  This meant that by saying Array[0] i was referencing the top row of the scene.  By typing Array[0][1] I'm referencing the grid square that is in the top row and second column from the left. 

Now here's where it gets really weird:

In each of those Array[row][col] spaces I stored ANOTHER array that was only two in length - one index for TilesetID and one index for TileID.

Code: [Select]
var TSID:int = 0;//helper variables to reference tilesetID and TileID
var TID:int = 1;
var tilesArray:Array = new Array(30); //number of rows in my scene

for(var r:int = 0; r < rows; r++)//every row
tilesArray[r] = new Array(cols);
for(var c:int = 0; c < cols; c++)//every column
tilesArray[r][c] = new Array(2);//Array[0] is TilesetID and Array[1] is TileID
tilesArray[r][c][TSID] = 1;//
tilesArray[r][c][TID] = 0; //fill the array with WALL tiles



This created this 3-d array and filled it with TilesetID and TileID numbers.

Next went through the nasty process of randomly changing tiles from walls to floors (hint use one tileset for walls and one tileset for floors.  Then set all of the squares in your floor tileset to have no collisions while all the squares in the walls tileset have full square collisions. 

Last I went through the whole array and used the setTileAt command to generate the appropriate tile at the each row and column coordinate.

I'll try to post more details later, but for now here's the gist of it:

Code: [Select]
for(var r:int = 0; r < rows; r++)
for(var c:int = 0; c < cols; c++)
setTileAt(r, c, 0, tilesArray[r][c][TSID], tilesArray[r][c][TID]);
                //it works like this setTileAt(row, column, layer, tilesetID, TileID)

after it writes all the tiles I have it make a player and a stair case. 

Thats the basic Idea I used - any feedback or further ideas would be awesome!