Dungeon creator

Top  Previous  Next

This code snippet allows you to create levels out of prefabs. Yeah, so what's so interesting about this? Well, let me show you how the level file looks like; it's all inside the level01.dat file, which, despite its name, is a regular text file and can be opened in a simple text editor like Notepad or Wordpad:

 

aum54_dungeon1

 

The first 2 numbers (15 and 13) set the size of the level on the x and y axis; the following lines describe the level which (believe it or not) will be assembled at runtime using a few prefabs and mdl entities. In my demo, "p" is the player, # is a wall, " " (an empty space) is the floor, "s" is a spider and "f" is the finish (the end of the level). Now take a look at the "real" level:

 

aum54_dungeon2

 

Do you see the resemblance? You should, because the "real" level was created at runtime using the information from the level01.dat file. Now why don't you edit the level data file and see how the changes become effective in the game as soon as you run the level again?

 

I chose to create a dungeon-type game prototype, but you can use much more complex prefabs, creating levels for shooters, adventure games, and so on. The advantages are obvious: you don't need to spend too much time editing the levels or building them and the resulting game will be much smaller. Just think about the size of a game with 30 levels, each one of them having (let's say) 10MB and compare that value with the size of a few prefabs + 30 levels of 200 - 300 bytes each... Now that's progress!

 

The spiders stand still until one of them spots the player and alarms all the others. I won't discuss how they (or the player) work because you've seen similar stuff before and I have added lots of comments to the code; I'd rather spend some time explaining the inner workings of the dungeon creator system.

 

I have created two prefabs for this project: a wall block and a floor block.

 

aum54_dungeon3

 

These prefabs are nothing more than 64 x 64 x 64 quants cubes, which were textured using simple, tileable textures. The engine reads the data from the level file and ent_creates the blocks and the entities according to the information that was found in the level01.dat file.

 

aum54_dungeon1

 

We need to plan the size of our level because we have to tell the engine the number of columns (15 in my example) and the number of rows (13 in my example). Then we can start laying out the level inside the text file; you can see that the first row consists only of walls; the following rows contain empty spaces (floors), the player (the engine will create a floor and the player model on top of it), and so on. Take a look at the "p" (player's) letter code to see how it works:

 

if (str_cmp(current_character, "p") == 1) // player? (we create the player and the floor below it)

{

       starting_position.z = 0;

       ent_create(floor_wmb, starting_position, make_static);

       starting_position.z += 64;

       player = ent_create(player_mdl, starting_position, players_action);

}

 

If the engine encounters the "p" letter in the level01.dat file, it creates a floor piece at z = 0 and assigns it the make_static function, and then it creates the player 64 quants above the floor, assigning it the "players_action" function.

 

You can add many more prefab types; they don't need to be simple cubes like in my example, but can be small, medium or even big level parts. Examine the heavily commented read_level_data() function for more information. Oh, and make sure to set proper numerical values inside this function if you are going to change the size of the prefabs for your game.

 

One more thing: Acknex doesn't like to work with an enormous number of entities in a single level (no engine likes that). I have limited the size of the levels to 30 x 30 tiles = 900 entities, but you can increase these values even more. I am using a simple function for all the level elements that aren't dynamic (don't move, don't trigger events, and so on):

 

function make_static()

{

       my.dynamic = off;

}

 

This function increases the frame rate, especially if we are using a larger level, with a big number of tiles (entities).