Here it is again, this time as a type with functions and methods. I have added a maxAttempts variable to prevent it from hitting an eternal loop in the room creation method and its a bit more flexible allowing for varied width/height of dungeon and rooms. Its in no way complete, but its been fun playing around with anyways.
SuperStrict
Graphics 640,480
SeedRnd(MilliSecs())
Local dunMaze:dungeonMaze = dungeonMaze.createMaze(25,25,0)
For Local i:Int = 0 To 19
dunMaze.createMazeRoom(Rnd(3,6),Rnd(3,6),1,50)
Next
While Not KeyHit(KEY_ESCAPE)
Cls
dunMaze.drawMaze()
Flip
If KeyHit(KEY_SPACE) Then
dunMaze.resetMaze(Rnd(20,40),Rnd(20,40),0)
For Local ii:Int = 0 To Rnd(15,35)
dunMaze.createMazeRoom(Rnd(3,6),Rnd(3,6),1,50)
Next
End If
Wend
End
Type dungeonMaze
Global dungeonMazeList:TList = CreateList()
Field maze:Int[,] ' the maze array that holds the content of the maze
Field mazeLink:TLink ' keep a direct link to the object in the list
Field mazeWidth:Int
Field mazeHeight:Int
Function createMaze:dungeonMaze(width:Int,height:Int,value:Int=0)
Local dm:dungeonMaze = New dungeonMaze
Local tmpMaze:Int[width,height]
dm.maze = tmpMaze
dm.mazeWidth = width
dm.mazeHeight = height
dm.mazeLink = ListAddLast(dungeonMazeList,dm)
' clear maze before returning
For Local x:Int = 0 Until width
For Local y:Int = 0 Until height
dm.maze[x,y] = value
Next
Next
Return dm
End Function
Method resetMaze(width:Int,height:Int,value:Int=0)
Local tmpMaze:Int[width,height]
maze = tmpMaze
mazeWidth = width
mazeHeight = height
For Local x:Int = 0 Until width
For Local y:Int = 0 Until height
maze[x,y] = value
Next
Next
End Method
Method removeMaze()
RemoveLink( mazeLink )
End Method
Method createMazeRoom(roomSizeX:Int,roomSizeY:Int,roomValue:Int,maxAttempts:Int = 10)
Local x:Int,y:Int,rx:Int,ry:Int,spacefound:Int,cnt:Int = 0
Repeat
spacefound = 0
rx = Rnd(mazeWidth-roomSizeX-1) ' create random x start from left side until width minus the roomsize minus the border
ry = Rnd(mazeHeight-roomSizeY-1) ' same thing for the y start and height
For x = 0 Until roomSizeX+2
For y = 0 Until roomSizeY+2
If maze[rx+x,ry+y] = 1 Then spacefound :+ 1 ' check to see if any squares in the maze is taken already by a room
Next
Next
cnt :+ 1
If cnt > maxAttempts Then Return
Until spacefound = 0 ' until no squares taken found
For x = 0 Until roomSizeX
For y = 0 Until roomSizeY
maze[rx+x+1,ry+y+1] = 1 ' fill in the new room
Next
Next
End Method
Method drawMaze()
For Local x:Int = 0 Until mazeWidth
For Local y:Int = 0 Until mazeHeight
If maze[x,y] = 0 Then
SetColor(128,128,128)
DrawRect(x*8,y*8,7,7)
Else
SetColor(192,192,192)
DrawRect(x*8,y*8,7,7)
End If
Next
Next
End Method
End Type