This is the level generator I created for my DarkROADS project:
http://forum.thegamecreators.com/?m=forum_view&t=124851&b=8
I know it's a bit messy, but I'll explain to you how it works:
it comes with 3 .dba files and a simple texture, which are zipped and attached!
Step 1
it initializes these global variables:
global spareObjects = 5
global gridWidth = 7
global blockWidth = 40
global blockHeight = 6
global blockDepth = 40
My function starts creating blocks with objectnumbers higher than spareObjects. So you can define how many spare objects you will need, lets say, for a player model, or items.
The gridWidth is the number of boxes you wish your grid to be wide.
blockWidth, Height and Depth represent the dimensions of you platform blocks.
Step 2
BuildGrid() takes you inside the Grid v1.dba document.
It starts of initializing some variables for positioning your 'Grid'.
Next it will loop through 3 two dimensional grids, located in Level1.txt
(note the 99 at the end of every grid. This acts as an EndOfFile marker)
The first is for the 'type' of your blocks.
0 = no block at all
1 = a metal block
You can add more stuff if you like, read the comments in de source!
next it will loop through the second grid which will define the y position of each block.
0 = do nothing
1 = place the block 1 position higher
2 = place the block 2 positions higher
etc.
last it will loop through a grid which defines the height of each block.
0 = do nothing
1 = default height
2 = twice the default height.
etc.
You can move the camera by holding down the right mouse button to steer the camera, and pressing the arrow keys to move around.
It's not the cleanest code. But heck, i own this program since 1 month now
and it works!
Here are the snippets, without texture though..
You'll have to provide those yourself
(only 2 needed)
V0.dba:
REM ****************************
REM vvvvvv INITIALIZATION vvvvvv
load dll "user32.dll",1
Sw=call dll(1,"GetSystemMetrics",0)
Sh=call dll(1,"GetSystemMetrics",1)
delete dll 1
set display mode Sw, Sh, 16
hide mouse
sync on
sync rate 60
color backdrop rgb(0,0,0)
ink rgb(255,255,255),0
REM ^^^^^^ INITIALIZATION ^^^^^^
REM ****************************
`load some textures
load image "grey metal.bmp", 1
load image "red metal.bmp", 2
`Define global variables
global spareObjects = 5 `numbers after this number will be taken by the objects made by BuildGrid()
global gridWidth = 7 `This is the number of boxes your grid will be wide
global blockWidth = 40 `This is the Width of the boxes in your grid
global blockHeight = 6 `This is the Height of the boxes in your grid
global blockDepth = 40 `This is the Depth of the boxes in your grid
`Build a level
buildgrid()
`Position Camera
position camera 0, 140, 50, -20
point camera 0, 0, 27, -20
`pre-define camera rotation variables
rx# = camera angle x(0)
ry# = camera angle y(0)
`main loop
repeat
`Display tips:
text 10,10,"Use the mouse to look around."
text 10,25,"Use the arrow keys to move around."
text 10,40,"Press [esc] to exit."
`never ever use this function in your game:
control camera using arrowkeys 0,1,1
`Rotate the camera with the mouse
rx# = wrapvalue(rx#+(mousemovey()*0.1))
ry# = wrapvalue(ry#+(mousemovex()*0.1))
rotate camera 0,rx#,ry#,0
sync
until keystate(1)
end
Grid v1.dba
function buildGrid()
restore level1type
counter = 1
finalCounter = 0
x = 0
z = (((BlockDepth*gridWidth)/2)*-1)
REMSTART ****************************
This loop will check the first grid
and build a '2 dimensional grid' out
of 3 dimensional objects.
0 = empty space
1 = grey metal box
2 = red metal box
****************************** REMEND
do
read plains
`99 is the last number at the grid. When this is read it will
`break out of this loop and head to the next one!
if plains = 99 then finalCounter = counter : exit
`If the loop reads a 1, it will create a box at that position
`with a grey metal texture.
if plains = 1
make object box (counter + spareObjects),blockWidth,blockHeight,blockDepth
position object (counter + spareObjects),x,blockHeight/2,z
texture object (counter + spareObjects), 1
endif
`If the loop reads a 2, it will create a box at that position
`with a red metal texture.
if plains = 2
make object box (counter + spareObjects),blockWidth,blockHeight,blockDepth
position object (counter + spareObjects),x,blockHeight/2,z
texture object (counter + spareObjects), 2
endif
z = z + blockDepth
`Next line of boxes
if (counter mod gridWidth) = 0
x = x - BlockWidth
z = (((BlockDepth*gridWidth)/2)*-1)
endif
`Count at which position the gridBuilder is
counter = counter + 1
loop
REMSTART ****************************
This loop will check the second grid
and will update the y-positions of
the previously made metal boxes.
0 = do nothing (default)
1 = place object at height 1
2 = place object at height 2
etc.
****************************** REMEND
restore level1y
counter = 1
y=0
do
read plains
`99 is the last number at the grid. When this is read it will
`break out of this loop and head to the next one!
if plains = 99 then exit
`If the object excists this will place the object higher or lower,
`according to a positive or negative number in the second grid.
if object exist(counter + spareObjects)
position object (counter + spareObjects),object position x(counter + spareObjects),(plains*blockHeight)+(blockHeight/2),object position z(counter + spareObjects)
endif
`Count at which position the gridBuilder is
counter = counter + 1
loop
REMSTART ****************************
This loop will check the third grid
and will update height of the
previously made metal boxes.
0 = do nothing (object remains 1 box high)
1 = do nothing (object remains 1 box high)
2 = create a 2 boxes high box
etc.
****************************** REMEND
restore level1height
counter = 1
do
read plains
`99 is the last number at the grid. When this is read it will
`break out of this loop and head to the next one!
if plains = 99 then exit
`This one is actually quite complicated.
`Because i'm unable to re-init a box's boundries
`i have to read it's old position, create a second
`box with the wanted height, place it at the old
`box's position, retexture it and delete the old box.
if plains <> 0 and plains <> 1
make object box (finalCounter + spareObjects),blockWidth,blockHeight*plains,blockDepth
position object (finalCounter + spareObjects),object position x(counter + spareObjects),((object position y(counter + spareObjects)-(blockHeight/2))+((blockHeight*plains)/2))+(blockHeight/8),object position z(counter + spareObjects)
texture object (finalCounter + spareObjects),(limb texture (counter + spareObjects),0)
delete object (counter + spareObjects)
finalCounter = finalCounter+1
endif
`Count at which position the gridBuilder is
counter = counter + 1
loop
endfunction
level1.txt
level1type:
data 0,0,1,2,1,0,0
data 0,0,0,2,0,0,0
data 0,1,1,2,1,1,0
data 0,1,1,0,1,1,0
data 0,2,1,2,1,2,0
data 0,0,0,2,0,0,0
data 0,0,0,2,0,0,0,99
level1y:
data 0,0,-3,1,-3,0,0
data 0,0,0,1,0,0,0
data 0,1,1,1,1,1,0
data 0,2,1,0,1,2,0
data 0,1,1,1,1,2,0
data 0,0,0,1,0,0,0
data 0,0,0,3,0,0,0,99
level1height:
data 0,0,3,1,3,0,0
data 0,0,0,1,0,0,0
data 0,4,1,1,1,4,0
data 0,1,1,0,1,1,0
data 0,2,1,1,1,3,0
data 0,0,0,1,0,0,0
data 0,0,0,1,0,0,0,99
Feedback is most welcome!