Creating grid based worlds
Grids (or nodes) are an easy way to create the base for your game.
Why? Simple! When you move around the world and you project your player position in comparison to an underlying grid you can easily implement collision, pathfinding, field of view, etc.
Creating our Grid
Now how do we create our grid?
A 2d grid and a 3d grid shown above |
An easy way to create a grid with multiple properties is a Structure, which will hold our tile properties (Position X,Y Tile ID for picking the correct texture and a boolean that renders it walkable or not)
Public Structure GridStruct
Public X As Integer
Public Y As Integer
Public Z As Integer
Public ID As Integer
Public Blocked As Boolean
End Structure
Public MaxGridX As Integer = 47 'Maximum tiles on the X axis
Public MaxGridY As Integer = 24 'Maximum tiles on the Y axis
Public GridWidth As Integer = 24 'Each tile will be 24x24 pixels in size
Public GridWorld(MaxGridX, MaxGridY) As GridStruct '2D World
Public Sub GridCalls(ByVal Func As String)
Dim X, Y, Z As Integer
If Func = "CREATE" Then
For X = 0 To MaxGridX
For Y = 0 To MaxGridY
GridWorld(X, Y).X = X * GridWidth
GridWorld(X, Y).Y = Y * GridWidth
GridWorld(X, Y).Z = Z * GridWidth
'So example of a tile position would be
'(2,3,1).x = 2 * 24
'(2,3,1).y = 3 * 24
'(2,3,1).z = 1 * 24
GridWorld(X, Y).ID = Int(Rnd() * 4) 'Assigning random values for tile ID
Next
Next
'Now we'll create a worm like algorithm to create random connected dirt through our grid world
Dim StartX As Integer = 20
Dim StartY As Integer = 12
Dim WormSteps As Integer = 360
Dim Direction As Integer = 0
Randomize()
GridWorld(StartX, StartY).ID = 5
For Z = 0 To WormSteps
Direction = Int(Rnd() * 4)
If Direction = 0 Then
StartY -= 1
ElseIf direction = 1 Then
StartX += 1
ElseIf direction = 2 Then
StartY += 1
ElseIf direction = 3 Then
StartX -= 1
End If
If StartX < 0 Then StartX = 0 : If StartX > MaxGridX Then StartX = MaxGridX
If StartY < 0 Then StartY = 0 : If StartY > MaxGridY Then StartY = MaxGridY
GridWorld(StartX, StartY).ID = 5
Next
End If
End Sub
We make a call to the above on our Form_Load event (GridCalls("CREATE")) for the grid to be created.
And now for drawing our grid we'll create a Tile map image as shown below.
Tilemap creation |
TestDrawingS = New Bitmap(My.Resources.TileMap) 'Resource
We change our resource file to the TileMap.PNG file (We had this code in SoftRender("LOAD"))
And for our drawing we'll use this code on our SoftRender("DRAW")
For X = 0 To MaxGridX
For Y = 0 To MaxGridY
QRect = New Rectangle(GridWorld(X, Y).ID * 24, 0, 24, 72) 'We select the tile (From Tile ID - X position and Width and Height)
TestDrawingB = New TextureBrush(TestDrawingS, QRect)
TestDrawingB.TranslateTransform(GridWorld(X, Y).X, GridWorld(X, Y).Y - 48) 'Subtracting our tilemap offset
TRect = New Rectangle(GridWorld(X, Y).X, GridWorld(X, Y).Y - 48, 24, 72)
TestDrawingB.ScaleTransform(1, 1)
Gs.FillRectangle(TestDrawingB, TRect)
TestDrawingB.Dispose()
Next
Next
And we should have a result like shown in the image below
The forest |
Now we see the random map. We have lots of trees and rocks, we could change our tilemap image to have more ground tiles or our tile picking random tile ID picking to give less weight to tree and rock tiles.
Comments
Post a Comment