Class for performing grid based movement on an entity in a grid. More...
#include <GridMover.h>
Public Types | |
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... | |
using | Ptr = std::unique_ptr< GridMover > |
Unique grid mover pointer. More... | |
Public Member Functions | |
GridMover (Grid2D &grid, GridObject *gameObject=nullptr) | |
Create a manually controlled grid mover. More... | |
GridMover (const GridMover &)=delete | |
Copy constructor. More... | |
GridMover & | operator= (const GridMover)=delete |
Copy assignment operator. More... | |
std::string | getClassType () const override |
Get the name of this class. More... | |
std::string | getClassName () 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... | |
~GridMover () override | |
Destructor. 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 GridMover::Ptr | create (Grid2D &grid, GridObject *gameObject=nullptr) |
Create the grid mover. More... | |
Protected Member Functions | |
GridMover (Type type, Grid2D &grid, GridObject *target) | |
Create a grid mover. More... | |
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... | |
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.
Definition at line 56 of file GridMover.h.
using ime::GridMover::Ptr = std::unique_ptr<GridMover> |
Unique grid mover pointer.
Definition at line 58 of file GridMover.h.
|
strong |
Restricts the movement of the target along axes.
Definition at line 75 of file GridMover.h.
|
strong |
Types of grid movers.
Definition at line 63 of file GridMover.h.
|
explicit |
Create a manually controlled grid mover.
grid | The grid the game object is in |
gameObject | The game object to be controlled by the grid mover |
|
delete |
Copy constructor.
|
override |
Destructor.
|
protected |
Create a grid mover.
type | The type of the grid mover |
grid | Grid to move a target entity in |
target | GridObject 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
|
static |
Create the grid mover.
grid | The grid the game object is in |
gameObject | The game object to be controlled by the grid mover |
|
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 |
Get the name of this class.
Implements ime::Object.
Reimplemented in ime::KeyboardGridMover, ime::RandomGridMover, and ime::TargetGridMover.
|
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.
Index ime::GridMover::getCurrentTileIndex | ( | ) | const |
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())
Direction ime::GridMover::getDirection | ( | ) | const |
Get the current direction of the game object.
Grid2D & ime::GridMover::getGrid | ( | ) |
Get access to the grid in which the target is moved in.
MoveRestriction ime::GridMover::getMovementRestriction | ( | ) | const |
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
Direction ime::GridMover::getPrevDirection | ( | ) | const |
Get the previous direction of the target.
Index ime::GridMover::getPrevTileIndex | ( | ) | const |
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
const Vector2f & ime::GridMover::getSpeed | ( | ) | const |
float ime::GridMover::getSpeedMultiplier | ( | ) | const |
|
inherited |
GridObject * ime::GridMover::getTarget | ( | ) | const |
Get access to the controlled entity.
Type ime::GridMover::getType | ( | ) | const |
Get the type of the grid mover.
std::pair< bool, GridObject * > ime::GridMover::isBlockedInDirection | ( | const Direction & | direction | ) | const |
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
|
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
bool ime::GridMover::isMovementFrozen | ( | ) | const |
Check if the targets movement is frozen or not.
|
inherited |
Check if another object is the same instance as this object.
other | The object to compare against this object |
bool ime::GridMover::isTargetMoving | ( | ) | const |
Check if target is moving or not.
int ime::GridMover::onBorderCollision | ( | const Callback<> & | callback, |
bool | oneTime = false |
||
) |
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
|
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
int ime::GridMover::onDirectionChange | ( | const Callback< Direction > & | callback, |
bool | oneTime = false |
||
) |
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
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
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
int ime::GridMover::onObjectCollision | ( | const Callback< GridObject *, GridObject * > & | callback, |
bool | oneTime = false |
||
) |
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 |
|
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.
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 |
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
|
delete |
Copy assignment operator.
|
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 |
bool ime::GridMover::requestMove | ( | const Direction & | dir | ) |
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::GridMover::resetTargetTile | ( | ) |
Reset the target tile to be the same as the entity tile.
void ime::GridMover::setMovementFreeze | ( | bool | freeze | ) |
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
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
void ime::GridMover::setSpeed | ( | const Vector2f & | speed | ) |
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}
void ime::GridMover::setSpeedMultiplier | ( | float | multiplier | ) |
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
void ime::GridMover::setTarget | ( | GridObject * | target | ) |
|
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 |
void ime::GridMover::syncWith | ( | const GridMover & | other | ) |
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.
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
|
protectedinherited |