Public Types | Public Member Functions | List of all members
ime::GameObjectContainer Class Reference

A container for GameObject instances. More...

#include <GameObjectContainer.h>

Inheritance diagram for ime::GameObjectContainer:
ime::ObjectContainer< GameObject >

Public Types

using Callback = std::function< void(Args...)>
 For each callback. More...
 
using Predicate = std::function< bool(const GameObject *)>
 Predicate callback. More...
 
using ObjectPtr = std::unique_ptr< GameObject >
 Unique Object pointer. More...
 

Public Member Functions

 GameObjectContainer (RenderLayerContainer &renderLayers)
 Constructor. More...
 
GameObjectadd (GameObject::Ptr gameObject, int renderOrder=0u, const std::string &renderLayer="default")
 Add a game object to the container. More...
 
template<typename T >
T * add (GameObject::Ptr gameObject, int renderOrder=0u, const std::string &renderLayer="default")
 Add a game object to the container. More...
 
GameObjectadd (const std::string &group, GameObject::Ptr gameObject, int renderOrder=0u, const std::string &renderLayer="default")
 Add a game object to a group in the container. More...
 
GameObjectaddObject (ObjectPtr object, const std::string &group="none")
 Add an object to the container. More...
 
GameObjectfindByTag (const std::string &tag)
 Get an object with a given tag. More...
 
const GameObjectfindByTag (const std::string &tag) const
 
U * findByTag (const std::string &tag)
 Get an object with a given tag. More...
 
const U * findByTag (const std::string &tag) const
 
GameObjectfindById (unsigned int id)
 Get an object with the given id. More...
 
const GameObjectfindById (unsigned int id) const
 
U * findById (unsigned int id)
 Get an object with the given id. More...
 
const U * findById (unsigned int id) const
 
GameObjectfindIf (const Predicate &predicate)
 Conditionally find an object in the container. More...
 
const GameObjectfindIf (const Predicate &predicate) const
 
void removeByTag (const std::string &tag)
 Remove all objects with the given tag. More...
 
void removeById (unsigned int id)
 Remove an object with the given id. More...
 
bool remove (GameObject *object)
 Remove an object from the container. More...
 
void removeIf (const Predicate &predicate)
 Conditionally remove objects from the container. More...
 
void removeAll ()
 Remove all objects from the container. More...
 
std::size_t getCount () const
 Get the number of objects in the container. More...
 
ObjectContainer< GameObject > & createGroup (const std::string &name)
 Create a group to add objects to. More...
 
ObjectContainer< GameObject > & getGroup (const std::string &name) const
 Get a group in the container. More...
 
bool hasGroup (const std::string &name) const
 Check whether or not the container has a given group. More...
 
bool removeGroup (const std::string &name)
 Remove a group from the container. More...
 
void removeAllGroups ()
 Remove all groups from the container. More...
 
void forEach (const Callback< GameObject * > &callback) const
 Apply a callback function to each object in the container. More...
 
void forEachInGroup (const std::string &name, const Callback< GameObject * > &callback) const
 Apply a callback to each object in a specific group. More...
 
void forEachInGroups (const std::initializer_list< std::string > &groups, const Callback< GameObject * > &callback) const
 Apply a callback to each object in specific groups. More...
 
void forEachNotInGroup (const Callback< GameObject * > &callback) const
 Apply a callback to all objects that do not belong to a group. More...
 

Detailed Description

A container for GameObject instances.

Definition at line 37 of file GameObjectContainer.h.

Member Typedef Documentation

◆ Callback

using ime::ObjectContainer< GameObject >::Callback = std::function<void(Args...)>
inherited

For each callback.

Definition at line 44 of file ObjectContainer.h.

◆ ObjectPtr

using ime::ObjectContainer< GameObject >::ObjectPtr = std::unique_ptr<GameObject >
inherited

Unique Object pointer.

Definition at line 47 of file ObjectContainer.h.

◆ Predicate

using ime::ObjectContainer< GameObject >::Predicate = std::function<bool(const GameObject *)>
inherited

Predicate callback.

Definition at line 46 of file ObjectContainer.h.

Constructor & Destructor Documentation

◆ GameObjectContainer()

ime::GameObjectContainer::GameObjectContainer ( RenderLayerContainer renderLayers)
explicit

Constructor.

Parameters
renderLayersThe render layer container for

Member Function Documentation

◆ add() [1/3]

GameObject * ime::GameObjectContainer::add ( const std::string &  group,
GameObject::Ptr  gameObject,
int  renderOrder = 0u,
const std::string &  renderLayer = "default" 
)

Add a game object to a group in the container.

Parameters
groupThe group to assign the game object to
gameObjectThe game object to be added
renderOrderThe render order of the game object
renderLayerThe render layer the game object belongs to
Returns
A pointer to the object in the container
  • If the render layer is unspecified or the specified layer cannot be found then the game object will be added to the default layer. The default layer is created by the Scene when you instantiate it. Note that the default layer may be deleted from the scenes render layers, however you must make sure that the layer you specify during a call to this function already exists otherwise undefined behavior

◆ add() [2/3]

GameObject * ime::GameObjectContainer::add ( GameObject::Ptr  gameObject,
int  renderOrder = 0u,
const std::string &  renderLayer = "default" 
)

Add a game object to the container.

Parameters
gameObjectThe game object to be added
renderOrderThe render order of the game object
renderLayerThe render layer the game object belongs to
Returns
A pointer to the game object after its added to the container

If the render layer is unspecified or the specified layer cannot be found then the game object will be added to the default layer. The default layer is created by the Scene when you instantiate it. Note that the default layer may be deleted from the scenes render layers, however you must make sure that the layer you specify during a call to this function already exists otherwise undefined behavior

◆ add() [3/3]

template<typename T >
T * ime::GameObjectContainer::add ( GameObject::Ptr  gameObject,
int  renderOrder = 0u,
const std::string &  renderLayer = "default" 
)
inline

Add a game object to the container.

Parameters
gameObjectThe game object to be added
renderOrderThe render order of the game object
renderLayerThe render layer the game object belongs to
Returns
A pointer casted to type T after the or a nullptr if the object is not convertible to T

If the render layer is unspecified or the specified layer cannot be found then the game object will be added to the default layer. The default layer is created by the Scene when you instantiate it. Note that the default layer may be deleted from the scenes render layers, however you must make sure that the layer you specify during a call to this function already exists otherwise undefined behavior

Definition at line 80 of file GameObjectContainer.h.

◆ addObject()

GameObject * ime::ObjectContainer< GameObject >::addObject ( ObjectPtr  object,
const std::string &  group = "none" 
)
inherited

Add an object to the container.

Parameters
objectThe object to be added
groupThe name of the group to add the object to
Returns
A pointer to the object after its added to the container

If the group is not found, it will be created and the object will be the first member of it. Groups are useful if you want to refer to objects that are the same or similar as a whole

By default the object does not belong to any group

See also
createGroup

◆ createGroup()

ObjectContainer< GameObject > & ime::ObjectContainer< GameObject >::createGroup ( const std::string &  name)
inherited

Create a group to add objects to.

Parameters
nameThe name of the group
Returns
The created group

This function is useful if you want to relate some objects and refer to them as a whole using a common group name instead of using a common tag name and looping though the container to find which objects have a given tag. Note that the name of the group must be unique

auto weapons = objectContainer.createGroup("weapons");
weapons.addObject(knife);
weapons.addObject(machete);
weapons.addObject(bat);
See also
addObject

◆ findById() [1/2]

GameObject * ime::ObjectContainer< GameObject >::findById ( unsigned int  id)
inherited

Get an object with the given id.

Parameters
idThe id of the object to be retrieved
Returns
The object with the given id or a nullptr if the object could not be found in the container

◆ findById() [2/2]

U * ime::ObjectContainer< GameObject >::findById ( unsigned int  id)
inherited

Get an object with the given id.

Parameters
idThe id of the object to be retrieved
Returns
The object with the given id or a nullptr if the object could not be found in the container or the the object is found but it is not convertible to type U

You can use this function to get the derived class type U if T is a base class

// The type of rectangle is ime::Shape*
auto rectangle = shapeContainer.findById(4);
// The type of rectangle2 is ime::RectangleShape*
auto rectangle2 = shapeContainer.findById<ime::RectangleShape>(4);
A 2D shape having four sides and four corners (90 degree angles)

◆ findByTag() [1/2]

GameObject * ime::ObjectContainer< GameObject >::findByTag ( const std::string &  tag)
inherited

Get an object with a given tag.

Parameters
tagThe tag of the object to be searched
Returns
The object with the given tag or a nullptr if the object could not be found in the container

Note that this function will return the first object it finds with the the given tag if the container has multiple objects with the same tag

◆ findByTag() [2/2]

U * ime::ObjectContainer< GameObject >::findByTag ( const std::string &  tag)
inherited

Get an object with a given tag.

Parameters
tagThe tag of the object to be searched
Returns
The object with the given tag or a nullptr if the object could not be found in the container or the the object is found but it is not convertible to type U

Note that this function will return the first object it finds with the the given tag if the container has multiple objects with the same tag. You can use this function to get the derived class type U if T is a base class:

// The type of rectangle is ime::Shape*
auto* rectangle = shapeContainer.findByTag("myRect");
// The type of rectangle2 is ime::RectangleShape*
auto* rectangle2 = shapeContainer.findByTag<ime::RectangleShape>("myRect");

◆ findIf()

GameObject * ime::ObjectContainer< GameObject >::findIf ( const Predicate predicate)
inherited

Conditionally find an object in the container.

Parameters
predicateA function which returns true if the object should be returned or false if the search should continue
Returns
The object that matches the found condition or a nullptr if an object that matches the success condition could not be found in the container

◆ forEach()

void ime::ObjectContainer< GameObject >::forEach ( const Callback< GameObject * > &  callback) const
inherited

Apply a callback function to each object in the container.

Parameters
callbackThe function to be applied to each object

Note that the callback is applied to all objects, this includes those that are assigned to groups

See also
forEachInGroup and forEachNotInGroup

◆ forEachInGroup()

void ime::ObjectContainer< GameObject >::forEachInGroup ( const std::string &  name,
const Callback< GameObject * > &  callback 
) const
inherited

Apply a callback to each object in a specific group.

Parameters
nameThe name of the group to apply callback on
callbackThe function to be applied to each object in the group

This function is a shortcut for:

// It's undefined behaviour to call getGroup if the group does not exist
if (container.hasGroup(name)) {
container.getGroup(name)->forEach(...);
}
See also
forEach and forEachNotInGroup

◆ forEachInGroups()

void ime::ObjectContainer< GameObject >::forEachInGroups ( const std::initializer_list< std::string > &  groups,
const Callback< GameObject * > &  callback 
) const
inherited

Apply a callback to each object in specific groups.

Parameters
groupsThe name of the groups to apply the callback on
callbackThe function to be applied to each object in the groups

For example, the following code deactivates all the game objects in the groups "bombs" and "guns":

gameObjects.forEachInGroups({"bombs", "guns"}, [](ime::GameObject* object) {
object->setActive(false);
});
Class for modelling game objects (players, enemies etc...)
Definition: GameObject.h:42
See also
forEach, forEachInGroup, ForEachNotInGroup

◆ forEachNotInGroup()

void ime::ObjectContainer< GameObject >::forEachNotInGroup ( const Callback< GameObject * > &  callback) const
inherited

Apply a callback to all objects that do not belong to a group.

Parameters
callbackThe function to be applied to each object in the container that does not belong to any group
See also
forEach and forEachInGroup

◆ getCount()

std::size_t ime::ObjectContainer< GameObject >::getCount ( ) const
inherited

Get the number of objects in the container.

Returns
The number of objects in the container

◆ getGroup()

ObjectContainer< GameObject > & ime::ObjectContainer< GameObject >::getGroup ( const std::string &  name) const
inherited

Get a group in the container.

Parameters
nameThe name of the group
Returns
The requested group
Warning
The specified group must exist in the container before this function is called otherwise undefined behavior
See also
createGroup and hasGroup

◆ hasGroup()

bool ime::ObjectContainer< GameObject >::hasGroup ( const std::string &  name) const
inherited

Check whether or not the container has a given group.

Parameters
nameThe name of the group to be checked
Returns
True if the container has the specified group, otherwise false
See also
createGroup

◆ remove()

bool ime::ObjectContainer< GameObject >::remove ( GameObject object)
inherited

Remove an object from the container.

Parameters
objectThe object to be removed
Returns
True if the object was removed or false if the object does not exist in the container
Warning
The argument must not be a nullptr

◆ removeAll()

void ime::ObjectContainer< GameObject >::removeAll ( )
inherited

Remove all objects from the container.

Warning
This function will invalidate any pointer(s) to the objects once they are removed from the container

◆ removeAllGroups()

void ime::ObjectContainer< GameObject >::removeAllGroups ( )
inherited

Remove all groups from the container.

This function will remove all objects that belong to a group from the container, leaving only objects that do not belong to a group if any. Note that pointers to the removed objects will be invalidated

See also
removeGroup

◆ removeById()

void ime::ObjectContainer< GameObject >::removeById ( unsigned int  id)
inherited

Remove an object with the given id.

Parameters
idThe id of the object to be removed
Warning
This function will invalidate any pointer(s) to the object once it is removed from the container

◆ removeByTag()

void ime::ObjectContainer< GameObject >::removeByTag ( const std::string &  tag)
inherited

Remove all objects with the given tag.

Parameters
tagThe tag of the objects to be removed
Warning
This function will invalidate any pointer(s) to the object once it is removed from the container

◆ removeGroup()

bool ime::ObjectContainer< GameObject >::removeGroup ( const std::string &  name)
inherited

Remove a group from the container.

Parameters
nameThe name of the group to be removed
Returns
True if the group was removed or false if the specified group does not exist in the container
Warning
This function will remove all objects in the given group from the container, therefore any pointers to the objects will be invalidated
See also
createGroup and removeAllGroups

◆ removeIf()

void ime::ObjectContainer< GameObject >::removeIf ( const Predicate predicate)
inherited

Conditionally remove objects from the container.

Parameters
predicateA function which returns true if the object should be removed or false if it should not be removed from the container

Note that this function will remove all objects for which the predicate return true

Warning
This function will invalidate any pointer(s) to the object once it is removed from the container

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