Moves a GridObject to a specific position in the Grid2D. More...
#include <TargetGridMover.h>
Public Types | |
using | Ptr = std::unique_ptr< TargetGridMover > |
Unique grid mover pointer. More... | |
enum class | Type { Manual , Random , Target , Cyclic , KeyboardControlled , Custom } |
Types of grid movers. More... | |
enum class | MoveRestriction { None , All , Vertical , Horizontal , Diagonal , NonDiagonal } |
Restricts the movement of the target along axes. More... | |
Public Member Functions | |
TargetGridMover (Grid2D &grid, GridObject *target=nullptr) | |
Constructor. 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... | |
std::string | getPathFinderType () const |
Get the type of path finder used. More... | |
void | setDestination (const Index &index) |
Set the index of the tile the target should go to. More... | |
void | setDestination (const Vector2f &position) |
Set the position the target should go to. More... | |
Index | getDestination () const |
Get the destination position of the target. More... | |
void | resetDestination () |
Reset the destination tile to a tile outside the bounds of the grid. More... | |
const std::stack< Index > & | getPath () const |
Get the path of the target. More... | |
void | clearPath () |
Clear the current path of the target. More... | |
bool | isDestinationReachable (const Index &index) const |
Check whether or not a destination is reachable. More... | |
bool | isDestinationReachable (const Vector2f &position) const |
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 | setAdaptiveMoveEnable (bool enable) |
Enable or disable adaptive movement. More... | |
bool | isAdaptiveMoveEnabled () const |
Check if adaptive movement is enabled or not. More... | |
int | onDestinationReached (Callback< Index > callback) |
Add an event listener to a destination reached event. More... | |
void | setPathViewEnable (bool showPath) |
Set whether or not the targets path should be shown or not. More... | |
bool | isPathViewEnabled () const |
Check if the targets path is shown or not. More... | |
void | onPathGenFinish (const Callback< const std::stack< Index > & > &callback) |
Add an event listener to a path generation finish event. More... | |
void | renderPath (priv::RenderTarget &window) const |
~TargetGridMover () override | |
Destructor. More... | |
std::string | getClassType () const override |
Get the name of this class. More... | |
void | syncWith (const GridMover &other) |
Sync this grid mover with another grid mover. More... | |
bool | requestMove (const Direction &dir) |
Request a move in a given direction. More... | |
std::pair< bool, GridObject * > | isBlockedInDirection (const Direction &direction) const |
Check if the target is blocked from moving in a direction. More... | |
Direction | getDirection () const |
Get the current direction of the game object. More... | |
Direction | getPrevDirection () const |
Get the previous direction of the target. More... | |
void | setTarget (GridObject *target) |
Change the controlled entity. More... | |
GridObject * | getTarget () const |
Get access to the controlled entity. More... | |
void | setSpeed (const Vector2f &speed) |
Set the speed of the game object. More... | |
const Vector2f & | getSpeed () const |
Get the speed of the target. More... | |
void | setSpeedMultiplier (float multiplier) |
Set a speed multiplier. More... | |
float | getSpeedMultiplier () const |
Get the speed multiplier. 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... | |
void | setMovementFreeze (bool freeze) |
Freeze or unfreeze the targets movement. More... | |
bool | isMovementFrozen () const |
Check if the targets movement is frozen or not. More... | |
Index | getCurrentTileIndex () const |
Get the index of the tile currently occupied by the target. More... | |
Index | getPrevTileIndex () const |
Get the index of the tile previously occupied by the target. More... | |
Type | getType () const |
Get the type of the grid mover. More... | |
Grid2D & | getGrid () |
Get access to the grid in which the target is moved in. More... | |
const Grid2D & | getGrid () const |
bool | isTargetMoving () const |
Check if target is moving or not. More... | |
void | teleportTargetToDestination () |
Force the target to reach it's destination. More... | |
int | onDirectionChange (const Callback< Direction > &callback, bool oneTime=false) |
Add an event listener to a target direction change. More... | |
void | resetTargetTile () |
Reset the target tile to be the same as the entity tile. More... | |
int | onTargetTileReset (const Callback< Index > &callback, bool oneTime=false) |
Add an event listener to target tile reset event. More... | |
virtual void | update (Time deltaTime) |
int | onMoveBegin (const Callback< Index > &callback, bool oneTime=false) |
Add an event listener to a move begin event. More... | |
int | onMoveEnd (const Callback< Index > &callback, bool oneTime=false) |
Add an event listener to an adjacent tile reached event. More... | |
int | onObjectCollision (const Callback< GridObject *, GridObject * > &callback, bool oneTime=false) |
Add an event listener to a grid collision event event. More... | |
int | onBorderCollision (const Callback<> &callback, bool oneTime=false) |
Add an event listener to a grid border collision event. More... | |
int | onTileCollision (const Callback< Index > &callback, bool oneTime=false) |
Add an event listener to a tile collision event. More... | |
void | setTag (const std::string &tag) |
Assign the object an alias. More... | |
const std::string & | getTag () const |
Get the tag assigned to the object. More... | |
unsigned int | getObjectId () const |
Get the unique id of the object. More... | |
int | onPropertyChange (const std::string &property, const Callback< Property > &callback, bool oneTime=false) |
Add an event listener to a specific property change event. More... | |
int | onPropertyChange (const Callback< Property > &callback, bool oneTime=false) |
Add an event listener to any property change event. More... | |
void | suspendedEventListener (int id, bool suspend) |
Pause or resume execution of an event listener. More... | |
bool | isEventListenerSuspended (int id) const |
Check if an event listener is suspended or not. More... | |
bool | removeEventListener (const std::string &event, int id) |
Remove an event listener from an event. More... | |
bool | removeEventListener (int id) |
Remove an event listener. More... | |
int | onDestruction (const Callback<> &callback) |
Add a destruction listener. More... | |
bool | isSameObjectAs (const Object &other) const |
Check if another object is the same instance as this object. More... | |
Static Public Member Functions | |
static TargetGridMover::Ptr | create (Grid2D &grid, GridObject *target=nullptr) |
Create a TargetGridMover. More... | |
Protected Member Functions | |
void | emitChange (const Property &property) |
Dispatch a property change event. More... | |
void | emitDestruction () |
Emit a destruction event. More... | |
Protected Attributes | |
EventEmitter | eventEmitter_ |
Event dispatcher. More... | |
Moves a GridObject to a specific position in the Grid2D.
Definition at line 41 of file TargetGridMover.h.
using ime::TargetGridMover::Ptr = std::unique_ptr<TargetGridMover> |
Unique grid mover pointer.
Definition at line 43 of file TargetGridMover.h.
|
stronginherited |
Restricts the movement of the target along axes.
Definition at line 75 of file GridMover.h.
|
stronginherited |
Types of grid movers.
Definition at line 63 of file GridMover.h.
|
explicit |
Constructor.
grid | Grid to move target in |
target | The game object to be moved in the grid |
|
override |
Destructor.
void ime::TargetGridMover::clearPath | ( | ) |
Clear the current path of the target.
If the target is currently en route on this path, it will stop when it gets to its currently targeted adjacent tile. In addition, if the targets movement was stopped while it was moving to an adjacent tile and is resumed after the path is cleared the target will resume the move to an adjacent tile and stop thereafter
|
static |
Create a TargetGridMover.
grid | The grid the target will be moved in |
target | The game object to be moved in the grid |
|
protectedinherited |
Dispatch a property change event.
property | The property that changed |
This function will invoke all the event listeners of the specified property
|
protectedinherited |
Emit a destruction event.
|
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.
|
inherited |
Get the index of the tile currently occupied by the target.
Recall that when moved, the target occupies a tile ahead of time (see requestMove())
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 current movement restriction of the game object.
|
inherited |
Get the unique id of the object.
Note that each instance of ime::Object has a unique id
const std::stack< Index > & ime::TargetGridMover::getPath | ( | ) | const |
Get the path of the target.
std::string ime::TargetGridMover::getPathFinderType | ( | ) | const |
Get the type of path finder used.
|
inherited |
Get the previous direction of the target.
|
inherited |
Get the index of the tile previously occupied by the target.
If the target never moved, then this function will return the tile currently occupied by the target
|
inherited |
|
inherited |
|
inherited |
|
inherited |
Get access to the controlled entity.
|
inherited |
Get the type of the grid mover.
bool ime::TargetGridMover::isAdaptiveMoveEnabled | ( | ) | const |
Check if adaptive movement is enabled or not.
|
inherited |
Check if the target is blocked from moving in a direction.
direction | The direction to be checked |
This function will return true if the target is blocked by a collidable tile or an obstacle (see ime::GridObject::setObstacle), or if a move in the given direction will place it outside the bounds of the grid
bool ime::TargetGridMover::isDestinationReachable | ( | const Index & | index | ) | const |
Check whether or not a destination is reachable.
index | Destination to be checked (in tiles) |
bool ime::TargetGridMover::isDestinationReachable | ( | const Vector2f & | position | ) | const |
Check whether or not a destination is reachable.
position | The destination in (in pixels) to be checked |
|
inherited |
Check if an event listener is suspended or not.
id | The identification number of the listener to be checked |
This function also returns false if the specified event listener does not exist
|
inherited |
Check if the targets movement is frozen or not.
bool ime::TargetGridMover::isPathViewEnabled | ( | ) | const |
Check if the targets path is shown or not.
|
inherited |
Check if another object is the same instance as this object.
other | The object to compare against this object |
|
inherited |
Check if target is moving or not.
|
inherited |
Add an event listener to a grid border collision event.
callback | Function to execute when the collision takes place |
oneTime | True to execute the callback one-time or false to execute it every time the event is triggered |
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 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 |
The destruction listener is called when the object reaches the end of its lifetime. Note that an object may have multiple destruction listeners registered to it
|
inherited |
Add an event listener to a target direction change.
callback | The function to be executed when target direction changes |
oneTime | True to execute the callback one-time or false to execute it every time the event is triggered |
The direction change event is triggered by a move request
|
inherited |
Add an event listener to a move begin event.
callback | The function to be executed when the game object starts moving |
oneTime | True to execute the callback one-time or false to execute it every time the event is triggered |
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 index of the tile that the game object is currently moving to
|
inherited |
Add an event listener to an adjacent tile reached event.
callback | Function to execute when the target reaches its target tile |
oneTime | True to execute the callback one-time or false to execute it every time the event is triggered |
This event is emitted when the target moves from its current tile to any of its adjacent tiles.
The callback is passed the index of the tile the target moved to
|
inherited |
Add an event listener to a grid collision event event.
callback | The function to be executed when the target collides with another game object |
oneTime | True to execute the callback one-time or false to execute it every time the event is triggered |
void ime::TargetGridMover::onPathGenFinish | ( | const Callback< const std::stack< Index > & > & | callback | ) |
Add an event listener to a path generation finish event.
callback | The callback to be executed when the event is raised |
The path generation event is triggered when the targets destination is set. If the target is currently not moving, the event will be triggered immediately. However, if the target is moving, the event will be triggered the next time the path is generated
On invocation the callback is passed the generated path (which may be empty - see setDestination()). Note that, only one event listener may be registered to this event at a time. To remove the event listener, pass nullptr as the argument
|
inherited |
Add an event listener to any property change event.
callback | The function to be executed when any property changes |
oneTime | True to execute the callback one-time or false to execute it every time the event is triggered |
When unsubscribing the any property change event handler, you must pass "propertyChange" as the name of the event
|
inherited |
Add an event listener to a specific property change event.
property | The name of the property to add an event listener to |
oneTime | True to execute the callback one-time or false to execute it every time the event is triggered |
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, the setTag() function, modifies the tag property of the object, thus will generate a "tag" change event each time it is called
Note that multiple event listeners may be registered to the same property change event. In addition, when adding a property change event listener, the name of the property must be in lowercase.
|
inherited |
Add an event listener to target tile reset event.
callback | Function to execute when the target tile is reset |
oneTime | True to execute the callback one-time or false to execute it every time the event is triggered |
|
inherited |
Add an event listener to a tile collision event.
callback | Function to execute when the collision takes place |
oneTime | True to execute the callback one-time or false to execute it every time the event is triggered |
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 index of the tile the target collided with
|
inherited |
Remove an event listener from an event.
event | The name of the event to remove an event listener from |
id | The unique id of the event listener to be removed |
|
inherited |
Remove an event listener.
id | The id of the event listener to be removed |
|
inherited |
Request a move in a given direction.
dir | The direction to move in |
The target can only move one tile at a time, as a result it cannot be requested to move to a tile while it is currently moving towards another tile (see isTargetMoving()). In addition, If a move in the specified direction is possible, the adjacent tile in the specified direction will be flagged as occupied by the target before it moves to it
void ime::TargetGridMover::resetDestination | ( | ) |
Reset the destination tile to a tile outside the bounds of the grid.
|
inherited |
Reset the target tile to be the same as the entity tile.
void ime::TargetGridMover::setAdaptiveMoveEnable | ( | bool | enable | ) |
Enable or disable adaptive movement.
enable | True to enable, otherwise false |
This function affects how the target behaves when its current path to a destination tile is blocked by a solid tile or an obstacle.
When smart move is enabled, the target immediately attempts to find another path to its destination, in other words it can see ahead. When smart move is disabled the target will continue to move along the blocked path until it gets to the roadblock. Only then will it attempt to find another path
Note that if the grid is static, its advised to keep adaptive move disabled for performance reasons
By default, adaptive movement is disabled
void ime::TargetGridMover::setDestination | ( | const Index & | index | ) |
Set the index of the tile the target should go to.
index | The targets new destination (in tiles) |
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 | ( | const Vector2f & | position | ) |
Set the position the target should go to.
position | New target position (in pixels) |
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 |
Freeze or unfreeze the targets movement.
freeze | True to freeze movement or false to unfreeze |
This function achieves the same thing as setMovementRestriction(). The difference is when the function is called while the target is moving. setMovementRestriction(true) will prevent the target from moving further after the targets gets to its targeted tile whilst setMovementFreeze(true) will immediately prevent the target from moving further. This means that the target can temporarily be in between grid tiles
By default, the targets movement is not frozen
|
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 | ) |
void ime::TargetGridMover::setPathViewEnable | ( | bool | showPath | ) |
Set whether or not the targets path should be shown or not.
showPath | True to show path, otherwise false |
By default, the path is not shown
|
inherited |
Set the speed of the game object.
speed | The new speed |
If the game object is currently moving, the speed will be set after it reaches its current target tile
Note that for a ime::MoveRestriction::Diagonal or ime::MoveRestriction::None, the speed must be the same for both the x and y axis, otherwise undefined behaviour
By default, the speed is ime::Vector2f{60, 60}
|
inherited |
Set a speed multiplier.
multiplier | The new speed multiplier |
A speed multiplier increases or decreases the speed of the target without affecting its default speed. For example, A multiplier of 2.0f makes the target move twice as fast, a multiplier of 0.5f makes the target move at half its normal speed and a multiplier of 0.0f stops the target from moving. Note that a negative multiplier will be ignored
By default, the multiplier is 1.0f (normal)
|
inherited |
Assign the object an alias.
tag | The alias of the object |
This function is useful if you want to refer to the object by a tag instead of its object id. Unlike an object id, multiple objects may have the same tag
By default, the tag is an empty string
|
inherited |
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 |
Pause or resume execution of an event listener.
id | The event listeners unique identification number |
suspend | True to suspend/pause or false to unsuspend/resume |
|
inherited |
Sync this grid mover with another grid mover.
other | The grid mover to sync with this grid mover with |
In situations where you want to change a targets grid mover, you'll have to synchronize the new grid mover with the current grid mover to avoid misalignment with the grid. When misaligned, the target is no longer confined to the grid and moves indefinitely in its current direction. Note that misalignment does not occur when the target is not moving, i.e when isMoving() returns false.
|
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
|
protectedinherited |