The tile map
|
I’ve been working on the tile map for our space shooter game. Each and every tile have a different texture and sound when the avatar walks over them, some tiles have collision vs some entities, the avatar, the projectiles and the enemy amongst others. We did not want to hard code the entire map, since this would be time consuming and a pain when needing to create and make changes to the map. Instead the tile map creation that reads from a .txt file, filled with numbers, where every number represent a different tile type. By doing this, the level designer can easily create a map in a tile map editing software (we’re using the program Tiled), that we instantly can implement in game. Instead of writing an entire new class for each and every tile type, I did it so all different tile types are different instances of the same tile class. Since the tiles would basically have the same methods and member variables, and the only things separating them from one another is its texture, the sound they emit when the player walks over it, and what the tile has collision against, I felt it would take to much time writing basically the same class with minor differences, several times. During the instantiation the class the type of tile is decided and separated with an enum tag. The constructor takes an integer deciding the tile type, two additional integers handling the positioning of the tile, and a string handling the file path to the tile sheet. The tile map creation is handled in a method i Gamestate class that takes a file path to a .txt file (an std::string) as a parameter. The .txt file is iterated through in two for-loops, and each number in the .txt file is used in the creation of a new instance of the tile. When handling the collision and walk sound on different tiles, the player checks what tile it currently has collision with and plays the corresponding sound for that tile. If the tile the player collides with is a wall, the player cannot walk through it. I had some bugs and issues with tile vs player collision, where the player would shake and be pushed out of the tile to much if collision was detected. This bug was simply fixed, and was caused by the way the loops iterating the entities and tile map was written. Whenever player vs wall tile collision was detected, the player would be pushed out of the tile several times instead of only once, since the action was excited as many times as the number of entities. However, if the player has collision with more than one tile at once, it still shakes a bit, however the perception of the shaking will be less intense if we change the camera to not center the player in the middle of the screen. – A screenshot of the tile map ingame. |
