Class for moving a game object to a specific position in the grid. More...
#include <TargetGridMover.h>
Public Types | |
using | Ptr = std::shared_ptr< TargetGridMover > |
Shared grid mover pointer. More... | |
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... | |
Public Member Functions | |
TargetGridMover (TileMap &tileMap, GameObject::Ptr target=nullptr) | |
Create a random grid mover object. More... | |
std::string | getClassName () const override |
Get the name of this class. More... | |
void | setPathFinder (std::unique_ptr< IPathFinderStrategy > pathFinder) |
Set the path finder. More... | |
void | setDestination (Index index) |
Set the index of the tile the target should go to. More... | |
void | setDestination (Vector2f position) |
Set the position the target should go to. More... | |
Index | getDestination () const |
Get the destination position of the target. More... | |
const std::stack< Index > & | getPath () const |
Get the path of the target. More... | |
bool | isDestinationReachable (Index index) |
Check whether or not a destination is reachable. More... | |
void | startMovement () |
Start moving the target to its destination tile. More... | |
void | stopMovement () |
Stop the targets movement. More... | |
void | enableAdaptiveMovement (bool isAdaptive) |
Adaptively avoid solid tiles and obstacles. More... | |
int | onDestinationReached (Callback< Tile > callback) |
Add an event listener to a destination reached event. More... | |
~TargetGridMover () override | |
Destructor. 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) |
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... | |
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 | |
void | emitChange (const Property &property) |
Dispatch a property change event. More... | |
void | emit (const std::string &event) |
Dispatch an action event. More... | |
Class for moving a game object to a specific position in the grid.
Definition at line 35 of file TargetGridMover.h.
using ime::TargetGridMover::Ptr = std::shared_ptr<TargetGridMover> |
Shared grid mover pointer.
Definition at line 37 of file TargetGridMover.h.
|
stronginherited |
Restricts the movement of the target along axes.
Definition at line 77 of file GridMover.h.
|
stronginherited |
Types of grid movers.
Definition at line 66 of file GridMover.h.
|
explicit |
Create a random grid mover object.
tileMap | Grid to move target in |
target | GameObject to be moved in the grid |
|
override |
Destructor.
|
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
void ime::TargetGridMover::enableAdaptiveMovement | ( | bool | isAdaptive | ) |
Adaptively avoid solid tiles and obstacles.
isAdaptive | True to enable, otherwise false |
When enabled, the target will adaptively avoid collisions with solid tiles and obstacles. The targets path is updated every time it moves from one tile to the next. This makes the target aware of tile state changes as they happen
When disabled, the target will continue moving in its current path to the destination until it either collides with something or reaches the destination. The targets path is updated each time the destination changes. Therefore if it remains fixed the target will not know of any tile state changes until it gets to that tile
Adaptive movement is disabled by default
|
overridevirtual |
|
overridevirtualinherited |
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.
Index ime::TargetGridMover::getDestination | ( | ) | const |
Get the destination position of the target.
This destination will be returned even if the target has reached it
|
inherited |
Get the current direction of the game object.
|
inherited |
Get access to the grid in which the target is moved in.
|
inherited |
Get the maximum speed of the game object.
|
inherited |
Get the current movement restriction of the game object.
|
inherited |
Get the id of the object.
Each object has a unique id
const std::stack<Index>& ime::TargetGridMover::getPath | ( | ) | const |
Get the path of the target.
|
inherited |
|
inherited |
Get access to the controlled entity.
|
virtualinherited |
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.
|
inherited |
Get the type of the grid mover.
bool ime::TargetGridMover::isDestinationReachable | ( | Index | index | ) |
Check whether or not a destination is reachable.
index | Destination to be checked |
|
inherited |
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
|
inherited |
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
Add an event listener to a destination reached event.
callback | Function to execute when the target reaches its destination tile |
This event is fired when the target reaches the final target tile
The callback is passed the destination tile of the target after it reaches it
|
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
|
inherited |
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:
|
inherited |
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
|
inherited |
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
|
inherited |
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
|
inherited |
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
|
inherited |
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 |
|
inherited |
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
|
inherited |
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::TargetGridMover::setDestination | ( | Index | index | ) |
Set the index of the tile the target should go to.
index | The targets new destination |
The specified index must be within the the bounds of the grid and the tile at index must be reachable from the targets current tile, otherwise, the target will not move, since it cannot establish a path to the destination
void ime::TargetGridMover::setDestination | ( | Vector2f | position | ) |
Set the position the target should go to.
position | New target position |
The specified position must be within the grid and the tile at that position must be reachable from the targets current tile, otherwise the target will not move since it cannot establish a path to the destination
|
inherited |
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
|
inherited |
Restrict the movement of the game object to certain directions.
moveRestriction | Permitted direction of travel |
By default the movement restrictions is MovementRestriction::None
void ime::TargetGridMover::setPathFinder | ( | std::unique_ptr< IPathFinderStrategy > | pathFinder | ) |
Set the path finder.
pathFinder | New path finder |
The default path finder is Breadth First Search
|
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
|
inherited |
Change the controlled entity.
target | New target |
Provide nullptr as argument to remove current target
void ime::TargetGridMover::startMovement | ( | ) |
Start moving the target to its destination tile.
This function has no effect if the targets movement is not stopped
void ime::TargetGridMover::stopMovement | ( | ) |
Stop the targets movement.
Since a child in the grid can never be in between tiles, the targets movement will be stopped after it completes its current move. The targets movement is stopped by default
|
inherited |
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 |
|
virtualinherited |
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.