Public Types | Public Member Functions | Friends | List of all members
ime::TileMap Class Reference

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 TilegetTile (const Index &index)
 Get a tile at a certain index. More...
 
const TilegetTile (const Vector2f &position)
 Get the tile at at certain position. More...
 
const TilegetTileAbove (const Tile &tile)
 Get the tile above a certain tile. More...
 
const TilegetTileBelow (const Tile &tile)
 Get the tile below a certain tile. More...
 
const TilegetTileLeftOf (const Tile &tile)
 Get the tile to the left of a certain tile. More...
 
const TilegetTileRightOf (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...
 
RenderLayerContainerrenderLayers ()
 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 TilegetTileOccupiedByChild (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
 

Detailed Description

Class for creating a visual 2D grid.

The grid if made up of tiles for visual representation

Definition at line 52 of file TileMap.h.

Member Function Documentation

◆ addChild()

bool ime::TileMap::addChild ( GameObject::Ptr  child,
const Index index,
bool  assignLayer = true 
)

Add an entity to the tilemap.

Parameters
childGameObject to add to the tilemap
indexIndex of the tile to add the entity to
assignLayerTrue to assign the game object a render layer
Returns
True if the entity has been added or false if the index is invalid or the entity already exists in the tilemap

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.

Note
If assignLayer is set to true, the game object will be added to the default render layer and assigned a render order of 0. When it is set to false, it must be manually assigned to one of the tilemap's render layers, otherwise it will not be drawn on the screen
Warning
If the default render layer is removed, then assignLayer must always be set to false, otherwise undefined behavior
See also
renderLayers

◆ addSprite()

void ime::TileMap::addSprite ( Sprite::Ptr  sprite,
const Index index,
int  renderOrder = 0,
const std::string &  renderLayer = "default" 
)

Add a sprite to the tilemap.

Parameters
spriteThe sprite to be added to the tilemap
indexThe index of the tile to add the sprite at
renderOrderThe render order of the sprite in the layer
renderLayerThe 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

◆ construct()

void ime::TileMap::construct ( Vector2u  size,
char  id 
)

Construct a tilemap.

Parameters
idThe id of each tile
sizeThe 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

See also
loadFromFile and loadFromVector

◆ forEachChild()

void ime::TileMap::forEachChild ( const Callback< const GameObject::Ptr & > &  callback)

Execute a callback for each child in the tilemap.

Parameters
callbackFunction to execute

◆ forEachChildInTile()

void ime::TileMap::forEachChildInTile ( const Tile tile,
const Callback< const GameObject::Ptr & > &  callback 
)

Execute a callback for each child in a tile.

Parameters
tileTile to execute callback on
callbackFunction 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

◆ forEachTile()

void ime::TileMap::forEachTile ( const Callback< const Tile & > &  callback)

Execute a callback on all the tiles of the tilemap.

Parameters
callbackFunction to execute for each tile

◆ forEachTileExcept()

void ime::TileMap::forEachTileExcept ( char  id,
const Callback< const Tile & > &  callback 
)

Execute a callback on all tiles except tiles with a given id.

Parameters
idId of the tiles to be ignored
callbackFunction to be executed

◆ forEachTileInRange()

void ime::TileMap::forEachTileInRange ( Index  startPos,
Index  endPos,
const Callback< const Tile & > &  callback 
)

Execute a callback function on each tile in a range.

Parameters
startPosThe beginning of the range
endPosThe end of the range
callbackFunction to execute for each tile
Note
Only horizontal ranges are supported

◆ forEachTileWithId()

void ime::TileMap::forEachTileWithId ( char  id,
const Callback< const Tile & > &  callback 
)

Execute a callback function on every tile with a certain id.

Parameters
idId of the tile
callbackFunction to execute

◆ getChildWithId()

GameObject::Ptr ime::TileMap::getChildWithId ( std::size_t  id) const

Get the child in the tilemap with a certain id.

Parameters
idId of the child to get access to
Returns
The child with the specified id or a nullptr if the child with the specified id does not exist in the tilemap

◆ getNumOfOccupants()

std::size_t ime::TileMap::getNumOfOccupants ( const Tile tile) const

Get the number of occupants in a tile.

Parameters
tileTile to get the number of occupants for
Returns
The number of occupants in a tile

◆ getOccupant()

GameObject::Ptr ime::TileMap::getOccupant ( const Tile tile)

Get the occupant of a tile.

Parameters
tileTile to get the occupant of
Returns
The occupant of the specified tile if the tile is occupied, otherwise a nullptr

An occupant is the first child to occupy a tile, subsequent children are considered visitors

See also
forEachTile(Tile&, Callback)

◆ getPosition()

Vector2f ime::TileMap::getPosition ( ) const

Get the position of the tilemap in pixels.

Returns
The position of the tilemap pixels

◆ getSize()

Vector2u ime::TileMap::getSize ( ) const

Get the size of the tilemap in pixels.

Returns
Size of the tilemap in pixels

◆ getSizeInTiles()

Vector2u ime::TileMap::getSizeInTiles ( ) const

Get the size of the tilemap in tiles.

Returns
Size of the tilemap in tiles
Warning
The x component is the number of columns whilst the y component is the number of rows

◆ getSpaceBetweenTiles()

unsigned int ime::TileMap::getSpaceBetweenTiles ( ) const

Get the spacing between tiles in all directions.

Returns
The spacing between tiles

The spacing is how far tiles are from one another. The default space between tiles is 1

◆ getTile() [1/2]

const Tile& ime::TileMap::getTile ( const Index index)

Get a tile at a certain index.

Parameters
indexIndex of the tile to get
Returns
The tile at the specified index or an invalid tile if the specified index is out of bounds of the tilemap

A tile is invalid if it has a negative index

◆ getTile() [2/2]

const Tile& ime::TileMap::getTile ( const Vector2f position)

Get the tile at at certain position.

Parameters
positionPosition of the tile to retrieve
Returns
The tile at the specified position or an invalid tile if the specified position does not lie within the tilemap bounds

A tile is invalid if it has a negative index

◆ getTileAbove()

const Tile& ime::TileMap::getTileAbove ( const Tile tile)

Get the tile above a certain tile.

Parameters
tileTile to get the tile above of
Returns
The tile above the specified tile or an invalid tile if there is no tile above the specified tile

A tile is invalid if it has a negative index

◆ getTileBelow()

const Tile& ime::TileMap::getTileBelow ( const Tile tile)

Get the tile below a certain tile.

Parameters
tileTile to get the tile below of
Returns
The tile below the specified tile or an invalid tile if there is no tile below the specified tile

A tile is invalid if it has a negative index

◆ getTileLeftOf()

const Tile& ime::TileMap::getTileLeftOf ( const Tile tile)

Get the tile to the left of a certain tile.

Parameters
tileTile to get the tile to the left of
Returns
The tile to the left of the specified tile or an invalid tile if there's no tile to the left of the specified tile

A tile is invalid if it has a negative index

◆ getTileOccupiedByChild()

const Tile& ime::TileMap::getTileOccupiedByChild ( const GameObject::Ptr child)

Get the tile occupied by a child of the tilemap.

Parameters
childChild whose occupation tile is to be retrieved
Returns
The tile occupied by the specified child or an invalid tile if the child is not in the tilemap

An invalid tile has a negative index

◆ getTileRightOf()

const Tile& ime::TileMap::getTileRightOf ( const Tile tile)

Get the tile to the right of a certain tile.

Parameters
tileTile to get the tile to the right of
Returns
The tile to the right of the specified tile or an invalid tile if there's no tile to the left of the specified tile

A tile is invalid if it has a negative index

◆ getTileSize()

Vector2u ime::TileMap::getTileSize ( ) const

Get the size of each tile in the grid.

Returns
The size of each tile in the grid

All the tiles have the same size

◆ hasChild()

bool ime::TileMap::hasChild ( const GameObject::Ptr child)

Check if the tilemap has a certain child or not.

Parameters
childChild to search for in the tilemap
Returns
True if the tilemap has the child, otherwise false

◆ isCollidable()

bool ime::TileMap::isCollidable ( const Index index) const

Check if a tile is collidable or not.

Parameters
indexIndex of the tile to be checked
Returns
True if tile is collidable, or false if the tile is not collidable or the index is invalid

◆ isIndexValid()

bool ime::TileMap::isIndexValid ( const Index index) const

Check if the index is within bounds of the tilemap or not.

Parameters
indexIndex to check
Returns
True if the index is within bounds, otherwise false

◆ isTileOccupied()

bool ime::TileMap::isTileOccupied ( const Tile tile) const

Check if tile is occupied or not.

Parameters
tileTile to be checked
Returns
True if the tile is occupied, otherwise false

A tile is occupied if it has at least one child

See also
addChild

◆ isVisible()

bool ime::TileMap::isVisible ( ) const

Check if the tilemap is visible or not.

Returns
True if the tilemap is visible, otherwise false

◆ loadFromFile()

void ime::TileMap::loadFromFile ( const std::string &  filename,
const char &  separator = '\0' 
)

Construct the tilemap from data located on a file on the disk.

Parameters
filenameName of the file that contains the map data
separatorCharacter used to separate map data

◆ loadFromVector()

void ime::TileMap::loadFromVector ( Map  map)

Construct the tilemap form a vector that contains map data.

Parameters
mapVector to construct map from

◆ moveChild() [1/2]

void ime::TileMap::moveChild ( const GameObject::Ptr child,
const Index index 
)

Move child to a different position in the tilemap.

Parameters
childChild to move
indexNew position of the child

The child is ignored if it does not exist in the tilemap or the specified index is invalid

See also
addChild

◆ moveChild() [2/2]

void ime::TileMap::moveChild ( const GameObject::Ptr child,
const Tile tile 
)

Move child to a different tile.

Parameters
childChild to be moved
tileTile to move child to

The child is ignored if it does not exist in the tilemap or the specified tile is invalid

See also
addChild

◆ removeAllChildren()

bool ime::TileMap::removeAllChildren ( const Tile tile)

Remove all children in a tile.

Parameters
tileTile to remove all children from
Returns
True if all children were removed, or false if the tile is not occupied
See also
removeAllVisitors and removeOccupant

◆ removeAllVisitors()

bool ime::TileMap::removeAllVisitors ( const Tile tile)

Remove all the visitors of a tile.

Parameters
tileTile whose visitors are to be removed
Returns
True if all visitors have been removed or false if the tile has no visitors

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

See also
tileHasVisitors

◆ removeChild()

bool ime::TileMap::removeChild ( const GameObject::Ptr child)

Remove a child from the grid.

Parameters
childChild to be removed
Returns
True if the child was removed or false if the child does not exist in the grid

◆ removeChildFromTile()

bool ime::TileMap::removeChildFromTile ( const Tile tile,
const GameObject::Ptr child 
)

Remove a child from a tile.

Parameters
tileTile to remove child from
childChild to be removed
Returns
True if the child was removed or false if the child is not in the specified tile

◆ removeChildrenIf()

void ime::TileMap::removeChildrenIf ( const std::function< bool(const GameObject::Ptr &)> &  callback)

Remove children from the grid using a condition.

Parameters
callbackFunction that determines if the condition is satisfied or not

All children for which callback returns true are removed from the grid

◆ removeChildWithId()

bool ime::TileMap::removeChildWithId ( std::size_t  id)

Remove a child with a certain id from the tilemap.

Parameters
idId of the child to be removed
Returns
True if the child was removed or false if the child with the specified id does not exist in the tilemap

◆ removeOccupant()

bool ime::TileMap::removeOccupant ( const Tile tile)

Remove an occupant of a tile.

Parameters
tileTile to remove occupant from
Returns
True if the occupant was removed or false if the tile is not occupied or it is invalid

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

◆ renderLayers()

RenderLayerContainer& ime::TileMap::renderLayers ( )

Get the tilemap render layers.

Returns
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

◆ setCollidableByExclusion()

void ime::TileMap::setCollidableByExclusion ( char  id,
bool  isCollidable 
)

Enable or disable collisions for all tiles except those with with a certain id.

Parameters
idIdentification of the tiles to exclude
isCollidableTrue to enable collision, otherwise false

◆ setCollidableById()

void ime::TileMap::setCollidableById ( char  id,
bool  isCollidable 
)

Enable or disable collisions for tiles with a certain id.

Parameters
idId of the tiles to enable or disable collisions for
isCollidableTrue to enable collision, otherwise false

All the tiles with the specified id will be marked as solid tiles if currently marked as empty tiles

◆ setCollidableByIndex() [1/3]

void ime::TileMap::setCollidableByIndex ( const Index index,
bool  isCollidable 
)

Enable or disable collision for a tile at a certain location.

Parameters
indexLocation (in tiles) of the tile
isCollidableTrue to enable collision, otherwise false

The tile will be marked as a solid tile, if currently set as an empty tile

◆ setCollidableByIndex() [2/3]

void ime::TileMap::setCollidableByIndex ( const std::initializer_list< Index > &  locations,
bool  isCollidable 
)

Enable or disable collision for tiles at certain locations.

Parameters
locationsLocations (in tiles) of the tiles
isCollidableTrue 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

◆ setCollidableByIndex() [3/3]

void ime::TileMap::setCollidableByIndex ( Index  startPos,
Index  endPos,
bool  isCollidable 
)

Enable or disable collisions for tiles in a range.

Parameters
startPosThe start position of the range
endPosThe ending position of the range
isCollidableTrue to set collidable, otherwise false
Warning
Only horizontal ranges are supported. This means that that the indexes in the range [startPos, endPos] must refer to tiles that are valid and horizontally contiguous

◆ setCurrentTileset()

void ime::TileMap::setCurrentTileset ( const std::string &  name)

Set the current tileset.

Parameters
nameName 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

◆ setPosition()

void ime::TileMap::setPosition ( int  x,
int  y 
)

Set the position of the tile map.

Parameters
xX coordinate of the tile map
yY coordinate of the tile map

The position is (0, 0) by default

◆ setTileset()

void ime::TileMap::setTileset ( const std::string &  name,
const std::string &  filename 
)

Set the image to be used as the tileset.

Parameters
nameName of the tileset
filenameFilename of the tileset image
Exceptions
FileNotFoundIf the tileset cannot be loaded by the asset manager

◆ setVisible()

void ime::TileMap::setVisible ( bool  visible)

Show or hide the tilemap.

Parameters
visibleTrue to show or false to hide the tilemap

By default, the tilemap is visible

◆ textureTile()

void ime::TileMap::textureTile ( const Index index,
const UIntRect rect 
)

Texture a tile at given index.

Parameters
indexIndex of the tile to apply a texture to
rectTexture 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

◆ textureTilesById() [1/2]

void ime::TileMap::textureTilesById ( char  id,
const Sprite sprite 
)

Apply a texture to all tiles with a certain id.

Parameters
idId of the tile to apply texture to
spriteTexture 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

◆ textureTilesById() [2/2]

void ime::TileMap::textureTilesById ( char  id,
const UIntRect rect 
)

Apply a texture to all tiles with a certain id.

Parameters
idId of the tile to apply texture to
rectTexture 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

See also
textureTilesById(char, const Sprite&)

◆ tileHasVisitors()

bool ime::TileMap::tileHasVisitors ( const Tile tile) const

Check if the tile at a specified index has visitors or not.

Parameters
tileTile to be checked
Returns
True if the tile has visitors, otherwise false

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

◆ toggleVisibility()

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


The documentation for this class was generated from the following file: