Hummingbird Framework
 All Classes Functions Variables Modules Pages
hb::GameObject Class Reference

GameObject (also known as Actor). More...

#include <GameObject.h>

Inheritance diagram for hb::GameObject:
Collaboration diagram for hb::GameObject:

Classes

class  Component
 Base class for implementing custom Components. More...
 

Public Member Functions

 GameObject ()
 Default constructor.
 
 GameObject (const std::initializer_list< Component * > &components)
 Class constructor. Adds all given components to the new GameObject instance. More...
 
 ~GameObject ()
 Class destructor. More...
 
int getId () const
 Get the GameObject's id. More...
 
const std::string & getName () const
 Get the GameObject's name. More...
 
void setName (const std::string &name)
 Set the GameObject's name.
 
void setActive (bool active)
 Set if GameObject is active. More...
 
bool isActive () const
 Get if GameObject is active. More...
 
void preUpdate ()
 Runs all its Component's preUpdate().
 
void update ()
 Runs all its Component's update().
 
void postUpdate ()
 Runs all its Component's postUpdate().
 
void destroy ()
 Marks the GameObject to be destroyed. More...
 
void addComponent (Component *component)
 Add a Component to the GameObject.
 
void addComponents (const std::vector< Component * > &components)
 Add a group of Components to the GameObject.
 
template<typename ComponentType >
ComponentType * getComponent () const
 Get first Component of type ComponentType. More...
 
template<typename ComponentType >
std::vector< ComponentType * > getComponents () const
 Get all Components of type ComponentType. More...
 
void sendMessage (const std::string &name, DataRepository &data)
 Send message to this GameObject's Components.
 
- Public Member Functions inherited from hb::Transform
 Transform ()
 Default constructor. More...
 
 Transform (const Vector3d &init_pos)
 Constructor with initial position.
 
 ~Transform ()
 Class destructor.
 
const Vector3dgetPosition () const
 Get Transform position. More...
 
void setPosition (const Vector3d &p)
 Set position from a Vector3d. More...
 
void setPosition (double p_x, double p_y, double p_z)
 Set position from doubles. More...
 
const Vector3dgetRotation () const
 Get Transform rotation. More...
 
void setRotation (const Vector3d &rotation)
 Set rotation from a Vector3d. More...
 
void setRotation (double rotation_x, double rotation_y, double rotation_z)
 Set rotation from doubles. More...
 
const Vector3dgetScale () const
 Get Transform scale. More...
 
void setScale (const Vector3d &scale)
 Set scale from a Vector3d. More...
 
void setScale (double scale_x, double scale_y, double scale_z)
 Set position from doubles. More...
 

Static Public Member Functions

static GameObjectgetGameObjectById (int id)
 Get GameObject instance given its id. More...
 
static const std::vector
< GameObject * > & 
getGameObjectsByName (const std::string &name)
 Get vector of GameObjects with the given name. More...
 
static void destroyAll ()
 Destroy all existing GameObjects.
 
static void updateAll ()
 Run the Update for all existing GameObjects.
 

Advanced usage

Used for messing with the unique id system of the GameObjects.

static void setNextGameObjectId (int id)
 Set next GameObject id. More...
 
 GameObject (int id)
 Class constructor for manually setting the GameObject id.
 

Detailed Description

GameObject (also known as Actor).

This class creates a object in the Game. It, by default, doesn't have any behaviour and you should not inherit from it. Instead GameObjects are composed by Components.

These Components are the ones that must implement the behaviour of the GameObject composed by them.

GameObjects are identified by a unique id that is assigned to every GameObject created and you can get it by querying its id (see getGameObjectById()). They can also be grouped by name, that is, you can set the same name to a group of GameObjects and later you can get them all by querying their name (see getGameObjectsByName()).

The Update of a GameObject consists in three steps: pre-update, update and post-update. These are used for respectively running its Components pre-update, update and post-update functions. The GameObject will also send a message to its MessageManager with the name of the event ("pre-update", "update" and "post-update") with an empty DataRepository as the parameter.

To destroy a GameObject you must call destroy(), not its destructor. The GameObject then, will be marked to be destroyed and after the next update step it'll be deleted. Just before being deleted, the GameObject will send a message to its MessageManager with the name "destroy" and an empty DataRepository as the parameter.

A GameObject can be active or inactive. If a GameObject is inactive it exists, has a unique id and may be grouped by name; all its Components also exist and have been instantiated. But it won't be updated. Same applies to Components.

Constructor & Destructor Documentation

hb::GameObject::GameObject ( const std::initializer_list< Component * > &  components)

Class constructor. Adds all given components to the new GameObject instance.

Parameters
componentsList of components to be added to the new GameObject.
hb::GameObject::~GameObject ( )

Class destructor.

It takes care of the destruction of all Components assigned to the GameObject.

Member Function Documentation

void hb::GameObject::destroy ( )

Marks the GameObject to be destroyed.

The GameObject will be destroyed if it is marked to be destroyed after the update step.

template<typename ComponentType >
ComponentType* hb::GameObject::getComponent ( ) const
inline

Get first Component of type ComponentType.

Template Parameters
ComponentTypeType of the Component to be retrieved.
Returns
Pointer to a ComponentType instance, or nullptr if there is none.
template<typename ComponentType >
std::vector<ComponentType*> hb::GameObject::getComponents ( ) const
inline

Get all Components of type ComponentType.

Template Parameters
ComponentTypeType of the Components to be retrieved.
Returns
Vector of pointers to ComponentType instances, empty vector if none.
static GameObject* hb::GameObject::getGameObjectById ( int  id)
static

Get GameObject instance given its id.

Parameters
idGameObject id.
Returns
Pointer to GameObject instance.
static const std::vector<GameObject*>& hb::GameObject::getGameObjectsByName ( const std::string &  name)
static

Get vector of GameObjects with the given name.

Parameters
nameName of the GameObjects.
Returns
Vector of pointers to the GameObjects matching the given name.
int hb::GameObject::getId ( ) const

Get the GameObject's id.

Returns
The GameObject's id.
const std::string& hb::GameObject::getName ( ) const

Get the GameObject's name.

Returns
The GameObject's name.
bool hb::GameObject::isActive ( ) const

Get if GameObject is active.

Returns
Is GameObject active.
void hb::GameObject::setActive ( bool  active)

Set if GameObject is active.

Parameters
activeIs GameObject active.
static void hb::GameObject::setNextGameObjectId ( int  id)
static

Set next GameObject id.

Parameters
idNext GameObject id

The auto-increment of the GameObject unique id will start from id after this is set.


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