Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | List of all members
ime::GameObject Class Reference

Scene class forward declaration. More...

#include <GameObject.h>

Inheritance diagram for ime::GameObject:
ime::Object

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...
 
GameObjectoperator= (const GameObject &)
 Assignment operator. More...
 
 GameObject (GameObject &&) noexcept
 Move constructor. More...
 
GameObjectoperator= (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...
 
PropertyContainergetUserData ()
 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...
 
BodyPtrgetRigidBody ()
 Get the game objects physics body. More...
 
const BodyPtrgetRigidBody () 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...
 
TransformgetTransform ()
 Get the game objects transform. More...
 
const TransformgetTransform () const
 
void resetSpriteOrigin ()
 Reset the origin of the sprite. More...
 
SpritegetSprite ()
 Get the game objects graphical representation. More...
 
const SpritegetSprite () 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...
 

Detailed Description

Scene class forward declaration.

Abstract base class for game objects (players, enemies etc...)

Definition at line 44 of file GameObject.h.

Member Typedef Documentation

◆ BodyPtr

using ime::GameObject::BodyPtr = std::shared_ptr<Body>

Shared Body pointer.

Definition at line 47 of file GameObject.h.

◆ Ptr

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

Shared GameObject pointer.

Definition at line 46 of file GameObject.h.

Member Enumeration Documentation

◆ Type

enum ime::GameObject::Type
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.

Constructor & Destructor Documentation

◆ GameObject() [1/3]

ime::GameObject::GameObject ( Scene scene,
Type  type = Type::Unknown 
)
explicit

Construct the game object.

Parameters
sceneThe scene this game object belongs to
typeType of the game object

◆ GameObject() [2/3]

ime::GameObject::GameObject ( const GameObject other)

Copy constructor.

Parameters
otherObject to be copied

◆ GameObject() [3/3]

ime::GameObject::GameObject ( GameObject &&  )
noexcept

Move constructor.

◆ ~GameObject()

ime::GameObject::~GameObject ( )
override

Destructor.

Member Function Documentation

◆ attachRigidBody()

void ime::GameObject::attachRigidBody ( BodyPtr  body)

Attach a physics Body to the game object.

Parameters
bodyPhysics 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.

Warning
The pointer must not be a nullptr. Also, you cannot attach a rigid body to a game object that already has a rigid body attached to it, the current rigid body must be removed first
See also
removeRigidBody

◆ copy()

GameObject::Ptr ime::GameObject::copy ( ) const

Create a copy of the game object.

Returns
A new game object

◆ create()

static GameObject::Ptr ime::GameObject::create ( Scene scene,
Type  type = Type::Unknown 
)
static

Create a game object.

Returns
The created game object

◆ 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()

std::string ime::GameObject::getClassName ( ) const
overridevirtual

Get the name of the class the game object is instantiated from.

Returns
The name of the concrete 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

See also
getClassType

Implements ime::Object.

◆ getClassType()

std::string ime::GameObject::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
getClassName

Reimplemented from ime::Object.

◆ 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

◆ getRigidBody()

BodyPtr& ime::GameObject::getRigidBody ( )

Get the game objects physics body.

Returns
The game objects physics body if any, otherwise a nullptr

◆ getSprite()

Sprite& ime::GameObject::getSprite ( )

Get the game objects graphical representation.

Returns
The game objects graphical representation

By default, the sprite is empty

◆ getState()

int ime::GameObject::getState ( ) const

Get the current state of the game object.

Returns
The current state of the game object

◆ 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

◆ getTransform()

Transform& ime::GameObject::getTransform ( )

Get the game objects transform.

Returns
The game objects transform

The transform can be used to query or modify the game object position, scale, rotation and origin

◆ getType()

Type ime::GameObject::getType ( ) const

Get the type of the game object.

Returns
The type of the game object

◆ getUserData()

PropertyContainer& ime::GameObject::getUserData ( )

Get the user data added to game object.

Returns
The user data

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

◆ hasRigidBody()

bool ime::GameObject::hasRigidBody ( ) const

Check if the the game object has a rigid body attached to it.

Returns
True if the game object has a rigid body attached to it, otherwise false

◆ isActive()

bool ime::GameObject::isActive ( ) const

Check if the game object is active or not.

Returns
True if the game object is active, otherwise false

◆ isCollidable()

bool ime::GameObject::isCollidable ( ) const

Check if the game object is collidable or not.

Returns
True if the game object is collidable, otherwise false

◆ onCollisionEnd()

void ime::GameObject::onCollisionEnd ( Callback< GameObject::Ptr, GameObject::Ptr callback)

Add an event listener to a collision end event.

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

Note
A collision end event can only occur if the game object has a rigid body attached to it and the rigid body has a Collider attached to it
See also
attachRigidBody
onCollisionStart

◆ onCollisionStart()

void ime::GameObject::onCollisionStart ( Callback< GameObject::Ptr, GameObject::Ptr callback)

Add an event listener to a collision begin event.

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

Note
A collision begin event can only occur if the game object has a rigid body attached to it and the rigid body has a Collider attached to it
See also
attachRigidBody
onCollisionEnd

◆ 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

◆ 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

◆ 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)

◆ 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=() [1/2]

GameObject& ime::GameObject::operator= ( const GameObject )

Assignment operator.

◆ operator=() [2/2]

GameObject& ime::GameObject::operator= ( GameObject &&  )
noexcept

Move assignment 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!=

◆ 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

◆ removeRigidBody()

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

See also
attachRigidBody

◆ resetSpriteOrigin()

void ime::GameObject::resetSpriteOrigin ( )

Reset the origin of the sprite.

The origin is reset to the local centre of the sprite

Note
This function must be called everytime the sprites texture, texture rectangle size or scale is changed

◆ setActive()

void ime::GameObject::setActive ( bool  isActive)

Set whether the game object is active or inactive.

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

◆ setCollidable()

void ime::GameObject::setCollidable ( bool  collidable)

Set whether the game object is collidable or not.

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

◆ setState()

void ime::GameObject::setState ( int  state)

Set current state.

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

◆ 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

◆ setType()

void ime::GameObject::setType ( Type  type)

Set the type of the game object.

Parameters
typeType to set

The new type will overwrite the previous type

◆ swap()

void ime::GameObject::swap ( GameObject other)

Swap the game object with another game object.

Parameters
otherThe game object to swap with this gam object

◆ 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::GameObject::update ( Time  deltaTime)
virtual

Update the game object.

Parameters
deltaTimeTime past since last update
Warning
When overriding this function make sure to call the base class version first in your implementation

The documentation for this class was generated from the following file:
ime::GameObject::removeRigidBody
void removeRigidBody()
Remove a rigid body from the game object.