TurtleBrains  0.3.5
High quality, portable, C++ framework for rapid 2D game development.
TurtleBrains::Game::Entity Class Reference

#include <tb_entity.hpp>

Inheritance diagram for TurtleBrains::Game::Entity:
TurtleBrains::Graphics::GraphicList TurtleBrains::Graphics::Graphic

Public Member Functions

 Entity (const EntityType &entityType)
 
virtual ~Entity (void)
 
EntityManagerGetEntityManager (void) const
 
void Simulate (void)
 
void ClearBehaviors (void)
 
void PushBehavior (EntityBehaviorInterface *entityBehavior)
 
void PopBehavior (void)
 
void PopBehaviorIf (EntityBehaviorInterface *entityBehavior)
 
void PopAllBehaviors (void)
 
const EntityBehaviorInterfaceGetActiveBehavior (void) const
 
EntityBehaviorInterfaceGetActiveBehavior (void)
 
bool IsCollidable (void) const
 
void SetCollidable (const bool isCollidable)
 
bool HasBoundingVolume (void) const
 
void ClearBoundingVolumes (void)
 
void AddBoundingCircle (const float circleRadius, const tbMath::Vector2 &centerOffset=tbMath::Vector2::Zero())
 
void AddBoundingBox (const float boxWidth, const float boxHeight, const tbMath::Vector2 &centerOffset=tbMath::Vector2::Zero())
 
bool CheckCollisionWith (const tbMath::Vector2 &point) const
 
bool CheckCollisionWith (const tbMath::Vector2 &center, const float radius) const
 
bool CheckCollisionWith (const tbMath::Vector2 &center, const float width, const float height) const
 
bool CheckCollisionWith (const Entity &otherEntity) const
 
const EntityTypeGetEntityType (void) const
 
const EntityTypeContainerGetEntityTypes (void) const
 
void AddEntityType (const EntityType &entityType)
 
void RemoveEntityType (const EntityType &entityType)
 
bool IsEntityOfType (const EntityType &entityType) const
 
- Public Member Functions inherited from TurtleBrains::Graphics::GraphicList
 GraphicList (void)
 
virtual ~GraphicList (void)
 
void ClearGraphics (void)
 
void AddGraphic (Graphic *graphic)
 
void AddGraphic (Graphic &graphic)
 
void RemoveGraphic (Graphic *graphic)
 
virtual void SetColor (const tbGraphics::Color &newColor) override
 
virtual PixelSpace GetPixelWidth (void) const
 
virtual PixelSpace GetPixelHeight (void) const
 
void RecalculateWidthAndHeight (void)
 
- Public Member Functions inherited from TurtleBrains::Graphics::Graphic
 Graphic (void)
 
 Graphic (const Graphic &other)
 
virtual ~Graphic (void)
 
bool IsActive (void) const
 
void SetActive (const bool isActive)
 
void Update (const float deltaTime)
 
bool IsVisible (void) const
 
void SetVisible (const bool isVisible)
 
void Render (void) const
 
const tbMath::Vector2GetPosition (void) const
 
void SetPosition (const float x, const float y)
 
void SetPosition (const tbMath::Vector2 &position)
 
const tbMath::Vector2GetOrigin (void) const
 
void SetOrigin (const AnchorLocation &anchor, const tbMath::Vector2 &offset=tbMath::Vector2::Zero())
 
void SetOrigin (const float originX, const float originY)
 
void SetOrigin (const tbMath::Vector2 &origin)
 
float GetDepth (void) const
 
void SetDepth (const float newDepth)
 
const tbMath::Vector2GetScale (void) const
 
void SetScale (const float scale)
 
void SetScale (const tbMath::Vector2 &scale)
 
void SetScale (const float scaleX, const float scaleY)
 
float GetRotation (void) const
 
void SetRotation (const float rotation)
 
void SetScroll (const float scroll)
 
void SetScroll (const float scrollX, const float scrollY)
 
bool IsRelative (void) const
 
const ColorGetColor (void) const
 
float GetWidth (void) const
 
float GetHeight (void) const
 
float GetScaledWidth (void) const
 
float GetScaledHeight (void) const
 
bool UnstableIsPointContained (const tbMath::Vector2 &point) const
 

Static Public Attributes

static const EntityType kInvalidType
 

Protected Member Functions

virtual void OnSimulate (void)
 
virtual void OnUpdate (const float deltaTime)
 
virtual void OnRender (void) const
 
virtual void OnDebugRender (void) const
 
virtual void OnCollide (const Entity &other)
 
virtual void OnAdd (void)
 
virtual void OnRemove (void)
 
- Protected Member Functions inherited from TurtleBrains::Graphics::Graphic
virtual void ApplyTransform (void) const
 
virtual void PopTransform (void) const
 

Friends

class EntityManager
 
class TileSystem
 

Detailed Description

The Entity object is a GraphicList to give it the ability to display itself with Graphic objects and the ability for subclasses to overload OnUpdate. Since entities will likely be performing game logic, Simulate and OnSimulate have been added to the interface to provide a fixed time step update that may be called 0 to N times per frame.

Constructor & Destructor Documentation

TurtleBrains::Game::Entity::Entity ( const EntityType entityType)
explicit

Constructs an empty entity object which contains no Graphics and position is defaults to 0,0 but has the specified type.

virtual TurtleBrains::Game::Entity::~Entity ( void  )
virtual

Destructs an entity which currently (TurtleBrains v0.2.0) does not have any resources to clean up after.

Member Function Documentation

void TurtleBrains::Game::Entity::AddBoundingBox ( const float  boxWidth,
const float  boxHeight,
const tbMath::Vector2 centerOffset = tbMath::Vector2::Zero() 
)

Adds a bounding Box to the entity to perform collisions and enables the entity to be collidable. Multiple bounding volumes can be added to the entity, and if any bodies collide the collision check will return true.

Note
The entity collision detector is very simple. Scaling the entity will NOT scale the bounding volumes and rotating the entity will NOT rotate the centerOffset of the bounding volume.
Adding a bounding volume with AddBoundingCircle or AddBoundingBox will cause the entity to become collidable.
void TurtleBrains::Game::Entity::AddBoundingCircle ( const float  circleRadius,
const tbMath::Vector2 centerOffset = tbMath::Vector2::Zero() 
)

Adds a bounding Circle to the entity to perform collisions and enables the entity to be collidable. Multiple bounding volumes can be added to the entity, and if any bodies collide the collision check will return true.

Note
The entity collision detector is very simple. Scaling the entity will NOT scale the bounding volumes and rotating the entity will NOT rotate the centerOffset of the bounding volume.
Adding a bounding volume with AddBoundingCircle or AddBoundingBox will cause the entity to become collidable.
void TurtleBrains::Game::Entity::AddEntityType ( const EntityType entityType)

Adds the entity type to the entity, in this way you can have multiple types for an entity. If the entityType is kInvalidType an error condition will be triggered.

bool TurtleBrains::Game::Entity::CheckCollisionWith ( const tbMath::Vector2 point) const

Checks to see if the Entity collides with the provided point in the same 2D space.

Parameters
pointThe point to check if it is contained within one of the bounding volumes for the entity.
Note
This will check collision with the point regardless of the IsCollidable state.
bool TurtleBrains::Game::Entity::CheckCollisionWith ( const tbMath::Vector2 center,
const float  radius 
) const

Checks to see if the entity's bounding volumes collide with the circle provided, if the entity does not have any bounding volumes, this will instead check if the position of the entity is inside the circle, returning true if it is, false otherwise.

Parameters
centerThe center of the bounding circle to check for a collision with the entities bounding volumes.
radiusThe radius of the bounding circle for the collision check, for most projects this will be in pixels but it is possible that units are different.
Note
This will check collision with the circle regardless of the IsCollidable state.
bool TurtleBrains::Game::Entity::CheckCollisionWith ( const tbMath::Vector2 center,
const float  width,
const float  height 
) const

Checks to see if the entity's bounding volumes collide with the axis-aligned box provided, if the entity does not have any bounding volumes, this will instead check if the position of the entity is inside the box, returning true if it is, false otherwise.

Parameters
centerThe center of the bounding box to check for a collision with the entities bounding volumes.
widthThe horizontal size of the bounding box for the collision check, for most projects this will be in pixels but it is possible that units are different.
heightThe vertical size of the bounding box for the collision check, for most projects this will be in pixels but it is possible that units are different.
Note
This will check collision with the axis-aligned box regardless of the IsCollidable state.
bool TurtleBrains::Game::Entity::CheckCollisionWith ( const Entity otherEntity) const

Checks to see if the Entity collides with the other entity provided.

Parameters
otherEntityThe Entity to check for collisions with.
Note
THis will check collision with the other entity regardless of this or the other entities IsCollidable state. If either this or the otherEntity does not have a bounding volume no collision will occur.
void TurtleBrains::Game::Entity::ClearBehaviors ( void  )

Removes/clears all the behaviors from the entities behavior stack, same as PopAllBehaviors().

Note
The resources of the behavior will be cleaned up by calling delete, but the delete may be delayed some amount of time after the call. This is to ensure it is safe to call PopBehavior() from within the behaviors OnSimulate() call without worry, however the behavior should be considered destroyed for any other purpose except for the OnSimulate delay.
void TurtleBrains::Game::Entity::ClearBoundingVolumes ( void  )

Removes all bounding volumes the entity may have and will make the entity no longer collidable.

Note
Removing all bounding volumes will cause the entity to be no longer be collidable.
const EntityBehaviorInterface* TurtleBrains::Game::Entity::GetActiveBehavior ( void  ) const

TODO: TurtleBrains: Documentation: Teach the user how to use this.

Note
This was implemented for a quick check and may be deprecated from official release.
EntityBehaviorInterface* TurtleBrains::Game::Entity::GetActiveBehavior ( void  )

TODO: TurtleBrains: Documentation: Teach the user how to use this.

Note
This was implemented for a quick check and may be deprecated from official release.
EntityManager* TurtleBrains::Game::Entity::GetEntityManager ( void  ) const

Returns the EntityManager the entity is managed by, which can be nullptr.

const EntityType& TurtleBrains::Game::Entity::GetEntityType ( void  ) const

Returns the EntityType of the Entity.

Note
This was implemented when entities only had a single type. For backwards compatibility it returns the first entity type that gets added, but this will eventually be removed, so start using IsEntityOfType().
const EntityTypeContainer& TurtleBrains::Game::Entity::GetEntityTypes ( void  ) const

Returns a container of all the EntityTypes for the Entity.

bool TurtleBrains::Game::Entity::HasBoundingVolume ( void  ) const

Returns true if the entity has a bounding volume.

bool TurtleBrains::Game::Entity::IsCollidable ( void  ) const

Returns true if the entity will perform collision automatically each frame, it must also contain a bounding volume for collision to occur, call AddBoundingCircle() or AddBoundingBox() to create the bounding volume for the collision.

Note
Adding a bounding volume with AddBoundingCircle or AddBoundingBox will cause the entity to become collidable.
bool TurtleBrains::Game::Entity::IsEntityOfType ( const EntityType entityType) const

Checks through the entity types that have been added to the Entity for a match, returning true if a match is found or false if no match was found.

virtual void TurtleBrains::Game::Entity::OnAdd ( void  )
protectedvirtual

This is called just after the Entity is added to an EntityManager, aka: the scene, so the entity can perform any logic before starting to perform in the game loop.

virtual void TurtleBrains::Game::Entity::OnCollide ( const Entity other)
protectedvirtual

This should be overridden by a subclass if it needs to perform any game logic when a collision occurs with another entity type. This will be invoked on both entities, so if you had a bullet and a target the following calls: target.OnCollide(bullet) and bullet.OnCollide(target) would be made in no particular order.

virtual void TurtleBrains::Game::Entity::OnDebugRender ( void  ) const
protectedvirtual

This should render the bounding volumes of the entity in a light red.

virtual void TurtleBrains::Game::Entity::OnRemove ( void  )
protectedvirtual

This is called just before the Entity is removed from an EntityManager, aka the scene, so that any logic or resource cleanup can be performed.

virtual void TurtleBrains::Game::Entity::OnRender ( void  ) const
protectedvirtual

Calls Render() on each Graphic object contained in the list which will then call OnRender() if the object is visible.

Note
This will currently trigger an error condition if the Graphic object is not relative to the parent location as this feature is currently unimplemented. TODO: TIM: Implementation: Needs to be implemented.

Reimplemented from TurtleBrains::Graphics::GraphicList.

virtual void TurtleBrains::Game::Entity::OnSimulate ( void  )
protectedvirtual

This should be overridden by a subclass if it needs to perform any game logic updates at a fixed time step for determinism. This function will be invoked when Simulate() is called and the entity IsActive(). This may be called from 0 to N times per frame.

Note
OnSimulate() should not be called directly, even from a subclass. Use Simulate() which will first check if the Entity IsActive() before simulating with OnSimulate().
virtual void TurtleBrains::Game::Entity::OnUpdate ( const float  deltaTime)
protectedvirtual

Calls Update() on each Graphic object contained in the list which will then call OnUpdate() if the object is active.

Reimplemented from TurtleBrains::Graphics::GraphicList.

void TurtleBrains::Game::Entity::PopAllBehaviors ( void  )

Removes/clears all the behaviors from the entities behavior stack, same as ClearBehaviors().

Note
The resources of the behavior will be cleaned up by calling delete, but the delete may be delayed some amount of time after the call. This is to ensure it is safe to call PopBehavior() from within the behaviors OnSimulate() call without worry, however the behavior should be considered destroyed for any other purpose except for the OnSimulate delay.
void TurtleBrains::Game::Entity::PopBehavior ( void  )

Pops the behavior at the top of the stack, and deletes the resources for it. Resumes the next Behavior which is now the active one and on top of the stack.

Note
The resources of the behavior will be cleaned up by calling delete, but the delete may be delayed some amount of time after the call. This is to ensure it is safe to call PopBehavior() from within the behaviors OnSimulate() call without worry, however the behavior should be considered destroyed for any other purpose except for the OnSimulate delay.
void TurtleBrains::Game::Entity::PopBehaviorIf ( EntityBehaviorInterface entityBehavior)

Checks if the address of the current behavior, at the top of the stack, matches the address supplied, and only if these match does the behavior get popped by calling PopBehavior().

Parameters
entityBehaviorThe behavior to check against the top of the stack and pop if they match.
Note
The resources of the behavior will be cleaned up by calling delete, but the delete may be delayed some amount of time after the call. This is to ensure it is safe to call PopBehavior() from within the behaviors OnSimulate() call without worry, however the behavior should be considered destroyed for any other purpose except for the OnSimulate delay.
void TurtleBrains::Game::Entity::PushBehavior ( EntityBehaviorInterface entityBehavior)

Push the behavior onto the Entity so it becomes the active behavior controlling the Entity and the current behavior gets paused until this is popped. The behavior should be allocated with new, and the Entity will call delete on it when finished.

Parameters
entityBehaviorThe behavior to push and activate on the top of the entities behavior stack. Must be allocated with new, as the Entity will call delete on the behavior later.
void TurtleBrains::Game::Entity::RemoveEntityType ( const EntityType entityType)

Finds and removes an EntityType from the entity so it no longer counts as that type.

void TurtleBrains::Game::Entity::SetCollidable ( const bool  isCollidable)

Sets a flag that when false causes the entity to skip automated collision checks within the entity manager and when true will check for collision and call OnCollide() when a collision event occurs. Defaults to false, and will lead to better performance if false when the object does not need to check collision with other objects.

Note
Adding a bounding volume with AddBoundingCircle or AddBoundingBox will cause the entity to become collidable, so make sure the bounding volumes are set before attempting to turn of collision.
void TurtleBrains::Game::Entity::Simulate ( void  )

First checks to see if the entity IsActive() and calls OnSimulate() if it is. If the entity is not active nothing happens.

Member Data Documentation

const EntityType TurtleBrains::Game::Entity::kInvalidType
static

TODO: TurtleBrains: Documentation: Teach the user how to use this. TODO: TIM: This may get deprecated and replaced with "properties" / "flags", or, multiple types.