A 2D visual grid. More...
#include <Grid2D.h>
Public Member Functions | |
Grid2D (unsigned int tileWidth, unsigned int tileHeight, Scene &scene) | |
void | setPhysicsEngine (PhysicsEngine *engine) |
Scene & | getScene () |
Get the scene the grid belongs to. More... | |
const Scene & | getScene () const |
unsigned int | getRowCount () const |
Get the number of rows. More... | |
unsigned int | getColumnCount () const |
Get the number of columns. More... | |
Grid2DRenderer & | getRenderer () |
Get the grids renderer. More... | |
const Grid2DRenderer & | getRenderer () 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 Tile & | getTile (const Index &index) const |
Get a tile at a certain index. More... | |
const Tile & | getTile (const Vector2f &position) const |
Get the tile at at certain position. More... | |
const Tile & | getTileAbove (const Tile &tile) const |
Get the tile above a certain tile. More... | |
const Tile & | getTileBelow (const Tile &tile) const |
Get the tile below a certain tile. More... | |
const Tile & | getTileLeftOf (const Tile &tile) const |
Get the tile to the left of a certain tile. More... | |
const Tile & | getTileRightOf (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... | |
GridObject * | getChildWithId (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 Tile & | getTileOccupiedByChild (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 |
ime::Grid2D::~Grid2D | ( | ) |
Destructor.
bool ime::Grid2D::addChild | ( | GridObject * | child, |
const Index & | index | ||
) |
Add an GridObject to the grid.
child | GridObject to add to the grid |
index | Index of the tile to add the grid object to |
Note that the child is placed at the centre of the tile.
void ime::Grid2D::changeTile | ( | GridObject * | child, |
const Index & | index | ||
) |
Move child to a different position in the grid.
child | Child to move |
index | New position of the child |
The child is ignored if it does not exist in the grid or the specified index is invalid
void ime::Grid2D::changeTile | ( | GridObject * | child, |
const Tile & | tile | ||
) |
void ime::Grid2D::construct | ( | const Vector2u & | size, |
char | id = '\0' |
||
) |
Construct the grid from its dimensions.
size | The size of the grid in tiles |
id | The 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
void ime::Grid2D::forEachChild | ( | const Callback< GridObject * > & | callback | ) | const |
Execute a callback for each child in the grid.
callback | Function to execute |
void ime::Grid2D::forEachChildInTile | ( | const Tile & | tile, |
const Callback< GridObject * > & | callback | ||
) | const |
Execute a callback for each child in a tile.
tile | Tile to execute callback on |
callback | Function to execute |
Execute a callback on all the tiles of the grid.
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::Grid2D::forEachTileInRange | ( | Index | startPos, |
Index | endPos, | ||
const Callback< const Tile & > & | callback | ||
) | const |
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 |
GridObject * ime::Grid2D::getChildWithId | ( | std::size_t | id | ) | const |
Get the child in the grid with a certain id.
id | The id of the child to be retrieved |
unsigned int ime::Grid2D::getColumnCount | ( | ) | const |
Vector2f ime::Grid2D::getPosition | ( | ) | const |
Get the position of the grid in pixels.
Grid2DRenderer & ime::Grid2D::getRenderer | ( | ) |
Get 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...
unsigned int ime::Grid2D::getRowCount | ( | ) | const |
Scene & ime::Grid2D::getScene | ( | ) |
Get the scene the grid belongs to.
Vector2u ime::Grid2D::getSize | ( | ) | const |
Get the size of the grid, in pixels.
Vector2u ime::Grid2D::getSizeInTiles | ( | ) | const |
Get the size of the grid, in tiles.
unsigned int ime::Grid2D::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::Grid2D::getTileOccupiedByChild | ( | const GridObject * | child | ) | const |
Get the tile occupied by a child of the grid.
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::Grid2D::getTileSize | ( | ) | const |
Get the size of each tile in the grid.
All the tiles have the same size
bool ime::Grid2D::hasChild | ( | const GridObject * | child | ) | const |
Check if the grid has a certain child or not.
child | Child to search for in the grid |
bool ime::Grid2D::isCollidable | ( | const Index & | index | ) | const |
Check if a tile is collidable or not.
index | Index of the tile to be checked |
bool ime::Grid2D::isIndexValid | ( | const Index & | index | ) | const |
Check if the index is within bounds of the grid or not.
index | Index to check |
bool ime::Grid2D::isTileOccupied | ( | const Index & | index | ) | const |
Check if a tile is occupied or not.
index | The position of the tile to be checked |
A tile is occupied if it has at least one child
This function returns false if index is out of bounds
bool ime::Grid2D::isTileOccupied | ( | const Tile & | tile | ) | const |
void ime::Grid2D::loadFromFile | ( | const std::string & | filename, |
const char & | separator = '\0' |
||
) |
Construct the grid 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 |
FileNotFoundException | If filename cannot be opened for reading |
InvalidParseException | If the contents of filename cannot be successfully parsed into Grid2D map data |
void ime::Grid2D::loadFromVector | ( | Map | map | ) |
Construct the grid form a vector that contains map data.
map | The vector to construct the map from |
void ime::Grid2D::removeAllChildren | ( | ) |
Remove all children from the grid.
bool ime::Grid2D::removeChild | ( | GridObject * | child | ) |
Remove a child from the grid.
child | Child to be removed |
void ime::Grid2D::removeChildIf | ( | const std::function< bool(GridObject *)> & | callback | ) |
Remove children from the grid using a condition.
callback | Function that determines if the condition is satisfied or not |
All children for which the callback returns true are removed from the grid
bool ime::Grid2D::removeChildWithId | ( | std::size_t | id | ) |
Remove a child with a certain id from the grid.
id | Id of the child to be removed |
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.
id | Identification of the tiles to exclude |
isCollidable | True to enable collision, otherwise false |
attachCollider | True to attach a Collider to the tile, otherwise false |
InvalidArgumentException | If 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
void ime::Grid2D::setCollidableById | ( | char | id, |
bool | isCollidable, | ||
bool | attachCollider = false |
||
) |
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 |
attachCollider | True to attach a Collider to the tile, otherwise false |
InvalidArgumentException | If 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
void ime::Grid2D::setCollidableByIndex | ( | const Index & | index, |
bool | isCollidable, | ||
bool | attachCollider = false |
||
) |
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 |
attachCollider | True to attach a Collider to the tile, otherwise false |
InvalidArgumentException | If 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
void ime::Grid2D::setCollidableByIndex | ( | const std::initializer_list< Index > & | locations, |
bool | isCollidable, | ||
bool | attachCollider = false |
||
) |
Enable or disable collision for tiles at certain locations.
locations | Locations (in tiles) of the tiles |
isCollidable | True to enable collision, otherwise false |
attachCollider | True to attach a Collider to the tile, otherwise false |
InvalidArgumentException | If 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
void ime::Grid2D::setCollidableByIndex | ( | Index | startPos, |
Index | endPos, | ||
bool | isCollidable, | ||
bool | attachCollider = false |
||
) |
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 |
attachCollider | True to attach a Collider to the tile, otherwise false |
InvalidArgumentException | If 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
void ime::Grid2D::setPosition | ( | int | x, |
int | y | ||
) |
Set the position of the grid.
x | X coordinate of the grid |
y | Y coordinate of the grid |
The position is (0, 0) by default