Class for performing grid based movement on an entity in a grid. More...
#include <GridMover.h>
Public Types | |
enum | Type { Type::Manual, Type::Random, Type::Target, Type::KeyboardControlled, Type::Custom } |
Types of grid movers. More... | |
enum | MoveRestriction { MoveRestriction::None, MoveRestriction::All, MoveRestriction::Vertical, MoveRestriction::Horizontal, MoveRestriction::Diagonal, MoveRestriction::NonDiagonal } |
Restricts the movement of the target along axes. More... | |
using | Ptr = std::shared_ptr< GridMover > |
Shared grid mover pointer. More... | |
Public Member Functions | |
GridMover (TileMap &tilemap, GameObject::Ptr gameObject=nullptr) | |
Create a manually controlled grid mover. More... | |
std::string | getClassType () const override |
Get the name of this class. More... | |
bool | requestDirectionChange (const Direction &newDir) |
Change the direction of the game object. More... | |
Direction | getDirection () const |
Get the current direction of the game object. More... | |
void | setTarget (GameObject::Ptr target) |
Change the controlled entity. More... | |
GameObject::Ptr | getTarget () const |
Get access to the controlled entity. More... | |
void | setMaxLinearSpeed (Vector2f speed) |
Set the maximum linear speed of the game object. More... | |
Vector2f | getMaxLinearSpeed () const |
Get the maximum speed of the game object. More... | |
void | setMovementRestriction (MoveRestriction moveRestriction) |
Restrict the movement of the game object to certain directions. More... | |
MoveRestriction | getMovementRestriction () const |
Get the current movement restriction of the game object. More... | |
virtual Index | getTargetTileIndex () const |
Get the index of the adjacent tile the target is trying to reach. More... | |
Type | getType () const |
Get the type of the grid mover. More... | |
TileMap & | getGrid () |
Get access to the grid in which the target is moved in. More... | |
bool | isTargetMoving () const |
Check if target is moving or not. More... | |
virtual void | update (Time deltaTime) |
Update entity movement in the grid. More... | |
void | teleportTargetToDestination () |
Force the target to reach it's destination. More... | |
int | onTargetChanged (Callback< GameObject::Ptr > callback) |
Add an event listener to a target change event. More... | |
int | onMoveBegin (Callback< Tile > callback) |
Add an event listener to a move begin event. More... | |
int | onAdjacentTileReached (Callback< Tile > callback) |
Add an event listener to an adjacent tile reached event. More... | |
int | onGridBorderCollision (Callback<> callback) |
Add an event listener to a tilemap border collision event. More... | |
int | onSolidTileCollision (Callback< Tile > callback) |
Add an event listener to a tile collision event. More... | |
int | onObstacleCollision (Callback< GameObject::Ptr, GameObject::Ptr > callback) |
Add an event listener to an obstacle collision. More... | |
int | onCollectableCollision (Callback< GameObject::Ptr, GameObject::Ptr > callback) |
Add an event listener to a collectable collision event. More... | |
int | onEnemyCollision (Callback< GameObject::Ptr, GameObject::Ptr > callback) |
Add an event listener to an enemy collision event. More... | |
int | onPlayerCollision (Callback< GameObject::Ptr, GameObject::Ptr > callback) |
Add an event listener to a player collision event. More... | |
bool | removeCollisionHandler (int id) |
Remove a collision handler. More... | |
bool | removeEventListener (const std::string &event, int id) |
Remove an event listener from an event. More... | |
void | resetTargetTile () |
void | onTargetTileReset (Callback< Tile > callback) |
~GridMover () override | |
Destructor. More... | |
void | setTag (const std::string &tag) |
Assign the object an alias. More... | |
const std::string & | getTag () const |
Get the alias of the object. More... | |
unsigned int | getObjectId () const |
Get the id of the object. More... | |
virtual std::string | getClassName () const =0 |
Get the name of the concrete class. More... | |
int | onPropertyChange (const std::string &property, const Callback< Property > &callback) |
Add an event listener to a specific property change event. More... | |
void | onPropertyChange (const Callback< Property > &callback) |
Add an event listener to a property change event. More... | |
int | onEvent (const std::string &event, const Callback<> &callback) |
Add an event listener to an event. More... | |
bool | unsubscribe (const std::string &event, int id) |
Remove an event listener from an event. More... | |
int | onDestruction (const Callback<> &callback) |
Add a destruction listener. More... | |
bool | removeDestructionListener (int id) |
Remove a destruction listener form the object. More... | |
bool | operator== (const Object &rhs) const |
Check if two objects are the same object or not. More... | |
bool | operator!= (const Object &rhs) const |
Check if two objects are not the same object. More... | |
Protected Member Functions | |
GridMover (Type type, TileMap &tileMap, GameObject::Ptr target) | |
Create a grid mover. More... | |
void | emitChange (const Property &property) |
Dispatch a property change event. More... | |
void | emit (const std::string &event) |
Dispatch an action event. More... | |
Class for performing grid based movement on an entity in a grid.
This class monitors the movement of an entity in a grid and ensures that it always moves from one cell to the next and never between grid cells. The entities direction cannot be changed until it has completed it's current movement.
Note that the grid mover only supports orthogonal movement (left, right, up and down)
Definition at line 59 of file GridMover.h.
using ime::GridMover::Ptr = std::shared_ptr<GridMover> |
Shared grid mover pointer.
Definition at line 61 of file GridMover.h.
|
strong |
Restricts the movement of the target along axes.
Definition at line 77 of file GridMover.h.
|
strong |
Types of grid movers.
Definition at line 66 of file GridMover.h.
|
explicit |
Create a manually controlled grid mover.
tilemap | The grid the game object is in |
gameObject | The game object to be controlled by the grid mover |
|
override |
Destructor.
|
protected |
Create a grid mover.
type | The type of the grid mover |
tileMap | Grid to move a target entity in |
target | GameObject to be moved in the grid |
Note that this constructor is intended to be used by derived classes such that the user cannot change the type of the grid mover during instantiation. The public constructor sets the type to Type::Manual and it cannot be changed once set. Since derived classes must set their own type, they use this constructor to initialize the base class
|
protectedinherited |
Dispatch an action event.
event | The name of the event to be dispatched |
This function will invoke all event listeners of the specified event. The function should be used for events that represent an action, rather than those that represent a property change (Use emitChange for that)
|
protectedinherited |
Dispatch a property change event.
property | The property that changed |
This function will invoke all the event listeners of the specified property
|
pure virtualinherited |
Get the name of the concrete class.
This function is implemented by all internal classes that inherit from this class.
Implemented in ime::Tile, ime::SpriteSheet, ime::Sprite, ime::RectangleShape, ime::ConvexShape, ime::CircleShape, ime::Camera, ime::Scene, ime::RenderLayerContainer, ime::RenderLayer, ime::TargetGridMover, ime::RandomGridMover, ime::KeyboardGridMover, ime::DistanceJoint, ime::PolygonCollider, ime::EdgeCollider, ime::CircleCollider, ime::BoxCollider, ime::Body, ime::AABB, and ime::GameObject.
|
overridevirtual |
Get the name of this class.
Note that this function is only implemented by child classes of Object which also serve as a base class for other classes
Reimplemented from ime::Object.
Direction ime::GridMover::getDirection | ( | ) | const |
Get the current direction of the game object.
TileMap& ime::GridMover::getGrid | ( | ) |
Get access to the grid in which the target is moved in.
Vector2f ime::GridMover::getMaxLinearSpeed | ( | ) | const |
Get the maximum speed of the game object.
MoveRestriction ime::GridMover::getMovementRestriction | ( | ) | const |
Get the current movement restriction of the game object.
|
inherited |
Get the id of the object.
Each object has a unique id
|
inherited |
GameObject::Ptr ime::GridMover::getTarget | ( | ) | const |
Get access to the controlled entity.
|
virtual |
Get the index of the adjacent tile the target is trying to reach.
If the target it not moving towards any tiles, this function will return the index of the tile currently occupied by the target
Reimplemented in ime::RandomGridMover.
Type ime::GridMover::getType | ( | ) | const |
Get the type of the grid mover.
bool ime::GridMover::isTargetMoving | ( | ) | const |
Check if target is moving or not.
Add an event listener to an adjacent tile reached event.
callback | Function to execute when the target reaches its target tile |
This event is emitted when the target moves from its current tile to any of its adjacent tiles.
The callback is passed the tile the target moved to
int ime::GridMover::onCollectableCollision | ( | Callback< GameObject::Ptr, GameObject::Ptr > | callback | ) |
Add an event listener to a collectable collision event.
callback | Function to execute when the collision takes place |
This event is emitted when the target collides with a collectable in the grid. The callback is passed the target as the first argument and the collectable it collided with as the second argument
|
inherited |
Add a destruction listener.
callback | Function to be executed when the object is destroyed |
Note that an object may have more than one destruction listeners, however, you have to keep the returned id if you may want to remove the callback at a later time
int ime::GridMover::onEnemyCollision | ( | Callback< GameObject::Ptr, GameObject::Ptr > | callback | ) |
Add an event listener to an enemy collision event.
callback | Function to execute when the collision takes place |
This event is emitted when the target collides with an enemy in the grid. The callback is passed the target as the first argument and the enemy it collided with as the second argument
|
inherited |
Add an event listener to an event.
event | The name of the event to add an an event listener to |
callback | The function to be executed when the event takes place |
Unlike onPropertyChange, this function registers event listeners to events that occur when something happens to the object, or when the object does something (action events). Usually the name of the event/action is the name of the function:
int ime::GridMover::onGridBorderCollision | ( | Callback<> | callback | ) |
Add an event listener to a tilemap border collision event.
callback | Function to execute when the collision takes place |
This event is emitted when the target tries to go beyond the bounds of the grid. By default the event is handled internally before it's emitted to the outside. The internal handler prevents the target from leaving the grid. That is, the target will occupy the same tile it occupied before the collision. This behaviour is not removable, however, it may be overridden since the internal handler is called first before alerting external handlers
Add an event listener to a move begin event.
callback | The function to be executed when the game object starts moving |
This event is emitted when the game object starts moving from its current tile to one of its adjacent tile. The callback is passed the tile that the game object is currently moving to
int ime::GridMover::onObstacleCollision | ( | Callback< GameObject::Ptr, GameObject::Ptr > | callback | ) |
Add an event listener to an obstacle collision.
callback | Function to execute when the collision takes place |
This event is emitted when the target collides with an obstacle in the grid. By default the event is handled internally before its emitted to the outside. The internal handler prevents the target from occupying the same tile as the obstacle by moving it back to its previous tile after the collision
The callback is passed the target as the first argument and the obstacle it collided with as the second argument
int ime::GridMover::onPlayerCollision | ( | Callback< GameObject::Ptr, GameObject::Ptr > | callback | ) |
Add an event listener to a player collision event.
callback | Function to execute when the collision takes place |
This event is emitted when the target collides with a player in the grid. The callback is passed the target as the first argument and the player it collided with as the second argument
Add an event listener to a property change event.
callback | The function to be executed when the property changes |
Note that only one callback function may be registered with this function. This means that adding a new event listener overwrites the previous event listener. To remove the callback, pass a nullptr as an argument. The function may be useful if you want to write the logic for property changes in one function.
|
inherited |
Add an event listener to a specific property change event.
property | The name of the property to listen for |
callback | The function to be executed when the property changes |
A property change event is triggered by any function that begins with set, where the the text after set is the name of the property. For example, for the setTag function, the property that the function modifies is Tag.
Note that when adding a property change event listener, the name of the property must be in lowercase:
Unlike onPropertyChange(const Callback&) you can add multiple event listeners to the same property using this function. However you must store the unique id of the event listener if you wish to remove it at a later time
Add an event listener to a tile collision event.
callback | Function to execute when the collision takes place |
This event is emitted when the target collides with a solid tile in the grid (Solid tiles are always collidable). By default, the event is handled internally before its emitted to the outside. The internal handler prevents the target from occupying the solid tile by moving it back to its previous tile after the collision
The callback is passed the tile the target collided with
int ime::GridMover::onTargetChanged | ( | Callback< GameObject::Ptr > | callback | ) |
Add an event listener to a target change event.
callback | Function to execute when the target changes |
|
inherited |
Check if two objects are not the same object.
rhs | Object to compare against this object |
Two objects are different from each other if they have different object id's
|
inherited |
Check if two objects are the same object or not.
rhs | Object to compare against this object |
Two objects are the same object if they have the same object id. Recall that each object instance has a unique id
bool ime::GridMover::removeCollisionHandler | ( | int | id | ) |
Remove a collision handler.
id | Identification number of the handler |
The identification number is the number returned when an event listener was added to a collision event
|
inherited |
Remove a destruction listener form the object.
The | id of the destruction listener to be removed |
bool ime::GridMover::removeEventListener | ( | const std::string & | event, |
int | id | ||
) |
Remove an event listener from an event.
event | Name of the event to remove event listener from |
id | Identification number of the event listener |
The identification number is the number returned when an event listener was added to an events
bool ime::GridMover::requestDirectionChange | ( | const Direction & | newDir | ) |
Change the direction of the game object.
newDir | The new direction of the game object |
Note that the direction of the game object cannot be changed while it is moving to another tile. This function only works with predefined directions. In addition to returning true for successful direction change, the function will emit a "direction" property change event. Usually property change events are only emitted by setters (functions that begin with a "set" prefix)
void ime::GridMover::setMaxLinearSpeed | ( | Vector2f | speed | ) |
Set the maximum linear speed of the game object.
speed | The new maximum speed |
If the game object is currently moving, the speed will be set after it reaches its current target tile
void ime::GridMover::setMovementRestriction | ( | MoveRestriction | moveRestriction | ) |
Restrict the movement of the game object to certain directions.
moveRestriction | Permitted direction of travel |
By default the movement restrictions is MovementRestriction::None
|
inherited |
Assign the object an alias.
name | The alias of the object |
This function is useful if you want to refer to the object by tag instead of its id. Unlike an object id, multiple objects may have the same tag.
By default, the tag is an empty string
void ime::GridMover::setTarget | ( | GameObject::Ptr | target | ) |
Change the controlled entity.
target | New target |
Provide nullptr as argument to remove current target
void ime::GridMover::teleportTargetToDestination | ( | ) |
Force the target to reach it's destination.
The destination in this context is always the adjacent tile the target is headed towards. This function has no effect if the target is not moving towards any tile
|
inherited |
Remove an event listener from an event.
event | The name of the event to remove event listener from |
id | The unique id of the event listener to be removed |
|
virtual |
Update entity movement in the grid.
deltaTime | Time passed since movement was last updated |
The target can only move one tile at a time and cannot be instructed to move to another tile while it is currently moving to one of its adjacent tiles. After it reaching it's target tile it stops moving until instructed to move again. Therefore if the target is to be moved multiple tiles, the request to change direction must be made immediately after the target reaches its destination
Reimplemented in ime::RandomGridMover.