Class for creating a visual 2D grid. More...
#include <TileMap.h>
Public Types | |
using | Ptr = std::shared_ptr< TileMap > |
Public Member Functions | |
void | setPhysicsSimulation (std::shared_ptr< World > physicsSimulation) |
void | setVisible (bool visible) |
Show or hide the tilemap. More... | |
bool | isVisible () const |
Check if the tilemap is visible or not. More... | |
void | toggleVisibility () |
Toggle the visibility of the tilemap. More... | |
void | setPosition (int x, int y) |
Set the position of the tile map. More... | |
Vector2f | getPosition () const |
Get the position of the tilemap in pixels. More... | |
bool | isIndexValid (const Index &index) const |
Check if the index is within bounds of the tilemap or not. More... | |
void | setTileset (const std::string &name, const std::string &filename) |
Set the image to be used as the tileset. More... | |
void | setCurrentTileset (const std::string &name) |
Set the current tileset. More... | |
void | construct (Vector2u size, char id) |
Construct a tilemap. More... | |
void | loadFromFile (const std::string &filename, const char &separator='\0') |
Construct the tilemap from data located on a file on the disk. More... | |
void | loadFromVector (Map map) |
Construct the tilemap form a vector that contains map data. More... | |
void | setCollidableByIndex (const Index &index, bool isCollidable) |
Enable or disable collision for a tile at a certain location. More... | |
void | setCollidableByIndex (const std::initializer_list< Index > &locations, bool isCollidable) |
Enable or disable collision for tiles at certain locations. More... | |
void | setCollidableByIndex (Index startPos, Index endPos, bool isCollidable) |
Enable or disable collisions for tiles in a range. More... | |
void | setCollidableById (char id, bool isCollidable) |
Enable or disable collisions for tiles with a certain id. More... | |
void | setCollidableByExclusion (char id, bool isCollidable) |
Enable or disable collisions for all tiles except those with with a certain id. More... | |
bool | isCollidable (const Index &index) const |
Check if a tile is collidable or not. More... | |
Vector2u | getSize () const |
Get the size of the tilemap in pixels. More... | |
Vector2u | getSizeInTiles () const |
Get the size of the tilemap in tiles. More... | |
Vector2u | getTileSize () const |
Get the size of each tile in the grid. More... | |
unsigned int | getSpaceBetweenTiles () const |
Get the spacing between tiles in all directions. More... | |
const Tile & | getTile (const Index &index) |
Get a tile at a certain index. More... | |
const Tile & | getTile (const Vector2f &position) |
Get the tile at at certain position. More... | |
const Tile & | getTileAbove (const Tile &tile) |
Get the tile above a certain tile. More... | |
const Tile & | getTileBelow (const Tile &tile) |
Get the tile below a certain tile. More... | |
const Tile & | getTileLeftOf (const Tile &tile) |
Get the tile to the left of a certain tile. More... | |
const Tile & | getTileRightOf (const Tile &tile) |
Get the tile to the right of a certain tile. More... | |
void | forEachTileWithId (char id, const Callback< const Tile & > &callback) |
Execute a callback function on every tile with a certain id. More... | |
void | forEachTileExcept (char id, const Callback< const Tile & > &callback) |
Execute a callback on all tiles except tiles with a given id. More... | |
void | forEachTile (const Callback< const Tile & > &callback) |
Execute a callback on all the tiles of the tilemap. More... | |
void | forEachTileInRange (Index startPos, Index endPos, const Callback< const Tile & > &callback) |
Execute a callback function on each tile in a range. More... | |
RenderLayerContainer & | renderLayers () |
Get the tilemap render layers. More... | |
void | textureTile (const Index &index, const UIntRect &rect) |
Texture a tile at given index. More... | |
void | textureTilesById (char id, const UIntRect &rect) |
Apply a texture to all tiles with a certain id. More... | |
void | textureTilesById (char id, const Sprite &sprite) |
Apply a texture to all tiles with a certain id. More... | |
void | draw (Window &renderTarget) |
void | addSprite (Sprite::Ptr sprite, const Index &index, int renderOrder=0, const std::string &renderLayer="default") |
Add a sprite to the tilemap. More... | |
bool | addChild (GameObject::Ptr child, const Index &index, bool assignLayer=true) |
Add an entity to the tilemap. More... | |
GameObject::Ptr | getChildWithId (std::size_t id) const |
Get the child in the tilemap with a certain id. More... | |
bool | hasChild (const GameObject::Ptr &child) |
Check if the tilemap has a certain child or not. More... | |
bool | removeChildFromTile (const Tile &tile, const GameObject::Ptr &child) |
Remove a child from a tile. More... | |
bool | removeOccupant (const Tile &tile) |
Remove an occupant of a tile. More... | |
bool | removeChildWithId (std::size_t id) |
Remove a child with a certain id from the tilemap. More... | |
bool | removeChild (const GameObject::Ptr &child) |
Remove a child from the grid. More... | |
void | removeChildrenIf (const std::function< bool(const GameObject::Ptr &)> &callback) |
Remove children from the grid using a condition. More... | |
bool | removeAllVisitors (const Tile &tile) |
Remove all the visitors of a tile. More... | |
bool | removeAllChildren (const Tile &tile) |
Remove all children in a tile. More... | |
void | moveChild (const GameObject::Ptr &child, const Index &index) |
Move child to a different position in the tilemap. More... | |
void | moveChild (const GameObject::Ptr &child, const Tile &tile) |
Move child to a different tile. More... | |
const Tile & | getTileOccupiedByChild (const GameObject::Ptr &child) |
Get the tile occupied by a child of the tilemap. More... | |
bool | isTileOccupied (const Tile &tile) const |
Check if tile is occupied or not. More... | |
bool | tileHasVisitors (const Tile &tile) const |
Check if the tile at a specified index has visitors or not. More... | |
GameObject::Ptr | getOccupant (const Tile &tile) |
Get the occupant of a tile. More... | |
void | forEachChild (const Callback< const GameObject::Ptr & > &callback) |
Execute a callback for each child in the tilemap. More... | |
void | forEachChildInTile (const Tile &tile, const Callback< const GameObject::Ptr & > &callback) |
Execute a callback for each child in a tile. More... | |
std::size_t | getNumOfOccupants (const Tile &tile) const |
Get the number of occupants in a tile. More... | |
void | update (Time deltaTime) |
Friends | |
class | Scene |
Class for creating a visual 2D grid.
The grid if made up of tiles for visual representation
bool ime::TileMap::addChild | ( | GameObject::Ptr | child, |
const Index & | index, | ||
bool | assignLayer = true |
||
) |
Add an entity to the tilemap.
child | GameObject to add to the tilemap |
index | Index of the tile to add the entity to |
assignLayer | True to assign the game object a render layer |
If the specified tile is already occupied, the child will be added as a visitor of that tile. Note that child will always be placed at the centre point of the tile.
void ime::TileMap::addSprite | ( | Sprite::Ptr | sprite, |
const Index & | index, | ||
int | renderOrder = 0 , |
||
const std::string & | renderLayer = "default" |
||
) |
Add a sprite to the tilemap.
sprite | The sprite to be added to the tilemap |
index | The index of the tile to add the sprite at |
renderOrder | The render order of the sprite in the layer |
renderLayer | The render layer to add the sprite to |
If renderLayer cannot be found, then sprite will be added to the "default" layer. If the default layer cannot be found then the behaviour is undefined. A tilemap without a "default" layer is undefined behavior
Note that the sprite is added at the centre of the the tile
void ime::TileMap::construct | ( | Vector2u | size, |
char | id | ||
) |
Construct a tilemap.
id | The id of each tile |
size | The size of the tilemap in tiles |
This function should be used when there are no special tiles in the tilemap, that is, when all tiles have the same id. The x component of size is the number of rows whilst the y component is the number of columns
void ime::TileMap::forEachChild | ( | const Callback< const GameObject::Ptr & > & | callback | ) |
Execute a callback for each child in the tilemap.
callback | Function to execute |
void ime::TileMap::forEachChildInTile | ( | const Tile & | tile, |
const Callback< const GameObject::Ptr & > & | callback | ||
) |
Execute a callback for each child in a tile.
tile | Tile to execute callback on |
callback | Function to execute |
The callback will be passed the children of the tile, with the first child being the occupant of the tile. The callback will be ignored if the specified index is invalid or the tile is not occupied
Execute a callback on all the tiles of the tilemap.
callback | Function to execute for each tile |
Execute a callback on all tiles except tiles with a given id.
id | Id of the tiles to be ignored |
callback | Function to be executed |
void ime::TileMap::forEachTileInRange | ( | Index | startPos, |
Index | endPos, | ||
const Callback< const Tile & > & | callback | ||
) |
Execute a callback function on each tile in a range.
startPos | The beginning of the range |
endPos | The end of the range |
callback | Function to execute for each tile |
Execute a callback function on every tile with a certain id.
id | Id of the tile |
callback | Function to execute |
GameObject::Ptr ime::TileMap::getChildWithId | ( | std::size_t | id | ) | const |
Get the child in the tilemap with a certain id.
id | Id of the child to get access to |
std::size_t ime::TileMap::getNumOfOccupants | ( | const Tile & | tile | ) | const |
Get the number of occupants in a tile.
tile | Tile to get the number of occupants for |
GameObject::Ptr ime::TileMap::getOccupant | ( | const Tile & | tile | ) |
Get the occupant of a tile.
tile | Tile to get the occupant of |
An occupant is the first child to occupy a tile, subsequent children are considered visitors
Vector2f ime::TileMap::getPosition | ( | ) | const |
Get the position of the tilemap in pixels.
Vector2u ime::TileMap::getSize | ( | ) | const |
Get the size of the tilemap in pixels.
Vector2u ime::TileMap::getSizeInTiles | ( | ) | const |
Get the size of the tilemap in tiles.
unsigned int ime::TileMap::getSpaceBetweenTiles | ( | ) | const |
Get the spacing between tiles in all directions.
The spacing is how far tiles are from one another. The default space between tiles is 1
Get a tile at a certain index.
index | Index of the tile to get |
A tile is invalid if it has a negative index
Get the tile at at certain position.
position | Position of the tile to retrieve |
A tile is invalid if it has a negative index
Get the tile above a certain tile.
tile | Tile to get the tile above of |
A tile is invalid if it has a negative index
Get the tile below a certain tile.
tile | Tile to get the tile below of |
A tile is invalid if it has a negative index
Get the tile to the left of a certain tile.
tile | Tile to get the tile to the left of |
A tile is invalid if it has a negative index
const Tile& ime::TileMap::getTileOccupiedByChild | ( | const GameObject::Ptr & | child | ) |
Get the tile occupied by a child of the tilemap.
child | Child whose occupation tile is to be retrieved |
An invalid tile has a negative index
Get the tile to the right of a certain tile.
tile | Tile to get the tile to the right of |
A tile is invalid if it has a negative index
Vector2u ime::TileMap::getTileSize | ( | ) | const |
Get the size of each tile in the grid.
All the tiles have the same size
bool ime::TileMap::hasChild | ( | const GameObject::Ptr & | child | ) |
Check if the tilemap has a certain child or not.
child | Child to search for in the tilemap |
bool ime::TileMap::isCollidable | ( | const Index & | index | ) | const |
Check if a tile is collidable or not.
index | Index of the tile to be checked |
bool ime::TileMap::isIndexValid | ( | const Index & | index | ) | const |
Check if the index is within bounds of the tilemap or not.
index | Index to check |
bool ime::TileMap::isTileOccupied | ( | const Tile & | tile | ) | const |
bool ime::TileMap::isVisible | ( | ) | const |
Check if the tilemap is visible or not.
void ime::TileMap::loadFromFile | ( | const std::string & | filename, |
const char & | separator = '\0' |
||
) |
Construct the tilemap from data located on a file on the disk.
filename | Name of the file that contains the map data |
separator | Character used to separate map data |
void ime::TileMap::loadFromVector | ( | Map | map | ) |
Construct the tilemap form a vector that contains map data.
map | Vector to construct map from |
void ime::TileMap::moveChild | ( | const GameObject::Ptr & | child, |
const Index & | index | ||
) |
Move child to a different position in the tilemap.
child | Child to move |
index | New position of the child |
The child is ignored if it does not exist in the tilemap or the specified index is invalid
void ime::TileMap::moveChild | ( | const GameObject::Ptr & | child, |
const Tile & | tile | ||
) |
bool ime::TileMap::removeAllChildren | ( | const Tile & | tile | ) |
Remove all children in a tile.
tile | Tile to remove all children from |
bool ime::TileMap::removeAllVisitors | ( | const Tile & | tile | ) |
Remove all the visitors of a tile.
tile | Tile whose visitors are to be removed |
This function will remove all children currently occupying a tile except the occupant of the tile. In this context the occupant is the child that got to the tile before other children
bool ime::TileMap::removeChild | ( | const GameObject::Ptr & | child | ) |
Remove a child from the grid.
child | Child to be removed |
bool ime::TileMap::removeChildFromTile | ( | const Tile & | tile, |
const GameObject::Ptr & | child | ||
) |
Remove a child from a tile.
tile | Tile to remove child from |
child | Child to be removed |
void ime::TileMap::removeChildrenIf | ( | const std::function< bool(const GameObject::Ptr &)> & | callback | ) |
Remove children from the grid using a condition.
callback | Function that determines if the condition is satisfied or not |
All children for which callback returns true are removed from the grid
bool ime::TileMap::removeChildWithId | ( | std::size_t | id | ) |
Remove a child with a certain id from the tilemap.
id | Id of the child to be removed |
bool ime::TileMap::removeOccupant | ( | const Tile & | tile | ) |
Remove an occupant of a tile.
tile | Tile to remove occupant from |
Recall, an occupant is the first child to occupy a tile, if removed, the first visitor will become the new occupant. Subsequent calls to this function will result in visitors taking turns as occupants until the tile is no longer occupied
RenderLayerContainer& ime::TileMap::renderLayers | ( | ) |
Get the tilemap render layers.
Render layers allow the tilemap to be rendered in separate layers which are then composed back together. By default the tilemap has a "default" layer at index 0. When a drawable object is added to the scene without an explicit layer, it will be added to the default layer. You can add objects to the "default" layer or even remove the "default" layer from the render layer container, however you must not forget to reallocate the objects in the "default" layer to another layer, otherwise they will not be drawn to the screen
void ime::TileMap::setCollidableByExclusion | ( | char | id, |
bool | isCollidable | ||
) |
Enable or disable collisions for all tiles except those with with a certain id.
id | Identification of the tiles to exclude |
isCollidable | True to enable collision, otherwise false |
void ime::TileMap::setCollidableById | ( | char | id, |
bool | isCollidable | ||
) |
Enable or disable collisions for tiles with a certain id.
id | Id of the tiles to enable or disable collisions for |
isCollidable | True to enable collision, otherwise false |
All the tiles with the specified id will be marked as solid tiles if currently marked as empty tiles
void ime::TileMap::setCollidableByIndex | ( | const Index & | index, |
bool | isCollidable | ||
) |
Enable or disable collision for a tile at a certain location.
index | Location (in tiles) of the tile |
isCollidable | True to enable collision, otherwise false |
The tile will be marked as a solid tile, if currently set as an empty tile
void ime::TileMap::setCollidableByIndex | ( | const std::initializer_list< Index > & | locations, |
bool | isCollidable | ||
) |
Enable or disable collision for tiles at certain locations.
locations | Locations (in tiles) of the tiles |
isCollidable | True to enable collision, otherwise false |
All the tiles at specified indexes (if valid) will be set as solid tiles if currently set as empty tiles
Enable or disable collisions for tiles in a range.
startPos | The start position of the range |
endPos | The ending position of the range |
isCollidable | True to set collidable, otherwise false |
void ime::TileMap::setCurrentTileset | ( | const std::string & | name | ) |
Set the current tileset.
name | Name of the tileset |
All tileset related operations are performed on the current tileset. In order to tile the map from different tileset, the current tileset must be alternated
void ime::TileMap::setPosition | ( | int | x, |
int | y | ||
) |
Set the position of the tile map.
x | X coordinate of the tile map |
y | Y coordinate of the tile map |
The position is (0, 0) by default
void ime::TileMap::setTileset | ( | const std::string & | name, |
const std::string & | filename | ||
) |
Set the image to be used as the tileset.
name | Name of the tileset |
filename | Filename of the tileset image |
FileNotFound | If the tileset cannot be loaded by the asset manager |
void ime::TileMap::setVisible | ( | bool | visible | ) |
Show or hide the tilemap.
visible | True to show or false to hide the tilemap |
By default, the tilemap is visible
void ime::TileMap::textureTilesById | ( | char | id, |
const Sprite & | sprite | ||
) |
Apply a texture to all tiles with a certain id.
id | Id of the tile to apply texture to |
sprite | Texture to apply |
The texture will be applied as is (i.e the properties of the sprite object (scaling, rotation, origin etc..) will no be altered. This function is useful if the texture from the tileset must be transformed first (scaled, rotated, etc...) before its applied to the tilemap
void ime::TileMap::textureTilesById | ( | char | id, |
const UIntRect & | rect | ||
) |
Apply a texture to all tiles with a certain id.
id | Id of the tile to apply texture to |
rect | Texture to apply |
The current tileset image will be used to texture the tile. The rect defines the sub-rectangle of the tileset to use for texturing the tile. The image will taken as is from the tileset
bool ime::TileMap::tileHasVisitors | ( | const Tile & | tile | ) | const |
Check if the tile at a specified index has visitors or not.
tile | Tile to be checked |
A tile has visitors if its currently occupied by more than one child. The first child to occupy the tile is the occupant of that tile whilst other entities are visitors
void ime::TileMap::toggleVisibility | ( | ) |
Toggle the visibility of the tilemap.
This function will show the tilemap if it is currently visible or hide it if its currently visible