fixed TileMap
|
The game I’m working on, as a programmer, is (temporary called) Green Warden. This game is sort of a Tower Defence where the player runs around with an avatar, planting seeds that grow up to projectile-shooting trees (towers) against army of lumberjacks (and flamethrowers). When the players are building their trees/towers we wanted each tower to fit in in a fixed grid of tiles; like chess, where only one piece can be place on one tile. I wrote a tile map class with three main components: dimensions for each tile (in this case 32x32px), width and height for the whole map (i.e. 30tiles x 20tiles) and an array of bool values where each element of the array represent a tile on the whole tile map. The idea is, when an object is created on a tile, that tile recieves the value ‘true’ so other objects cannot be placed there again, and when that object is destroyed that tile returns to the value ‘false’. This method is hopefully applicable for our oncoming pathfinding too, where, for example, an enemy is looking for empty tiles that has the value ‘false’ which to set a path to walk. All tiles that has value ‘true’ is translated to a non-walkable area for the enemies. In short sense, tiles where the player builds a tower, that tile should block enemies paths and the player. When the player places a tower I had to figure out a way to calculate so the tower gets a fixed position one of the tiles on the tile map. The player could be on the position 197×579 on the screen and place a tower and that tower should fit in to a correct position on the tile map. On this picture I used my mouse to place out the trees. You can see that every tree is positioned on a even order with each tile. The calculation is pretty simple: sf::Vector2i TileMap::getFixedPosition(sf::Vector2i point) { sf::Vector2i fixedPos; fixedPos.x = (point.x / m_tilewidth) * m_tilewidth; fixedPos.y = (point.y / m_tileheight) * m_tileheight; return fixedPos; } where ‘m_tilewidth’ and ‘m_tileheight’ has both the value 64 in this example. Till next time, adios! |
