DEV Community

Graham Long
Graham Long

Posted on

Gamedevlog: TileMap part 2

Building on part 1

This will be a quick post this week, the next step on from part 1 is to move the tile that the player would have moved to, into the center and adjust all the other tiles accordingly.

To do this I modified the getPlayerPositionOnTileMap function, so that as well as adjusting the players position, I set bits in a moveDirection variable to indicate the direction to shift the TileMap, then using a when statement I shifted the tiles around based on the value of moveDirection.
Finally if moveDirection was set to anything apart from DONT_MOVE I needed to reset all the tile offsets to their new positions.

Building on this

Next, I wanted to spawn new tiles rather then just shuffling the existing ones, to do this I needed to ensure that the tiles were initialised within an OpenGL context, I couldn’t immediately see a clean way of doing this but in the end, I settled on adding a mModelsHaveNotBeenInitialised variable to the Tile class which was set to true until the initialise function is called, this allowed me to check the initialised state of the variables in the draw function (and therefore within an active openGL context) and if the models had not been initialised I called the initialise function, to make sure the offsets were correct, incase they had been set before the initialise function was called, I added calls to the setPosition function for each model to ensure it was drawn in the correct position.

Other small changes

I noticed that sometimes the new models were being drawn at the origin of the scene before being translated to their correct position, which caused a flickering effect, to fix this I simply added a call to GLES20.glFinish() in the reRenderTheScene function which waits until the rendering is complete before exiting the function.

Additionally I changed the old skybox textures to plain blue sides and a slightly modified cloud tile at the top of the scene.

Demo of the final scene


This all seems to be going a bit too smoothly at the minute....
The next step forward is to make the tile map repeatable so the player can actually navigate consistently further than 2 tiles and back, I think this will be a big step so may be further broken into more than one blog post.

The final code at this point in the project can be found here

Stay Safe.

Discussion (0)