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

A 2D visual grid. More...

#include <Grid2D.h>

Public Member Functions

 Grid2D (unsigned int tileWidth, unsigned int tileHeight, Scene &scene)
 
void setPhysicsEngine (PhysicsEngine *engine)
 
ScenegetScene ()
 Get the scene the grid belongs to. More...
 
const ScenegetScene () const
 
unsigned int getRowCount () const
 Get the number of rows. More...
 
unsigned int getColumnCount () const
 Get the number of columns. More...
 
Grid2DRenderergetRenderer ()
 Get the grids renderer. More...
 
const Grid2DRenderergetRenderer () const
 
void setPosition (int x, int y)
 Set the position of the grid. More...
 
Vector2f getPosition () const
 Get the position of the grid in pixels. More...
 
bool isIndexValid (const Index &index) const
 Check if the index is within bounds of the grid or not. More...
 
void construct (const Vector2u &size, char id='\0')
 Construct the grid from its dimensions. More...
 
void loadFromFile (const std::string &filename, const char &separator='\0')
 Construct the grid from data located on a file on the disk. More...
 
void loadFromVector (Map map)
 Construct the grid form a vector that contains map data. More...
 
void setCollidableByIndex (const Index &index, bool isCollidable, bool attachCollider=false)
 Enable or disable collision for a tile at a certain location. More...
 
void setCollidableByIndex (const std::initializer_list< Index > &locations, bool isCollidable, bool attachCollider=false)
 Enable or disable collision for tiles at certain locations. More...
 
void setCollidableByIndex (Index startPos, Index endPos, bool isCollidable, bool attachCollider=false)
 Enable or disable collisions for tiles in a range. More...
 
void setCollidableById (char id, bool isCollidable, bool attachCollider=false)
 Enable or disable collisions for tiles with a certain id. More...
 
void setCollidableByExclusion (char id, bool isCollidable, bool attachCollider=false)
 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 grid, in pixels. More...
 
Vector2u getSizeInTiles () const
 Get the size of the grid, 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) const
 Get a tile at a certain index. More...
 
const TilegetTile (const Vector2f &position) const
 Get the tile at at certain position. More...
 
const TilegetTileAbove (const Tile &tile) const
 Get the tile above a certain tile. More...
 
const TilegetTileBelow (const Tile &tile) const
 Get the tile below a certain tile. More...
 
const TilegetTileLeftOf (const Tile &tile) const
 Get the tile to the left of a certain tile. More...
 
const TilegetTileRightOf (const Tile &tile) const
 Get the tile to the right of a certain tile. More...
 
void forEachTileWithId (char id, const Callback< const Tile & > &callback) const
 Execute a callback function on every tile with a certain id. More...
 
void forEachTileExcept (char id, const Callback< const Tile & > &callback) const
 Execute a callback on all tiles except tiles with a given id. More...
 
void forEachTile (const Callback< const Tile & > &callback) const
 Execute a callback on all the tiles of the grid. More...
 
void forEachTileInRange (Index startPos, Index endPos, const Callback< const Tile & > &callback) const
 Execute a callback function on each tile in a range. More...
 
void draw (priv::RenderTarget &renderTarget) const
 
bool addChild (GridObject *child, const Index &index)
 Add an GridObject to the grid. More...
 
GridObjectgetChildWithId (std::size_t id) const
 Get the child in the grid with a certain id. More...
 
bool hasChild (const GridObject *child) const
 Check if the grid has a certain child or not. More...
 
bool removeChildWithId (std::size_t id)
 Remove a child with a certain id from the grid. More...
 
bool removeChild (GridObject *child)
 Remove a child from the grid. More...
 
void removeChildIf (const std::function< bool(GridObject *)> &callback)
 Remove children from the grid using a condition. More...
 
void removeAllChildren ()
 Remove all children from the grid. More...
 
void changeTile (GridObject *child, const Index &index)
 Move child to a different position in the grid. More...
 
void changeTile (GridObject *child, const Tile &tile)
 Move child to a different tile. More...
 
const TilegetTileOccupiedByChild (const GridObject *child) const
 Get the tile occupied by a child of the grid. More...
 
bool isTileOccupied (const Tile &tile) const
 Check if tile is occupied or not. More...
 
bool isTileOccupied (const Index &index) const
 Check if a tile is occupied or not. More...
 
void forEachChild (const Callback< GridObject * > &callback) const
 Execute a callback for each child in the grid. More...
 
void forEachChildInTile (const Tile &tile, const Callback< GridObject * > &callback) const
 Execute a callback for each child in a tile. More...
 
void update (Time deltaTime)
 
 ~Grid2D ()
 Destructor. More...
 

Friends

class Scene
 

Detailed Description

A 2D visual grid.

Definition at line 54 of file Grid2D.h.

Constructor & Destructor Documentation

◆ ~Grid2D()

ime::Grid2D::~Grid2D ( )

Destructor.

Member Function Documentation

◆ addChild()

bool ime::Grid2D::addChild ( GridObject child,
const Index index 
)

Add an GridObject to the grid.

Parameters
childGridObject to add to the grid
indexIndex of the tile to add the grid object to
Returns
True if the grid object has been added or false if the index is invalid or the grid object already exists in the grid

Note that the child is placed at the centre of the tile.

◆ changeTile() [1/2]

void ime::Grid2D::changeTile ( GridObject child,
const Index index 
)

Move child to a different position in the grid.

Parameters
childChild to move
indexNew position of the child

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

See also
addChild

◆ changeTile() [2/2]

void ime::Grid2D::changeTile ( GridObject 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 grid or the specified tile is invalid

See also
addChild

◆ construct()

void ime::Grid2D::construct ( const Vector2u size,
char  id = '\0' 
)

Construct the grid from its dimensions.

Parameters
sizeThe size of the grid in tiles
idThe id of each tile

Note that the x component of the size argument is the number of rows whilst the y component is the number of columns

See also
loadFromFile and loadFromVector

◆ forEachChild()

void ime::Grid2D::forEachChild ( const Callback< GridObject * > &  callback) const

Execute a callback for each child in the grid.

Parameters
callbackFunction to execute

◆ forEachChildInTile()

void ime::Grid2D::forEachChildInTile ( const Tile tile,
const Callback< GridObject * > &  callback 
) const

Execute a callback for each child in a tile.

Parameters
tileTile to execute callback on
callbackFunction to execute

◆ forEachTile()

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

Execute a callback on all the tiles of the grid.

Parameters
callbackFunction to execute for each tile

◆ forEachTileExcept()

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

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::Grid2D::forEachTileInRange ( Index  startPos,
Index  endPos,
const Callback< const Tile & > &  callback 
) const

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::Grid2D::forEachTileWithId ( char  id,
const Callback< const Tile & > &  callback 
) const

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

Parameters
idId of the tile
callbackFunction to execute

◆ getChildWithId()

GridObject * ime::Grid2D::getChildWithId ( std::size_t  id) const

Get the child in the grid with a certain id.

Parameters
idThe id of the child to be retrieved
Returns
The child with the specified id or a nullptr if the child with the specified id does not exist in the grid

◆ getColumnCount()

unsigned int ime::Grid2D::getColumnCount ( ) const

Get the number of columns.

Returns
The number of columns
See also
getRowCount

◆ getPosition()

Vector2f ime::Grid2D::getPosition ( ) const

Get the position of the grid in pixels.

Returns
The position of the grid pixels

◆ getRenderer()

Grid2DRenderer & ime::Grid2D::getRenderer ( )

Get the grids renderer.

Returns
The grids renderer

The renderer gives access to functions that determine how the grid is displayed. It allows you to manipulate things such as the tile colour, grid line colour etc...

◆ getRowCount()

unsigned int ime::Grid2D::getRowCount ( ) const

Get the number of rows.

Returns
The number of rows
See also
getColumnCount

◆ getScene()

Scene & ime::Grid2D::getScene ( )

Get the scene the grid belongs to.

Returns
The scene the grid belongs to

◆ getSize()

Vector2u ime::Grid2D::getSize ( ) const

Get the size of the grid, in pixels.

Returns
Size of the grid in pixels

◆ getSizeInTiles()

Vector2u ime::Grid2D::getSizeInTiles ( ) const

Get the size of the grid, in tiles.

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

◆ getSpaceBetweenTiles()

unsigned int ime::Grid2D::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::Grid2D::getTile ( const Index index) const

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 grid

A tile is invalid if it has a negative index

◆ getTile() [2/2]

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

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 grid bounds

A tile is invalid if it has a negative index

◆ getTileAbove()

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

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::Grid2D::getTileBelow ( const Tile tile) const

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::Grid2D::getTileLeftOf ( const Tile tile) const

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::Grid2D::getTileOccupiedByChild ( const GridObject child) const

Get the tile occupied by a child of the grid.

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 grid

An invalid tile has a negative index

◆ getTileRightOf()

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

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::Grid2D::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::Grid2D::hasChild ( const GridObject child) const

Check if the grid has a certain child or not.

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

◆ isCollidable()

bool ime::Grid2D::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::Grid2D::isIndexValid ( const Index index) const

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

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

◆ isTileOccupied() [1/2]

bool ime::Grid2D::isTileOccupied ( const Index index) const

Check if a tile is occupied or not.

Parameters
indexThe position of the tile to be checked
Returns
True if the tile is occupied, otherwise false

A tile is occupied if it has at least one child

This function returns false if index is out of bounds

See also
addChild

◆ isTileOccupied() [2/2]

bool ime::Grid2D::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

◆ loadFromFile()

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

Construct the grid 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
Exceptions
FileNotFoundExceptionIf filename cannot be opened for reading
InvalidParseExceptionIf the contents of filename cannot be successfully parsed into Grid2D map data
See also
loadFromVector

◆ loadFromVector()

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

Construct the grid form a vector that contains map data.

Parameters
mapThe vector to construct the map from
See also
loadFromFile

◆ removeAllChildren()

void ime::Grid2D::removeAllChildren ( )

Remove all children from the grid.

◆ removeChild()

bool ime::Grid2D::removeChild ( GridObject 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

◆ removeChildIf()

void ime::Grid2D::removeChildIf ( const std::function< bool(GridObject *)> &  callback)

Remove children from the grid using a condition.

Parameters
callbackFunction that determines if the condition is satisfied or not

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

◆ removeChildWithId()

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

Remove a child with a certain id from the grid.

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 grid

◆ setCollidableByExclusion()

void ime::Grid2D::setCollidableByExclusion ( char  id,
bool  isCollidable,
bool  attachCollider = false 
)

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
attachColliderTrue to attach a Collider to the tile, otherwise false
Exceptions
InvalidArgumentExceptionIf attachCollider is true and the Scene this grid belongs to does not have a PhysicsEngine

Without a Collider, only game objects that are controlled by a GridMover can collide with the tile. Attaching a collider makes a GridObject with a RigidBody that has a Collider attached to it able to collide with the tile. Note that a collider can only be attached if the Scene this Grid2D belongs to has a PhysicsEngine

By default, tiles are not collidable

◆ setCollidableById()

void ime::Grid2D::setCollidableById ( char  id,
bool  isCollidable,
bool  attachCollider = false 
)

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
attachColliderTrue to attach a Collider to the tile, otherwise false
Exceptions
InvalidArgumentExceptionIf attachCollider is true and the Scene this grid belongs to does not have a PhysicsEngine

Without a Collider, only game objects that are controlled by a GridMover can collide with the tile. Attaching a collider makes a GridObject with a RigidBody that has a Collider attached to it able to collide with the tile. Note that a collider can only be attached if the Scene this Grid2D belongs to has a PhysicsEngine

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

By default, tiles are not collidable

◆ setCollidableByIndex() [1/3]

void ime::Grid2D::setCollidableByIndex ( const Index index,
bool  isCollidable,
bool  attachCollider = false 
)

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

Parameters
indexLocation (in tiles) of the tile
isCollidableTrue to enable collision, otherwise false
attachColliderTrue to attach a Collider to the tile, otherwise false
Exceptions
InvalidArgumentExceptionIf attachCollider is true and the Scene this grid belongs to does not have a PhysicsEngine

Without a Collider, only game objects that are controlled by a GridMover can collide with the tile. Attaching a collider makes a GridObject with a RigidBody that has a Collider attached to it able to collide with the tile. Note that a collider can only be attached if the Scene this Grid2D belongs to has a PhysicsEngine

◆ setCollidableByIndex() [2/3]

void ime::Grid2D::setCollidableByIndex ( const std::initializer_list< Index > &  locations,
bool  isCollidable,
bool  attachCollider = false 
)

Enable or disable collision for tiles at certain locations.

Parameters
locationsLocations (in tiles) of the tiles
isCollidableTrue to enable collision, otherwise false
attachColliderTrue to attach a Collider to the tile, otherwise false
Exceptions
InvalidArgumentExceptionIf attachCollider is true and the Scene this grid belongs to does not have a PhysicsEngine

Without a Collider, only game objects that are controlled by a GridMover can collide with the tile. Attaching a collider makes a GridObject with a RigidBody that has a Collider attached to it able to collide with the tile. Note that a collider can only be attached if the Scene this Grid2D belongs to has a PhysicsEngine

By default, tiles are not collidable

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::Grid2D::setCollidableByIndex ( Index  startPos,
Index  endPos,
bool  isCollidable,
bool  attachCollider = false 
)

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
attachColliderTrue to attach a Collider to the tile, otherwise false
Exceptions
InvalidArgumentExceptionIf attachCollider is true and the Scene this grid belongs to does not have a PhysicsEngine

Without a Collider, only game objects that are controlled by a GridMover can collide with the tile. Attaching a collider makes a GridObject with a RigidBody that has a Collider attached to it able to collide with the tile. Note that a collider can only be attached if the Scene this Grid2D belongs to has a PhysicsEngine

By default, tiles are not collidable

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

◆ setPosition()

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

Set the position of the grid.

Parameters
xX coordinate of the grid
yY coordinate of the grid

The position is (0, 0) by default

Friends And Related Function Documentation

◆ Scene

friend class Scene
friend

Definition at line 669 of file Grid2D.h.


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