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

Class for modelling game objects (players, enemies etc...) More...

#include <GameObject.h>

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

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...
 
GameObjectoperator= (const GameObject &)
 Copy 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 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...
 
PropertyContainergetUserData ()
 Get the user data added to game object. More...
 
const PropertyContainergetUserData () 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...
 
RigidBodygetRigidBody ()
 Get the game objects physics body. More...
 
const RigidBodygetRigidBody () 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...
 
TransformgetTransform ()
 Get the game objects transform. More...
 
const TransformgetTransform () const
 
ScenegetScene ()
 Get the scene the game object belongs to. More...
 
const ScenegetScene () 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 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...
 

Detailed Description

Class for modelling game objects (players, enemies etc...)

Definition at line 42 of file GameObject.h.

Member Typedef Documentation

◆ BodyPtr

using ime::GameObject::BodyPtr = std::unique_ptr<RigidBody>

Unique Body pointer.

Definition at line 45 of file GameObject.h.

◆ CollisionCallback

Collision callback.

Definition at line 46 of file GameObject.h.

◆ Ptr

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

Unique game object pointer.

Definition at line 44 of file GameObject.h.

Constructor & Destructor Documentation

◆ GameObject() [1/3]

ime::GameObject::GameObject ( Scene scene)
explicit

Constructor.

Parameters
sceneThe scene this game object belongs to

◆ GameObject() [2/3]

ime::GameObject::GameObject ( const GameObject )

Copy constructor.

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

Create a new GameObject instance.

Parameters
sceneThe scene the object belongs to
Returns
The created instance

◆ 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

◆ emitDestruction()

void ime::Object::emitDestruction ( )
protectedinherited

Emit a destruction event.

Note
This function must be the first statement in the definition of a destructor to avoid undefined behavior. In addition, note that destruction listeners are invoked once. Therefore, multiple classes in a hierarchy may call this function but the class that makes the call first will be the one that invokes the destruction listeners

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

Reimplemented in ime::GridObject.

◆ 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 unique id of the object.

Returns
The unique id of the object

Note that each instance of ime::Object has a unique id

See also
setTag

◆ getRigidBody()

RigidBody * ime::GameObject::getRigidBody ( )

Get the game objects physics body.

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

◆ getScene()

Scene & ime::GameObject::getScene ( )

Get the scene the game object belongs to.

Returns
The scene the game object belongs to

◆ 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 tag assigned to the object.

Returns
The tag of the object
See also
setTag

◆ 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

◆ 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
See also
setActive

◆ isEventListenerSuspended()

bool ime::Object::isEventListenerSuspended ( int  id) const
inherited

Check if an event listener is suspended or not.

Parameters
idThe identification number of the listener to be checked
Returns
True if suspended, otherwise false

This function also returns false if the specified event listener does not exist

See also
suspendedEventListener

◆ isSameObjectAs()

bool ime::Object::isSameObjectAs ( const Object other) const
inherited

Check if another object is the same instance as this object.

Parameters
otherThe object to compare against this object
Returns
True if other is the same instance as this object, otherwise false

◆ 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 unique id of the destruction listener

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

Warning
It's not advised to call virtual functions in the destruction callback as some parts of the object may have already been destroyed by the time the callback is invoked. In such an event, the behavior is undefined
See also
removeEventListener

◆ onPropertyChange() [1/2]

int ime::Object::onPropertyChange ( const Callback< Property > &  callback,
bool  oneTime = false 
)
inherited

Add an event listener to any property change event.

Parameters
callbackThe function to be executed when any property changes
oneTimeTrue to execute the callback one-time or false to execute it every time the event is triggered
Returns
The unique id of the event listener

When unsubscribing the any property change event handler, you must pass "propertyChange" as the name of the event

See also
onPropertyChange(const std::string&, const ime::Callback<ime::Property>&)

◆ onPropertyChange() [2/2]

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

Add an event listener to a specific property change event.

Parameters
propertyThe name of the property to add an event listener to
oneTimeTrue to execute the callback one-time or false to execute it every time the event is triggered
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, 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.

// 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");
See also
unsubscribe and onPropertyChange(const ime::Callback<ime::Property>&)

◆ onRigidBodyCollisionEnd()

int ime::GameObject::onRigidBodyCollisionEnd ( const CollisionCallback callback,
bool  oneTime = false 
)

Add an event listener to a rigid body collision end event.

Parameters
callbackThe function to be executed when the event is fired
oneTimeTrue to execute the callback one-time or false to execute it every time the event is triggered
Returns
The event listeners identification number

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

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 and onCollisionStay

◆ onRigidBodyCollisionStart()

int ime::GameObject::onRigidBodyCollisionStart ( const CollisionCallback callback,
bool  oneTime = false 
)

Add an event listener to a rigid body collision begin event.

Parameters
callbackThe function to be executed when the event is fired
oneTimeTrue to execute the callback one-time or false to execute it every time the event is triggered
Returns
The event listeners identification number

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

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, onCollisionStay and onCollisionEnd

◆ onRigidBodyCollisionStay()

int ime::GameObject::onRigidBodyCollisionStay ( const CollisionCallback callback,
bool  oneTime = false 
)

Add an event listener to a rigid body collision stay event.

Parameters
callbackThe function to be executed when the event is fired
oneTimeTrue to execute the callback one-time or false to execute it every time the event is triggered
Returns
The event listeners identification number

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

Note
A collision stay event can only occur if the game object has a rigid body attached to it and the rigid body is awake and has a Collider attached to it that is not a sensor
See also
attachRigidBody, onCollisionStart and onCollisionEnd

◆ operator=() [1/2]

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

Copy assignment operator.

◆ operator=() [2/2]

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

Move assignment operator.

◆ removeEventListener() [1/2]

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

Remove an event listener from an event.

Parameters
eventThe name of the event to remove an 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 tag of the object to console every time it changes
auto tagChangeId = object.onPropertyChange("tag", [](ime::Property tag) {
std::cout << name.getValue<std::string>() << std::endl;
});
// Stop displaying the tag of the object when it changes
object.removeEventListener("tag", tagChangeId);
Class that can store a value of any type.
Definition: Property.h:38

◆ removeEventListener() [2/2]

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

Remove an event listener.

Parameters
idThe id of the event listener to be removed
Returns
True if the event listener was removed or false if no such handler exists

◆ 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 or not the game object is active.

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

See also
isActive

◆ 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 by IME

◆ setTag()

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

Assign the object an alias.

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

See also
getObjectId

◆ suspendedEventListener()

void ime::Object::suspendedEventListener ( int  id,
bool  suspend 
)
inherited

Pause or resume execution of an event listener.

Parameters
idThe event listeners unique identification number
suspendTrue to suspend/pause or false to unsuspend/resume
See also
isEventListenerSuspended

◆ swap()

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

Swap the game object with another game object.

Parameters
otherThe game object to swap with this game object

◆ update()

virtual void ime::GameObject::update ( Time  deltaTime)
inlinevirtual

Update the game object.

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

Member Data Documentation

◆ eventEmitter_

EventEmitter ime::Object::eventEmitter_
protectedinherited

Event dispatcher.

Definition at line 289 of file Object.h.


The documentation for this class was generated from the following file: