Wednesday, March 10, 2010

The "ArrayMap" technique.

The Basics
This technique is another way of representing a map created from a set of tiles. With the GridMatrix technique, I created a matrix with the exact size of my window and applied and scrolled through the matrix generation pictureboxes and applying them a specific tile ( a 32x32 picture ). 


There are a few weaknesses with this techique. First if you look my code closely I AM NOT EVEN inserting values inside the matrix, I am merely strolling through its dimensions and using this to represent rows and columns. This strikes me as the first big weakness. The second one is that: what if I need to access the tiles afterward ? Lets say my caracter is at the right edge of the screen and goes right, supposedly to make the map scroll to the right. Well because I did not store the location of the tiles and images somewhere, I have to re-scan the whole window and somehow code a way to remove the leftmost column and add one at the right. This is a would-be pain in the ass.


And here comes the ArrayMap technique:



This is an array. It signifies that the letter A represents all those numbers at once. And that we can get their values easily. 

An array is declared just like any other value:

Dim tileset as array

And to populate this array:

tileset = { 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3 }

Tileset will now have those values stores inside him. To retrieve them we can make a For Each loop and write each in a console

For Each x as Integer in tileset
        writeline(x)
Next


The Method I use

So I made a couple of arrays and planned to make picturebox images from the value contained in the array. Say the first value in the array would be 0 so the tile at (0, 0) is going to be say, grass. The 14's value would be the code for (14, 0) and the 16'th would be (0, 1)  (because we are scrolling left to right and then go to next line after the 15'th element ). 

Those are my arrays
The code I used to turn values into tiles:


And the result of using the BasicRoad tileset is: 



This is pretty much like the GridMatrix technique but instead of looping through a matrix we are using a For Each to cycle through the values of the array and using a Select Case to decide which tile to use as the image. 

No comments:

Post a Comment