Moves a GridObject randomly in a Grid2D. More...
#include <RandomGridMover.h>
Public Types | |
using | Ptr = std::unique_ptr< RandomGridMover > |
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 | |
RandomGridMover (Grid2D &grid, GridObject *target=nullptr) | |
Constructor. More... | |
std::string | getClassName () const override |
Get the name of this class. More... | |
void | startMovement () |
Start moving the target in the grid. More... | |
void | stopMovement () |
Stop moving the target in the grid. More... | |
~RandomGridMover () 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 RandomGridMover::Ptr | create (Grid2D &grid, GridObject *target=nullptr) |
Create a RandomGridMover. 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 randomly in a Grid2D.
Note that the target cannot move backwards because it may be stuck in a loop where it switches between the same two tiles. The only time it reverses direction is when attempting to get out of a ead-end.
Definition at line 40 of file RandomGridMover.h.
using ime::RandomGridMover::Ptr = std::unique_ptr<RandomGridMover> |
Unique grid mover pointer.
Definition at line 42 of file RandomGridMover.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 | Game object to be moved in the grid |
|
override |
Destructor.
|
static |
Create a RandomGridMover.
grid | The grid to move the target in |
target | 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())
|
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
|
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.
|
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
|
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.
|
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
|
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 |
|
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
|
inherited |
Reset the target tile to be the same as the entity tile.
|
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
|
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::RandomGridMover::startMovement | ( | ) |
Start moving the target in the grid.
This function will move the target if it hasn't been moved for the first time or the movement was stopped
void ime::RandomGridMover::stopMovement | ( | ) |
Stop moving the target in the grid.
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 |