Gamedevlog: Tilemap Part 3d
Published: 2020-11-01 17:04:26

# Generating a test map
The first thing I did for this development was to generate a test map, I created 4 new simple tiles in blender:
1. One with a straight path running through it
2. One with a T Junction path
3. One which had a crossroads
4. One which had a 90° Corner path

I then mocked up a simple map in Paint, see below:

![Test map](https://dev-to-uploads.s3.amazonaws.com/i/qon0c2zqz30agx4wob3p.png)

And then updated the existing .VTF files and created the necessary new ones to bring this level to life in my game.

It immediately became apparent that to do this I would need to be able to rotate the base tile model, so I added a new line to the .vtf files, the new line used <code>BR</code> (Base Rotation) tags and had the possible values:
* <code>None</code> - No rotation
* <code>Left</code> - 90° Rotation Left
* <code>Right</code> - 90° Rotation Right
* <code>Flip</code> - 180° Rotation

I then needed to pass this to the Tile class and parse it to apply the correct rotation to the model. 

![Parsing the base model rotation](https://dev-to-uploads.s3.amazonaws.com/i/smqi5ptablzbv2qae9s3.PNG)

Once this was done, The test map was recreated as expected in the game and I could walk around from one end to the next and back again.

# Dealing with Diagonal movement between tiles
This was an outstanding issue from the Last development and was only a slight change compared to the already implemented single direction movements.
I started by calculating where the players trajectory would intersect both of the edges that the player crosses.

![Two intersections](https://dev-to-uploads.s3.amazonaws.com/i/zy5o371f5o61o9k07mu0.PNG)

Then I calculated the distance from the start position to each of the intersection points. 

![Distance calcs](https://dev-to-uploads.s3.amazonaws.com/i/pabxbaelez6yj4ctjgub.PNG)

Whichever point is the closest is the point that the player leaves the current tile, special consideration must be given to when the player leaves the tile at one of the corners i.e. when the intersection distance is equal for both edges, knowing this we can set the intersection point and adjust the tile accordingly.

The Calculations for each of the 3 possibilities is nearly identical so I shall just show one:

The intersection point is selected based on which point is closest and the next tile and offset are then set based on which way the tile map is shifting. 

![intersection calculation and offset](https://dev-to-uploads.s3.amazonaws.com/i/d5dvgv7g5lcu4nqrb9s8.PNG)

Finally, the <code>currentTile</code> is adjusted the same way as in the previous development.

![Setting the next tile](https://dev-to-uploads.s3.amazonaws.com/i/k8miu8q10viz4p8u5pkj.PNG)

# Adding to the map
After a short period playing the demo map I soon grew tired of the look of the paths, and set all the tiles to have the grass base model.

I then decided to add the ability to add models to each of the tiles, to do this I needed to specify the following in the .vtf files:
1. The object to place on the map.
2. The position of the object relative to the tile.
3. The rotation of the object (only Y axis rotation for now).

The easiest way to implement this for now was on a single line, starting with a <code>GO</code> (Game Object) tag, followed by the x offset, y offset, z offset, rotation and a final closing <code>GO</code> tag.

![Game Object Definition in VTF file](https://dev-to-uploads.s3.amazonaws.com/i/flz1k58mabzm5dnd8r5x.PNG)

I created two models to test this out, a new rock model and a tree model which I extracted from the previous tree base model.

Next I updated the <code>VrTileFormatParser</code> class to parse the Game Object definitions and pass them onto the generated tile class as an <code>ArrayList</code> called <code>gameObjects</code>.

![gameObjects ArrayList definition](https://dev-to-uploads.s3.amazonaws.com/i/nos5xlvs9hkekquxh44v.PNG)

Parsing the GO tags was slightly more involved than the other tags as there are multiple information points on the single line, but this is no more complicated than parsing the obj files.

![Parsing GO Tags](https://dev-to-uploads.s3.amazonaws.com/i/2owrq4e3g77hy0c4ghgo.PNG)

Finally I needed to process the game object data in the <code>Tile</code> class, in the <code>initialise</code> method I added a loop to go through all the <code>Triple</code>s passed into the class and create/modify objects based on their values.

![Adding Game Objects to a tile](https://dev-to-uploads.s3.amazonaws.com/i/cod16u6lsfgxbr3sdgur.PNG)

# Conclusions
This seems to work pretty well, I have one tile with a few trees and rocks dotted across it and it looks pretty cool so far, it does remain to be seen if there are any performance issues with this but that can be looked at in future. 

![Populated Tile](https://dev-to-uploads.s3.amazonaws.com/i/kmxjjilf8u1n0q1zork5.png)

One final thing, As this platform (dev.to) doesn't seem to have any other game development logs on it, I am going to move this one to somewhere more appropriate, when I decide where this will be I shall update this post with details.