dEngine
Simple 2D C++ game engine
Public Member Functions | Public Attributes | Private Attributes | List of all members
dengine::GameObject Class Reference

A base object for a game The gameobject can be considered the most basic building block of a game. Everything in a game is considered a gameobject and it's functionality are determined by it's Components. More...

#include <GameObject.h>

Public Member Functions

 GameObject (std::string objectName)
 
std::string GetName ()
 Returns the object's name. More...
 
void AddComponent (Component *component)
 Adds a component to the GameObject. More...
 
void RemoveComponent (Component *component)
 Removes a component from the GameObject. More...
 
bool HasComponent (std::string name)
 Does it have a component? More...
 
virtual void Start ()
 Called on object instantiation. More...
 
virtual void NotifyCollision (GameObject &other)
 
virtual void Update ()
 Runs every frame performing any required logic. More...
 
virtual void Render ()
 Performs render on all attached components. More...
 
void SetPos (float xPos, float yPos)
 Set position. More...
 
std::weak_ptr< ComponentGetComponentByName (std::string name)
 Get a reference to a component. More...
 
void AddTag (std::string tag)
 Add a tag to the game object. More...
 
bool HasTag (std::string tag)
 Check if game object has a tag. More...
 
void RemoveTag (std::string tag)
 Remove tag from game object. More...
 

Public Attributes

SDL_Rect box
 A rect denoting the object's position in space. More...
 

Private Attributes

std::vector< class Component * > components
 A vector containing any components that may be on the GameObject. More...
 
std::string name
 A string containing the object name. More...
 
std::vector< std::string > tags
 

Detailed Description

A base object for a game The gameobject can be considered the most basic building block of a game. Everything in a game is considered a gameobject and it's functionality are determined by it's Components.

Constructor & Destructor Documentation

◆ GameObject()

GameObject::GameObject ( std::string  objectName)

Member Function Documentation

◆ AddComponent()

void GameObject::AddComponent ( Component component)

Adds a component to the GameObject.

Parameters
componentThe component to add to the object
See also
components

◆ AddTag()

void dengine::GameObject::AddTag ( std::string  tag)
inline

Add a tag to the game object.

◆ GetComponentByName()

std::weak_ptr< Component > GameObject::GetComponentByName ( std::string  name)

Get a reference to a component.

Parameters
nameName of the component to get
Returns
A weak pointer to the component on the gameobject
if(gameObject->HasComponent("Collider")){
Component* collider = gameObject->GetComponentByName("Collider").lock();
}

◆ GetName()

std::string GameObject::GetName ( )

Returns the object's name.

See also
name

Referenced by GetComponentByName().

◆ HasComponent()

bool GameObject::HasComponent ( std::string  name)

Does it have a component?

Checks if the gameobject has a component with the specified name

Parameters
nameThe name of the component to check
Returns
True if this gameobject has a component with the name name.

Referenced by GetComponentByName().

◆ HasTag()

bool dengine::GameObject::HasTag ( std::string  tag)
inline

Check if game object has a tag.

Returns
True if the game object has the tag

◆ NotifyCollision()

void GameObject::NotifyCollision ( GameObject other)
virtual

Notify the gameobject and any components of a collision

Parameters
otherThe other gameobject being collided with

◆ RemoveComponent()

void GameObject::RemoveComponent ( Component component)

Removes a component from the GameObject.

Parameters
componentThe component to remove
See also
components

◆ RemoveTag()

void dengine::GameObject::RemoveTag ( std::string  tag)
inline

Remove tag from game object.

◆ Render()

void GameObject::Render ( )
virtual

Performs render on all attached components.

◆ SetPos()

void GameObject::SetPos ( float  xPos,
float  yPos 
)

Set position.

Set the position of the game object

Parameters
xPosX position
yPosY position

◆ Start()

void GameObject::Start ( )
virtual

Called on object instantiation.

◆ Update()

void GameObject::Update ( )
virtual

Runs every frame performing any required logic.

Member Data Documentation

◆ box

SDL_Rect dengine::GameObject::box

A rect denoting the object's position in space.

Referenced by dengine::Sprite::Render(), dengine::SpriteSheet::Render(), SetPos(), and dengine::Collider::Update().

◆ components

std::vector<class Component *> dengine::GameObject::components
private

A vector containing any components that may be on the GameObject.

Referenced by AddComponent(), GetComponentByName(), HasComponent(), NotifyCollision(), RemoveComponent(), Render(), Start(), and Update().

◆ name

std::string dengine::GameObject::name
private

A string containing the object name.

Referenced by GameObject(), GetComponentByName(), GetName(), and HasComponent().

◆ tags

std::vector<std::string> dengine::GameObject::tags
private

Referenced by AddTag(), HasTag(), and RemoveTag().