An object that can be placed in a Grid2D. More...
#include <GridObject.h>
Public Types | |
using | Ptr = std::unique_ptr< GridObject > |
Unique game object pointer. More... | |
using | BodyPtr = std::unique_ptr< RigidBody > |
Unique Body pointer. More... | |
using | CollisionCallback = Callback< GameObject *, GameObject * > |
Collision callback. More... | |
Public Member Functions | |
GridObject (Scene &scene) | |
Constructor. More... | |
GridObject (const GridObject &) | |
Copy constructor. More... | |
GridObject & | operator= (const GridObject &) |
Copy assignment operator. More... | |
GridObject (GridObject &&) noexcept=default | |
Move constructor. More... | |
GridObject & | operator= (GridObject &&) noexcept=default |
Move assignment operator. More... | |
GridObject::Ptr | copy () const |
Create a copy of the game object. More... | |
void | swap (GridObject &other) |
Swap the object with another game object. More... | |
std::string | getClassName () const override |
Get the name of the class the game object is instantiated from. More... | |
void | setDirection (const Vector2i &dir) |
Set the direction of the object. More... | |
const Vector2i & | getDirection () const |
Get the objects current direction. More... | |
void | setObstacle (bool isObstacle) |
Set whether or not the game object is an obstacle. More... | |
bool | isObstacle () const |
Check if the object is an obstacle or not. More... | |
void | setCollisionId (int id) |
Set the game objects collision id. More... | |
int | getCollisionId () const |
Get the collision id. More... | |
void | setCollisionGroup (const std::string &name) |
Set the objects collision group. More... | |
const std::string & | getCollisionGroup () const |
Get the objects collision group. 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... | |
bool | isMoving () const |
Check if target is moving or not. More... | |
Index | getGridIndex () const |
Get the index of the grid tile currently occupied by the object. More... | |
Grid2D * | getGrid () |
Get access to the grid in which the target is moved in. More... | |
const Grid2D * | getGrid () const |
ExcludeList & | getCollisionExcludeList () |
Get the game objects collision exclude list. More... | |
const ExcludeList & | getCollisionExcludeList () const |
ExcludeList & | getObstacleCollisionFilter () |
Get the game objects obstacle collision filter. More... | |
const ExcludeList & | getObstacleCollisionFilter () const |
GridMover * | getGridMover () |
Get the objects grid mover. More... | |
const GridMover * | getGridMover () const |
int | onGridEnter (const Callback< GridObject * > &callback, bool oneTime=false) |
Add an event listener to a grid enter event. More... | |
int | onGridExit (const Callback< GridObject * > &callback, bool oneTime=false) |
Add an event listener to a grid exit event. More... | |
int | onGridMoveBegin (const Callback< GridObject * > &callback, bool oneTime=false) |
Add an event listener to a move begin event. More... | |
int | onGridPreMove (const Callback< GridObject * > &callback, bool oneTime=false) |
Add an event listener to a pre move event. More... | |
int | onGridPostMove (const Callback< GridObject * > &callback, bool oneTime=false) |
Add an event listener to a grid move event. More... | |
int | onGridMoveEnd (const Callback< GridObject * > &callback, bool oneTime=false) |
Add an event listener to an adjacent tile reached event. More... | |
int | onGridObjectCollision (const Callback< GridObject *, GridObject * > &callback, bool oneTime=false) |
Add an event listener to a grid object collision event. More... | |
int | onGridBorderCollision (const Callback< GridObject * > &callback, bool oneTime=false) |
Add an event listener to a grid border collision event. More... | |
int | onGridTileCollision (const Callback< GridObject *, Index > &callback, bool oneTime=false) |
Add an event listener to a grid tile collision event. More... | |
void | setGridMover (GridMover *gridMover) |
void | setGrid (Grid2D *grid) |
void | emitGridEvent (const Property &property) |
~GridObject () override | |
Destructor. More... | |
void | swap (GameObject &other) |
Swap the game object with another game object. More... | |
void | setState (int state) |
Set current state. More... | |
int | getState () const |
Get the current state of the game object. More... | |
void | setActive (bool isActive) |
Set whether or not the game object is active. More... | |
bool | isActive () const |
Check if the game object is active or not. More... | |
PropertyContainer & | getUserData () |
Get the user data added to game object. More... | |
const PropertyContainer & | getUserData () const |
std::string | getClassType () const override |
Get the name of this class. More... | |
void | attachRigidBody (BodyPtr body) |
Attach a physics Body to the game object. More... | |
RigidBody * | getRigidBody () |
Get the game objects physics body. More... | |
const RigidBody * | getRigidBody () const |
void | removeRigidBody () |
Remove a rigid body from the game object. More... | |
bool | hasRigidBody () const |
Check if the the game object has a rigid body attached to it. More... | |
int | onRigidBodyCollisionStart (const CollisionCallback &callback, bool oneTime=false) |
Add an event listener to a rigid body collision begin event. More... | |
int | onRigidBodyCollisionEnd (const CollisionCallback &callback, bool oneTime=false) |
Add an event listener to a rigid body collision end event. More... | |
int | onRigidBodyCollisionStay (const CollisionCallback &callback, bool oneTime=false) |
Add an event listener to a rigid body collision stay event. More... | |
Transform & | getTransform () |
Get the game objects transform. More... | |
const Transform & | getTransform () const |
Scene & | getScene () |
Get the scene the game object belongs to. More... | |
const Scene & | getScene () const |
void | resetSpriteOrigin () |
Reset the origin of the sprite. More... | |
Sprite & | getSprite () |
Get the game objects graphical representation. More... | |
const Sprite & | getSprite () const |
virtual void | update (Time deltaTime) |
Update the game object. More... | |
void | emitRigidBodyCollisionEvent (const std::string &event, GameObject *other) |
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 GridObject::Ptr | create (Scene &scene) |
Create a new GridObject instance. 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... | |
An object that can be placed in a Grid2D.
Definition at line 40 of file GridObject.h.
|
inherited |
Unique Body pointer.
Definition at line 45 of file GameObject.h.
|
inherited |
Collision callback.
Definition at line 46 of file GameObject.h.
using ime::GridObject::Ptr = std::unique_ptr<GridObject> |
Unique game object pointer.
Definition at line 42 of file GridObject.h.
|
explicit |
Constructor.
scene | The scene this game object belongs to |
ime::GridObject::GridObject | ( | const GridObject & | ) |
Copy constructor.
|
defaultnoexcept |
Move constructor.
|
override |
Destructor.
|
inherited |
Attach a physics Body to the game object.
body | Physics body to be attached to the game object |
When a rigid body is attached to a game object, the game object becomes enabled for physics. This means that it will react to gravity, friction, applies forces, impulses etc. The position and rotation of the game object will be controlled by the physics engine therefore you should refrain from calling functions that MODIFY the game objects transform (position, rotation and origin). A result of doing so is inconsistency. Note that the physics engine does not account for scaling. This means that scaling the objects sprite will NOT scale the objects body or the body's collider. If you want the body to scale with the objects sprite, you should remove the old collider and attach a new one with the appropriate size.
GridObject::Ptr ime::GridObject::copy | ( | ) | const |
Create a copy of the game object.
|
static |
Create a new GridObject instance.
scene | The scene the object belongs to |
|
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 the class the game object is instantiated from.
Note that this function must be overridden further if this class is extended, otherwise it will return the name of this class instead of your class name
Reimplemented from ime::GameObject.
|
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.
ExcludeList & ime::GridObject::getCollisionExcludeList | ( | ) |
Get the game objects collision exclude list.
This list allows you to specify which game objects this game object can collide with
By default, the game object collides with all other objects
const std::string & ime::GridObject::getCollisionGroup | ( | ) | const |
int ime::GridObject::getCollisionId | ( | ) | const |
const Vector2i & ime::GridObject::getDirection | ( | ) | const |
Get the objects current direction.
Grid2D * ime::GridObject::getGrid | ( | ) |
Get access to the grid in which the target is moved in.
Index ime::GridObject::getGridIndex | ( | ) | const |
Get the index of the grid tile currently occupied by the object.
Note that when the object is moved by a GridMover, it occupies a tile ahead of time, see ime::GridMover::requestMove()
This function returns ime::Index{-1, -1} if the object is not in a Grid
GridMover * ime::GridObject::getGridMover | ( | ) |
Get the objects grid mover.
|
inherited |
Get the unique id of the object.
Note that each instance of ime::Object has a unique id
ExcludeList & ime::GridObject::getObstacleCollisionFilter | ( | ) |
Get the game objects obstacle collision filter.
By default, any object that cannot collide with an obstacle game object will pass through it without generating a collision event whereas if they can collide, the two objects will never overlap. Sometimes you may want an obstacle object to exhibit the default behavior for some objects but allow others to pass through it and still generate a collision event. This list helps you achieve that. The collision groups of game objects added to this list will pass over an obstacle game object but generate a collision event
|
inherited |
Get the game objects physics body.
|
inherited |
Get the scene the game object belongs to.
const Vector2f & ime::GridObject::getSpeed | ( | ) | const |
|
inherited |
Get the game objects graphical representation.
By default, the sprite is empty
|
inherited |
Get the current state of the game object.
|
inherited |
|
inherited |
Get the game objects transform.
The transform can be used to query or modify the game object position, scale, rotation and origin
|
inherited |
Get the user data added to game object.
The user data object can be used to store additional information about the game object. For example, you may store a profile associated with the game object or when the game object was instantiated etc... You can store any type and any number of data in the user date object
Note that IME does not store anything inside the user data object, it is reserved for external use only
|
inherited |
Check if the the game object has a rigid body attached to it.
|
inherited |
Check if the game object is active or not.
|
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::GridObject::isMoving | ( | ) | const |
Check if target is moving or not.
bool ime::GridObject::isObstacle | ( | ) | const |
Check if the object is an obstacle or not.
|
inherited |
Check if another object is the same instance as this object.
other | The object to compare against this object |
|
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::GridObject::onGridBorderCollision | ( | const Callback< GridObject * > & | 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
The callback is passed this object on invocation
int ime::GridObject::onGridEnter | ( | const Callback< GridObject * > & | callback, |
bool | oneTime = false |
||
) |
Add an event listener to a grid enter event.
callback | The function to be executed when the game object is added to a Grid2D |
oneTime | True to execute the callback one-time or false to execute it every time the event is triggered |
The callback is passed this game object on invocation
int ime::GridObject::onGridExit | ( | const Callback< GridObject * > & | callback, |
bool | oneTime = false |
||
) |
Add an event listener to a grid exit event.
callback | The function to be executed when the game object is removed from a Grid2D |
oneTime | True to execute the callback one-time or false to execute it every time the event is triggered |
The callback is passed this game object on invocation
int ime::GridObject::onGridMoveBegin | ( | const Callback< GridObject * > & | callback, |
bool | oneTime = false |
||
) |
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
The callback is passed this game object on invocation
int ime::GridObject::onGridMoveEnd | ( | const Callback< GridObject * > & | callback, |
bool | oneTime = false |
||
) |
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 this game object on invocation
int ime::GridObject::onGridObjectCollision | ( | const Callback< GridObject *, GridObject * > & | callback, |
bool | oneTime = false |
||
) |
Add an event listener to a grid object collision event.
callback | Function to be executed when this game object collides with another game object in a Grid2D |
oneTime | True to execute the callback one-time or false to execute it every time the event is triggered |
This event is triggered when this grid object collides with another grid object or vice versa. A collision is registered between two grid objects when the occupy the same Grid2D tile
The callback is passed this game object and the game object it is in collision with respectively.
int ime::GridObject::onGridPostMove | ( | const Callback< GridObject * > & | callback, |
bool | oneTime = false |
||
) |
Add an event listener to a grid move event.
callback | The function to be executed after the object moves in the grid |
oneTime | True to execute the callback one-time or false to execute it every time the event is triggered |
This event is fired every time after the object moves in the grid. The difference between onGridMoveEnd() and onGridPostMove() is that the former is called once at the end of the tile movement and the latter is called every time after the objects position is updated while in transit to its target tile.
You can add as many event listeners as you want to this event. Furthermore, the callback is passed this game object on invocation
int ime::GridObject::onGridPreMove | ( | const Callback< GridObject * > & | callback, |
bool | oneTime = false |
||
) |
Add an event listener to a pre move event.
callback | The function to be executed before the object is moved in the grid |
oneTime | True to execute the callback one-time or false to execute it every time the event is triggered |
This event is fired every time the object is about to move in the grid. The difference between onGridMoveBegin() and onGridPreMove() is that the former is called once per tile movement and the latter is called every time the objects position is about to be updated while in transit to its target tile. In other words, onGridMoveBegin() is called at the beginning of the tile movement and onGridPreMove() is called until the object completes its tile movement.
You can add as many event listeners as you want to this event. Furthermore, the callback is passed this game object on invocation
int ime::GridObject::onGridTileCollision | ( | const Callback< GridObject *, Index > & | callback, |
bool | oneTime = false |
||
) |
Add an event listener to a grid 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 object collides with a grid tile By default, the event is handled internally before its emitted. The internal handler prevents the object from occupying the tile by moving it back to its previous tile. This behaviour is not removable, however, it may be overridden since the internal handler is called first before alerting external handlers
The callback is passed this object and the index of the tile it tried to occupy on invocation
|
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 a rigid body collision end event.
callback | The function to be executed when the event is fired |
oneTime | True to execute the callback one-time or false to execute it every time the event is triggered |
The callback function is called when two game objects stop overlapping. The callback is passed this game object and the game object that stopped overlapping with this game object respectively.
A collision end handler may be registered on the game object or on the collider that is attached to the game objects rigid body or on both. However, exercise caution as registering the same handler on both objects will result in the handler being executed twice each time the event is fired
|
inherited |
Add an event listener to a rigid body collision begin event.
callback | The function to be executed when the event is fired |
oneTime | True to execute the callback one-time or false to execute it every time the event is triggered |
The callback function is called when two game objects begin to overlap. The callback is passed this game object and the game object that collided with this game object respectively.
A collision begin handler may be registered on the game object or on the collider that is attached to the game objects rigid body or on both. However, exercise caution as registering the same handler on both objects will result in the handler being executed twice each time the event is fired
|
inherited |
Add an event listener to a rigid body collision stay event.
callback | The function to be executed when the event is fired |
oneTime | True to execute the callback one-time or false to execute it every time the event is triggered |
The callback function is called while this game object remains in contact with another game object. The callback is passed this game object and the game object that is currently in contact with this game object respectively.
A collision stay handler may be registered on the game object or on the collider that is attached to the game objects rigid body or on both. However, exercise caution as registering the same handler on both objects will result in the handler being executed twice each time the event is fired
GridObject & ime::GridObject::operator= | ( | const GridObject & | ) |
Copy assignment operator.
|
defaultnoexcept |
Move 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 |
|
inherited |
Remove a rigid body from the game object.
Removing a rigid Body from an game object disables all physics applied to it
|
inherited |
Reset the origin of the sprite.
The origin is reset to the local centre of the sprite
|
inherited |
Set whether or not the game object is active.
isActive | True to set as active or false to set as inactive |
Note that an inactive game object does not participate in collision detection. In addition the game object's update() method will not be invoked.
By default, the object is active
void ime::GridObject::setCollisionGroup | ( | const std::string & | name | ) |
Set the objects collision group.
name | The collision group to be set |
A collision group allows certain game objects to always collide or never collide with each other. When a collision group is added to the object's collision exclusion list (see getCollisionExcludeList()), the game object will never collide with game objects in that group (they will pass through each other without generating a collision event), whereas when not added, the game object will always collide with game objects whose collision group does not appear in its exclusion list
Note that the active state (see ime::GameObject::setActive()) takes higher precedence than "collision group" filtering. This means that, regardless of how the collision groups are configured, a collision will never take place if the game object is not active
For example, the following code makes objects in the "Enemies" collision group to never collide with each other:
By default, the collision group is am empty string. This means that the object does not belong to any collision group. Therefore, it will collide with any other game object whose collision id is the same as theirs
void ime::GridObject::setCollisionId | ( | int | id | ) |
Set the game objects collision id.
id | The id to be set |
A collision id allows game objects to selectively collide with each other within colliding groups (see setCollisionGroup()). Game objects with the same collision id will collide with each other, whilst game objects with different collision id's do not collide with each other.
Note that "collision group" filtering takes higher precedence than "collision id" filtering. This means that regardless of how collision id's are configured, a collision will never take place if the collision group of one game object is added to the other game objects collision group exclude list (see getCollisionExcludeList())
By default, the collision id is 0. This means that this game object will collide with any other game object that is not in its collision group exclude list
void ime::GridObject::setDirection | ( | const Vector2i & | dir | ) |
Set the direction of the object.
dir | The direction to be set |
By default the direction is Vector2i{0, 0}
void ime::GridObject::setObstacle | ( | bool | isObstacle | ) |
Set whether or not the game object is an obstacle.
isObstacle | True if it is an obstacle, otherwise false |
When the object is an obstacle and its active state is true (see ime::GameObject::setActive), then other game objects cannot overlap with it. A collision event will be raised when another game object attempts to occupy the same cell as the obstacle. However, the two objects will never overlap
By default, the game object is not an obstacle
void ime::GridObject::setSpeed | ( | const Vector2f & | speed | ) |
Set the speed of the game object.
speed | The new speed |
By default, the speed is ime::Vector2f{0, 0}
|
inherited |
Set current state.
state | The current state |
The state is number that should be associated with something (maybe an enum) in your game.
Be default, the state is -1, which is supposed to indicate that there is no state. The state property is optional and may be used if needs be. It is not used by IME
|
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 |
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 |
Swap the game object with another game object.
other | The game object to swap with this game object |
void ime::GridObject::swap | ( | GridObject & | other | ) |
Swap the object with another game object.
other | The object to swap with this game object |
|
inlinevirtualinherited |
Update the game object.
deltaTime | Time past since last update |
This function is automatically called by IME. deltaTime is synced with the render FPS. In other words, it is frame-rate dependent.
Note that this function is provided for external use only, IME will never put anything inside it. This means that don't have to call the base class version when overriding it
Definition at line 346 of file GameObject.h.
|
protectedinherited |