Class for modelling game objects (players, enemies etc...) More...
#include <GameObject.h>
Public Types | |
using | Ptr = std::unique_ptr< GameObject > |
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 | |
GameObject (Scene &scene) | |
Constructor. More... | |
GameObject (const GameObject &) | |
Copy constructor. More... | |
GameObject & | operator= (const GameObject &) |
Copy assignment operator. More... | |
GameObject (GameObject &&) noexcept | |
Move constructor. More... | |
GameObject & | operator= (GameObject &&) noexcept |
Move assignment operator. More... | |
void | swap (GameObject &other) |
Swap the game object with another game object. More... | |
GameObject::Ptr | copy () const |
Create a copy of the 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 | getClassName () const override |
Get the name of the class the game object is instantiated from. More... | |
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) |
~GameObject () 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 GameObject::Ptr | create (Scene &scene) |
Create a new GameObject 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... | |
Class for modelling game objects (players, enemies etc...)
Definition at line 42 of file GameObject.h.
using ime::GameObject::BodyPtr = std::unique_ptr<RigidBody> |
Unique Body pointer.
Definition at line 45 of file GameObject.h.
Collision callback.
Definition at line 46 of file GameObject.h.
using ime::GameObject::Ptr = std::unique_ptr<GameObject> |
Unique game object pointer.
Definition at line 44 of file GameObject.h.
|
explicit |
Constructor.
scene | The scene this game object belongs to |
ime::GameObject::GameObject | ( | const GameObject & | ) |
Copy constructor.
|
noexcept |
Move constructor.
|
override |
Destructor.
void ime::GameObject::attachRigidBody | ( | BodyPtr | body | ) |
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.
GameObject::Ptr ime::GameObject::copy | ( | ) | const |
Create a copy of the game object.
|
static |
Create a new GameObject 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
Implements ime::Object.
Reimplemented in ime::GridObject.
|
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.
|
inherited |
Get the unique id of the object.
Note that each instance of ime::Object has a unique id
RigidBody * ime::GameObject::getRigidBody | ( | ) |
Get the game objects physics body.
Scene & ime::GameObject::getScene | ( | ) |
Get the scene the game object belongs to.
Sprite & ime::GameObject::getSprite | ( | ) |
Get the game objects graphical representation.
By default, the sprite is empty
int ime::GameObject::getState | ( | ) | const |
Get the current state of the game object.
|
inherited |
Transform & ime::GameObject::getTransform | ( | ) |
Get the game objects transform.
The transform can be used to query or modify the game object position, scale, rotation and origin
PropertyContainer & ime::GameObject::getUserData | ( | ) |
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
bool ime::GameObject::hasRigidBody | ( | ) | const |
Check if the the game object has a rigid body attached to it.
bool ime::GameObject::isActive | ( | ) | const |
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
|
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
|
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.
int ime::GameObject::onRigidBodyCollisionEnd | ( | const CollisionCallback & | callback, |
bool | oneTime = false |
||
) |
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
int ime::GameObject::onRigidBodyCollisionStart | ( | const CollisionCallback & | callback, |
bool | oneTime = false |
||
) |
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
int ime::GameObject::onRigidBodyCollisionStay | ( | const CollisionCallback & | callback, |
bool | oneTime = false |
||
) |
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
GameObject & ime::GameObject::operator= | ( | const GameObject & | ) |
Copy assignment operator.
|
noexcept |
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 |
void ime::GameObject::removeRigidBody | ( | ) |
Remove a rigid body from the game object.
Removing a rigid Body from an game object disables all physics applied to it
void ime::GameObject::resetSpriteOrigin | ( | ) |
Reset the origin of the sprite.
The origin is reset to the local centre of the sprite
void ime::GameObject::setActive | ( | bool | isActive | ) |
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::GameObject::setState | ( | int | state | ) |
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 |
void ime::GameObject::swap | ( | GameObject & | other | ) |
Swap the game object with another game object.
other | The game object to swap with this game object |
|
inlinevirtual |
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 |