Scene class forward declaration. More...
#include <GameObject.h>
Public Types | |
enum | Type { Type::Unknown = -1, Type::Player = 0, Type::Enemy, Type::Collectable, Type::Obstacle } |
The type of the GameObject. More... | |
using | Ptr = std::shared_ptr< GameObject > |
Shared GameObject pointer. More... | |
using | BodyPtr = std::shared_ptr< Body > |
Shared Body pointer. More... | |
Public Member Functions | |
GameObject (Scene &scene, Type type=Type::Unknown) | |
Construct the game object. More... | |
GameObject (const GameObject &other) | |
Copy constructor. More... | |
GameObject & | operator= (const GameObject &) |
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 | setType (Type type) |
Set the type of the game object. More... | |
Type | getType () const |
Get the type 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 the game object is active or inactive. More... | |
bool | isActive () const |
Check if the game object is active or not. More... | |
void | setCollidable (bool collidable) |
Set whether the game object is collidable or not. More... | |
bool | isCollidable () const |
Check if the game object is collidable or not. More... | |
PropertyContainer & | getUserData () |
Get the user data added to game object. More... | |
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... | |
BodyPtr & | getRigidBody () |
Get the game objects physics body. More... | |
const BodyPtr & | 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... | |
void | onCollisionStart (Callback< GameObject::Ptr, GameObject::Ptr > callback) |
Add an event listener to a collision begin event. More... | |
void | onCollisionEnd (Callback< GameObject::Ptr, GameObject::Ptr > callback) |
Add an event listener to a collision end event. More... | |
Transform & | getTransform () |
Get the game objects transform. More... | |
const Transform & | getTransform () 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 | emitCollisionEvent (const std::string &event, const GameObject::Ptr &other) |
~GameObject () 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... | |
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... | |
Static Public Member Functions | |
static GameObject::Ptr | create (Scene &scene, Type type=Type::Unknown) |
Create a game object. More... | |
Protected Member Functions | |
void | emitChange (const Property &property) |
Dispatch a property change event. More... | |
void | emit (const std::string &event) |
Dispatch an action event. More... | |
Scene class forward declaration.
Abstract base class for game objects (players, enemies etc...)
Definition at line 44 of file GameObject.h.
using ime::GameObject::BodyPtr = std::shared_ptr<Body> |
Shared Body pointer.
Definition at line 47 of file GameObject.h.
using ime::GameObject::Ptr = std::shared_ptr<GameObject> |
Shared GameObject pointer.
Definition at line 46 of file GameObject.h.
|
strong |
The type of the GameObject.
Enumerator | |
---|---|
Unknown | Unknown object. |
Player | Player object. |
Enemy | Enemy object. |
Collectable | Collectable object. |
Obstacle | Obstacle object. |
Definition at line 52 of file GameObject.h.
|
explicit |
Construct the game object.
scene | The scene this game object belongs to |
type | Type of the game object |
ime::GameObject::GameObject | ( | const GameObject & | other | ) |
Copy constructor.
other | Object to be copied |
|
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 game object.
|
protectedinherited |
Dispatch an action event.
event | The 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)
|
protectedinherited |
Dispatch a property change event.
property | The property that changed |
This function will invoke all the event listeners of the specified property
|
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.
|
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 id of the object.
Each object has a unique id
BodyPtr& ime::GameObject::getRigidBody | ( | ) |
Get the game objects physics body.
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
Type ime::GameObject::getType | ( | ) | const |
Get the type of the game object.
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.
bool ime::GameObject::isCollidable | ( | ) | const |
Check if the game object is collidable or not.
void ime::GameObject::onCollisionEnd | ( | Callback< GameObject::Ptr, GameObject::Ptr > | callback | ) |
Add an event listener to a collision end event.
callback | The function to be executed when event is fired |
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. Pass nullptr to remove the current callback.
A collision end handler may be registered on the game object or on the rigid body attached to the game object or on both. However, it is advised to register the handler on either the game object or the rigid Body because registering the handler on both objects will result in the handler being executed twice each time the event is fired
void ime::GameObject::onCollisionStart | ( | Callback< GameObject::Ptr, GameObject::Ptr > | callback | ) |
Add an event listener to a collision begin event.
callback | The function to be executed when event is fired |
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. Pass nullptr to remove the current callback
A collision begin handler may be registered on the game object or on the rigid body attached to the game object or on both. However, it is advised to register the handler on either the game object or the rigid Body because registering the handler on both objects will result in the handler being executed twice each time the event is fired
|
inherited |
Add a destruction listener.
callback | Function to be executed when the object is destroyed |
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
|
inherited |
Add an event listener to an event.
event | The name of the event to add an an event listener to |
callback | The function to be executed when the event takes place |
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 an event listener to a property change event.
callback | The function to be executed when the property changes |
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.
|
inherited |
Add an event listener to a specific property change event.
property | The name of the property to listen for |
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, 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:
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
|
inherited |
Check if two objects are not the same object.
rhs | Object to compare against this object |
Two objects are different from each other if they have different object id's
GameObject& ime::GameObject::operator= | ( | const GameObject & | ) |
Assignment operator.
|
noexcept |
Move assignment operator.
|
inherited |
Check if two objects are the same object or not.
rhs | Object to compare against this object |
Two objects are the same object if they have the same object id. Recall that each object instance has a unique id
|
inherited |
Remove a destruction listener form the object.
The | id of the destruction 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 the game object is active or inactive.
isActive | True to set active or false to set inactive |
An active game object in this context refers to a game object that is in a good state, not killed or completely destroyed, whilst an inactive game object refers to one that is killed or destroyed
void ime::GameObject::setCollidable | ( | bool | collidable | ) |
Set whether the game object is collidable or not.
collidable | True to make collidable, otherwise false |
This function has no effect if the game object does not have a physics body attached to it or if the attached physics body does not have a collider.
When collidable is true, this function will reset the collision filter to all collisions and when collidable is false the function will reset the collision filter to no collisions. If the game object must collide with some game objects and ignore other game objects then it is advised to use the game objects Collider to enable or disable collisions as it gives you great flexibility
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 internally
|
inherited |
Assign the object an alias.
name | The 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
void ime::GameObject::setType | ( | Type | type | ) |
Set the type of the game object.
type | Type to set |
The new type will overwrite the previous type
void ime::GameObject::swap | ( | GameObject & | other | ) |
Swap the game object with another game object.
other | The game object to swap with this gam object |
|
inherited |
Remove an event listener from an event.
event | The name of the event to remove event listener from |
id | The unique id of the event listener to be removed |
|
virtual |
Update the game object.
deltaTime | Time past since last update |