Public Types | Public Member Functions | Protected Member Functions | List of all members
ime::GridMover Class Referenceabstract

Class for performing grid based movement on an entity in a grid. More...

#include <GridMover.h>

Inheritance diagram for ime::GridMover:
ime::Object ime::KeyboardGridMover ime::RandomGridMover ime::TargetGridMover

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...
 
TileMapgetGrid ()
 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...
 

Detailed Description

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.

Member Typedef Documentation

◆ Ptr

using ime::GridMover::Ptr = std::shared_ptr<GridMover>

Shared grid mover pointer.

Definition at line 61 of file GridMover.h.

Member Enumeration Documentation

◆ MoveRestriction

Restricts the movement of the target along axes.

Enumerator
None 

Target can move in all in all 8 directions (W, NW, N, NE, E, SE, S, SW)

All 

Target cannot move in any direction.

Vertical 

Target can only move vertically (N or S)

Horizontal 

Target can only move horizontally (W or E)

Diagonal 

Target can only move diagonally (NW, NE, SE, SW)

NonDiagonal 

Target can only move non-diagonally (W, N, E, S)

Definition at line 77 of file GridMover.h.

◆ Type

enum ime::GridMover::Type
strong

Types of grid movers.

Enumerator
Manual 

Manually triggered grid mover.

Random 

Moves a game object randomly in the grid.

Target 

Moves a game object to a specific tile within the grid.

KeyboardControlled 

Moves a game object within the grid using the keyboard as a trigger.

Custom 

For classes that extend the grid mover outside of IME.

Definition at line 66 of file GridMover.h.

Constructor & Destructor Documentation

◆ GridMover() [1/2]

ime::GridMover::GridMover ( TileMap tilemap,
GameObject::Ptr  gameObject = nullptr 
)
explicit

Create a manually controlled grid mover.

Parameters
tilemapThe grid the game object is in
gameObjectThe game object to be controlled by the grid mover
Warning
You may omit the game object if you want to set it later, However if the target is provided gameObject != nullptr, then it must be placed in the grid prior to instantiation of this class
See also
setTarget

◆ ~GridMover()

ime::GridMover::~GridMover ( )
override

Destructor.

◆ GridMover() [2/2]

ime::GridMover::GridMover ( Type  type,
TileMap tileMap,
GameObject::Ptr  target 
)
protected

Create a grid mover.

Parameters
typeThe type of the grid mover
tileMapGrid to move a target entity in
targetGameObject 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

Warning
if the target is not a nullptr, then it must be placed in the grid prior to instantiation of this class

Member Function Documentation

◆ emit()

void ime::Object::emit ( const std::string &  event)
protectedinherited

Dispatch an action event.

Parameters
eventThe 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)

See also
emitChange

◆ emitChange()

void ime::Object::emitChange ( const Property property)
protectedinherited

Dispatch a property change event.

Parameters
propertyThe property that changed

This function will invoke all the event listeners of the specified property

See also
emit

◆ getClassName()

virtual std::string ime::Object::getClassName ( ) const
pure virtualinherited

Get the name of the concrete class.

Returns
The name of the objects concrete class

This function is implemented by all internal classes that inherit from this class.

See also
getClassType

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.

◆ getClassType()

std::string ime::GridMover::getClassType ( ) const
overridevirtual

Get the name of this class.

Returns
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

See also
Object::getClassType and Object::getClassName

Reimplemented from ime::Object.

◆ getDirection()

Direction ime::GridMover::getDirection ( ) const

Get the current direction of the game object.

Returns
The current direction of the game object

◆ getGrid()

TileMap& ime::GridMover::getGrid ( )

Get access to the grid in which the target is moved in.

Returns
The grid in which the target is being moved in

◆ getMaxLinearSpeed()

Vector2f ime::GridMover::getMaxLinearSpeed ( ) const

Get the maximum speed of the game object.

Returns
The maximum speed of the game object

◆ getMovementRestriction()

MoveRestriction ime::GridMover::getMovementRestriction ( ) const

Get the current movement restriction of the game object.

Returns
The current movement restriction

◆ getObjectId()

unsigned int ime::Object::getObjectId ( ) const
inherited

Get the id of the object.

Returns
The id of the object

Each object has a unique id

◆ getTag()

const std::string& ime::Object::getTag ( ) const
inherited

Get the alias of the object.

Returns
The alias of the object
See also
setTag and getObjectId

◆ getTarget()

GameObject::Ptr ime::GridMover::getTarget ( ) const

Get access to the controlled entity.

Returns
The controlled entity, or a nullptr if there is no entity to control

◆ getTargetTileIndex()

virtual Index ime::GridMover::getTargetTileIndex ( ) const
virtual

Get the index of the adjacent tile the target is trying to reach.

Returns
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.

◆ getType()

Type ime::GridMover::getType ( ) const

Get the type of the grid mover.

Returns
The type of the grid mover

◆ isTargetMoving()

bool ime::GridMover::isTargetMoving ( ) const

Check if target is moving or not.

Returns
True if target is moving otherwise false
Warning
This function will return false if the target is not moving or there is no target set. Therefore, the existence of the target should be checked first for accurate results
See also
getTarget

◆ onAdjacentTileReached()

int ime::GridMover::onAdjacentTileReached ( Callback< Tile callback)

Add an event listener to an adjacent tile reached event.

Parameters
callbackFunction to execute when the target reaches its target tile
Returns
The event listeners identification number

This event is emitted when the target moves from its current tile to any of its adjacent tiles.

Note
When controlled by a grid mover, the target will always move one tile at a time, regardless of how fast the target is moving

The callback is passed the tile the target moved to

See also
onMoveBegin

◆ onCollectableCollision()

int ime::GridMover::onCollectableCollision ( Callback< GameObject::Ptr, GameObject::Ptr callback)

Add an event listener to a collectable collision event.

Parameters
callbackFunction to execute when the collision takes place
Returns
The event listeners identification number

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

◆ onDestruction()

int ime::Object::onDestruction ( const Callback<> &  callback)
inherited

Add a destruction listener.

Parameters
callbackFunction to be executed when the object is destroyed
Returns
The id of the destruction listener

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

Warning
The callback is called when the object is destroyed. Do not try to access the object in the callback. Doing so is undefined behavior
See also
removeDestructionListener

◆ onEnemyCollision()

int ime::GridMover::onEnemyCollision ( Callback< GameObject::Ptr, GameObject::Ptr callback)

Add an event listener to an enemy collision event.

Parameters
callbackFunction to execute when the collision takes place
Returns
The event listeners identification number

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

◆ onEvent()

int ime::Object::onEvent ( const std::string &  event,
const Callback<> &  callback 
)
inherited

Add an event listener to an event.

Parameters
eventThe name of the event to add an an event listener to
callbackThe function to be executed when the event takes place
Returns
The unique identification number of the event listener

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:

// Add event listeners to the object
object.onEvent("attachRigidBody", [] {
std::cout << "Rigid body attached to object << std::endl;
});
object.onEvent("removeRigidBody", [] {
std::cout << "Rigid body removed from object << std::endl;
});
// Invokes event listener(s)
object.attachRigidBody(body);
object.removeRigidBody();
See also
onPropertyChange and unsubscribe

◆ onGridBorderCollision()

int ime::GridMover::onGridBorderCollision ( Callback<>  callback)

Add an event listener to a tilemap border collision event.

Parameters
callbackFunction to execute when the collision takes place
Returns
The event listeners identification number

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

◆ onMoveBegin()

int ime::GridMover::onMoveBegin ( Callback< Tile callback)

Add an event listener to a move begin event.

Parameters
callbackThe function to be executed when the game object starts moving
Returns
The event listeners unique identification number

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

Note
When controlled by a grid mover, the game object will always move one tile at a time, regardless of how fast it's moving
See also
onAdjacentTileReached

◆ onObstacleCollision()

int ime::GridMover::onObstacleCollision ( Callback< GameObject::Ptr, GameObject::Ptr callback)

Add an event listener to an obstacle collision.

Parameters
callbackFunction to execute when the collision takes place
Returns
The event listeners identification number

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

◆ onPlayerCollision()

int ime::GridMover::onPlayerCollision ( Callback< GameObject::Ptr, GameObject::Ptr callback)

Add an event listener to a player collision event.

Parameters
callbackFunction to execute when the collision takes place
Returns
The event listeners identification number

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

◆ onPropertyChange() [1/2]

void ime::Object::onPropertyChange ( const Callback< Property > &  callback)
inherited

Add an event listener to a property change event.

Parameters
callbackThe function to be executed when the property changes
Returns
The unique id of the event listener

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.

See also
onPropertyChange(std::string, Callback)

◆ onPropertyChange() [2/2]

int ime::Object::onPropertyChange ( const std::string &  property,
const Callback< Property > &  callback 
)
inherited

Add an event listener to a specific property change event.

Parameters
propertyThe name of the property to listen for
callbackThe function to be executed when the property changes
Returns
The unique id of the event listener

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:

// Prints the tag of the player object to the console everytime it changes
player.onPropertyChange("tag", [](const Property& property) {
cout << "New tag: " << property.getValue<std::string>() << endl;
});
...
//Sets tag = "player1" and invokes event listener(s)
player.setTag("player1");

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

See also
unsubscribe and onPropertyChange(Callback)

◆ onSolidTileCollision()

int ime::GridMover::onSolidTileCollision ( Callback< Tile callback)

Add an event listener to a tile collision event.

Parameters
callbackFunction to execute when the collision takes place
Returns
The event listeners identification number

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

◆ onTargetChanged()

int ime::GridMover::onTargetChanged ( Callback< GameObject::Ptr callback)

Add an event listener to a target change event.

Parameters
callbackFunction to execute when the target changes
Returns
The event listeners identification number

◆ operator!=()

bool ime::Object::operator!= ( const Object rhs) const
inherited

Check if two objects are not the same object.

Parameters
rhsObject to compare against this object
Returns
True if rhs is NOT the same object as this object, otherwise false

Two objects are different from each other if they have different object id's

See also
getObjectId and operator==

◆ operator==()

bool ime::Object::operator== ( const Object rhs) const
inherited

Check if two objects are the same object or not.

Parameters
rhsObject to compare against this object
Returns
True if rhs is the same object as this object, otherwise false

Two objects are the same object if they have the same object id. Recall that each object instance has a unique id

See also
getObjectId and operator!=

◆ removeCollisionHandler()

bool ime::GridMover::removeCollisionHandler ( int  id)

Remove a collision handler.

Parameters
idIdentification number of the handler
Returns
True if the handler was removed or false if no such handler exits

The identification number is the number returned when an event listener was added to a collision event

◆ removeDestructionListener()

bool ime::Object::removeDestructionListener ( int  id)
inherited

Remove a destruction listener form the object.

Parameters
Theid of the destruction listener to be removed
Returns
True if the destruction listener was removed or false if the listener with the given id does not exist

◆ removeEventListener()

bool ime::GridMover::removeEventListener ( const std::string &  event,
int  id 
)

Remove an event listener from an event.

Parameters
eventName of the event to remove event listener from
idIdentification number of the event listener
Returns
True if the event listener was removed or false if the given event does not have an event listener with the given id

The identification number is the number returned when an event listener was added to an events

◆ requestDirectionChange()

bool ime::GridMover::requestDirectionChange ( const Direction newDir)

Change the direction of the game object.

Parameters
newDirThe new direction of the game object
Returns
True if the direction was changed or false if the game object is in motion or the grid mover is not in control of any 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)

gridMover.onPropertyChange("direction", [](const ime::Property& p) {
auto dir = p.getValue<ime::Direction>();
// Do something - Maybe rotate the game object in the new direction
});
See also
update

◆ setMaxLinearSpeed()

void ime::GridMover::setMaxLinearSpeed ( Vector2f  speed)

Set the maximum linear speed of the game object.

Parameters
speedThe new maximum speed

If the game object is currently moving, the speed will be set after it reaches its current target tile

Warning
When using a grid mover the velocity of the game object must not be set directly but rather through this function. Setting the velocity directly will transfer movement management of the game object from the grid mover to the physics engine

◆ setMovementRestriction()

void ime::GridMover::setMovementRestriction ( MoveRestriction  moveRestriction)

Restrict the movement of the game object to certain directions.

Parameters
moveRestrictionPermitted direction of travel

By default the movement restrictions is MovementRestriction::None

◆ setTag()

void ime::Object::setTag ( const std::string &  tag)
inherited

Assign the object an alias.

Parameters
nameThe 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

◆ setTarget()

void ime::GridMover::setTarget ( GameObject::Ptr  target)

Change the controlled entity.

Parameters
targetNew target

Provide nullptr as argument to remove current target

Warning
if the target is not a nullptr, then it must exist in the grid prior to function call

◆ teleportTargetToDestination()

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

◆ unsubscribe()

bool ime::Object::unsubscribe ( const std::string &  event,
int  id 
)
inherited

Remove an event listener from an event.

Parameters
eventThe name of the event to remove event listener from
idThe unique id of the event listener to be removed
Returns
True if the event listener was removed or false if the event or the event listener is does not exist
// Display the name of the object to console every time it changes
auto nameChangeId = object.onPropertyChange("name", [](Property name) {
std::cout << name.getValue<std::string>() << std::endl;
});
// Stop displaying the name of the object when it changes
object.unsubscribe("name", id);

◆ update()

virtual void ime::GridMover::update ( Time  deltaTime)
virtual

Update entity movement in the grid.

Parameters
deltaTimeTime 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

See also
onAdjacentTileReached and requestDirectionChange
Warning
The target will never move if this function is never called

Reimplemented in ime::RandomGridMover.


The documentation for this class was generated from the following file:
ime::Property
Class that can store a value of any type.
Definition: Property.h:36
ime::Vector2
2D vector
Definition: Vector2.h:35
ime::Property::getValue
T getValue() const
Get the value of the property.