My Project
Loading...
Searching...
No Matches
Public Member Functions | Protected Attributes | List of all members
GameEntity Class Reference

The GameEntity class serves as a base class for all entities in the game, providing basic functionalities like input handling, updating, and rendering. More...

#include <GameEntity.h>

Inheritance diagram for GameEntity:
BackGroundGameEntity EnemyGameEntity FoodGameEntity GroundGameEntity PlayerGameEntity

Public Member Functions

 GameEntity ()=default
 Default constructor for GameEntity.
 
virtual ~GameEntity ()
 Virtual destructor for GameEntity.
 
virtual void Input (float deltaTime)
 Handles input for the entity.
 
virtual void Update (float deltaTime)
 Updates the entity and its components.
 
virtual void Render (SDL_Renderer *renderer)
 Renders the entity and its components.
 
void SetRenderable (bool value)
 Sets the entity's renderable state.
 
bool IsRenderable () const
 Checks if the entity is renderable.
 
template<typename T , typename... Args>
T * AddComponent (Args &&...args)
 Adds a component to the entity.
 
template<typename T >
TGetComponent ()
 Retrieves the first component of type T.
 
template<typename T >
const TGetComponent () const
 Retrieves the first component of type T, const version.
 
bool Intersects (const GameEntity *e) const
 Checks if the entity intersects with another entity.
 
SDL_Rect ConvertFRectToRect (const SDL_FRect &frect) const
 Converts an SDL_FRect to SDL_Rect by casting float values to ints.
 

Protected Attributes

std::vector< std::shared_ptr< Component > > components
 
bool mRenderable {true}
 

Detailed Description

The GameEntity class serves as a base class for all entities in the game, providing basic functionalities like input handling, updating, and rendering.

This class is designed to be extended by specific entities in the game. It holds a collection of components that define the entity's behavior and state. The class supports adding, retrieving, and updating components.

Member Function Documentation

◆ AddComponent()

template<typename T , typename... Args>
T * GameEntity::AddComponent ( Args &&... args)
inline

Adds a component to the entity.

Parameters
argsArguments to pass to the component's constructor.
Returns
A pointer to the newly added component.

This method creates a component of type T, adds it to the entity's component list, and returns a pointer to it.

◆ ConvertFRectToRect()

SDL_Rect GameEntity::ConvertFRectToRect ( const SDL_FRect & frect) const
inline

Converts an SDL_FRect to SDL_Rect by casting float values to ints.

Parameters
frectThe floating point rectangle to convert.
Returns
An SDL_Rect with the converted values.

Utility method for converting between SDL rectangle types.

◆ GetComponent() [1/2]

template<typename T >
T * GameEntity::GetComponent ( )
inline

Retrieves the first component of type T.

Returns
A pointer to the component of type T, or nullptr if not found.

This method searches for a component of type T in the entity's component list and returns a pointer to it.

◆ GetComponent() [2/2]

template<typename T >
const T * GameEntity::GetComponent ( ) const
inline

Retrieves the first component of type T, const version.

Returns
A const pointer to the component of type T, or nullptr if not found.

This method searches for a component of type T in the entity's component list and returns a const pointer to it.

◆ Input()

virtual void GameEntity::Input ( float deltaTime)
inlinevirtual

Handles input for the entity.

Parameters
deltaTimeThe time since the last update.

This method is meant to be overridden by derived classes to handle specific input actions.

Reimplemented in BackGroundGameEntity, EnemyGameEntity, FoodGameEntity, GroundGameEntity, and PlayerGameEntity.

◆ Intersects()

bool GameEntity::Intersects ( const GameEntity * e) const
inline

Checks if the entity intersects with another entity.

Parameters
eThe other entity to check intersection with.
Returns
True if there is an intersection, false otherwise.

This method checks if the bounding rectangles of the sprite components of two entities intersect.

◆ IsRenderable()

bool GameEntity::IsRenderable ( ) const
inline

Checks if the entity is renderable.

Returns
True if the entity should be rendered, false otherwise.

◆ Render()

virtual void GameEntity::Render ( SDL_Renderer * renderer)
inlinevirtual

Renders the entity and its components.

Parameters
rendererThe SDL renderer to draw to.

If the entity is marked as renderable, iterates over all components and renders each one.

Reimplemented in BackGroundGameEntity, EnemyGameEntity, FoodGameEntity, GroundGameEntity, and PlayerGameEntity.

◆ SetRenderable()

void GameEntity::SetRenderable ( bool value)
inline

Sets the entity's renderable state.

Parameters
valueTrue if the entity should be rendered, false otherwise.

◆ Update()

virtual void GameEntity::Update ( float deltaTime)
inlinevirtual

Updates the entity and its components.

Parameters
deltaTimeThe time since the last update.

Iterates over all components and updates each one. Can be overridden by derived classes for additional update logic.

Reimplemented in BackGroundGameEntity, EnemyGameEntity, FoodGameEntity, GroundGameEntity, and PlayerGameEntity.


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