CS4850 Final Project
Loading...
Searching...
No Matches
GameEntity.hpp
1#pragma once
2
3#include <SDL2/SDL.h>
4#include <memory>
5#include <map>
6#include <functional>
7#include <string>
8#include <vector>
9
10#include "Component.hpp"
11#include "TextureComponent.hpp"
12#include "TransformComponent.hpp"
13#include "Collision2DComponent.hpp"
14
19struct GameEntity : public std::enable_shared_from_this<GameEntity> {
20 GameEntity();
21
27 GameEntity(const GameEntity& entity);
28
29 virtual ~GameEntity();
30
36 virtual void Input(float deltaTime);
37
43 virtual void Update(float deltaTime);
44
50 virtual void Render(SDL_Renderer* renderer);
51
57 template <typename T>
58 void AddComponent(std::shared_ptr<T> component);
59
67 bool HasComponent(ComponentType type);
68
76 template <typename T>
77 std::shared_ptr<T> GetComponent(ComponentType type);
78
84
90 std::shared_ptr<TransformComponent> GetTransform();
91
97 void SetRenderable(bool value) {
98 //SDL_Log("DEBUG: Setting renderable to %d", value);
99 mRenderable = value;
100 }
101
108 bool IsRenderable() const { return mRenderable; }
109
117 bool Intersects(std::shared_ptr<GameEntity> e);
118
123 void OnStart();
124
130 void AddTag(std::string tag) { mTags.push_back(tag); }
131
137 std::vector<std::string> GetTags() const { return mTags; }
138
144 void SetOnStart(std::function<void()> onStart);
145
151 void SetOnInput(std::function<void(float)> onInput);
152
158 void SetOnUpdate(std::function<void(float)> onUpdate);
159
160 protected:
161 std::vector<std::string> mTags; // Tags for identifying the entity
162 std::map<ComponentType, std::shared_ptr<Component>> mComponents;
163 bool mRenderable{true};
164 std::function<void()> mOnStart = nullptr;
165 std::function<void(float)> mOnInput = nullptr;
166 std::function<void(float)> mOnUpdate = nullptr;
167};
168
169
170
171
GameEntity class that represents an entity in the game.
Definition GameEntity.hpp:19
GameEntity(const GameEntity &entity)
Copy constructor to create a new entity from an existing one.
void AddComponent(std::shared_ptr< T > component)
Add a component to the entity.
void OnStart()
Run when the entity is started.
bool HasComponent(ComponentType type)
Check if the entity has a component of a certain type.
void AddDefaultTransformComponent()
Add a default transform component to the entity.
void SetOnInput(std::function< void(float)> onInput)
Set the function to be called when the entity receives input.
std::shared_ptr< TransformComponent > GetTransform()
Get the transform component of the entity.
virtual void Input(float deltaTime)
Handle input.
virtual void Update(float deltaTime)
Update the entity.
void AddTag(std::string tag)
Add a tag to the entity for identification.
Definition GameEntity.hpp:130
std::shared_ptr< T > GetComponent(ComponentType type)
Get a component of a certain type.
void SetOnUpdate(std::function< void(float)> onUpdate)
Set the function to be called when the entity is updated.
bool IsRenderable() const
Check if the entity is renderable.
Definition GameEntity.hpp:108
void SetOnStart(std::function< void()> onStart)
Set the function to be called when the entity is started.
std::vector< std::string > GetTags() const
Get the tags of the entity.
Definition GameEntity.hpp:137
virtual void Render(SDL_Renderer *renderer)
Render the entity.
bool Intersects(std::shared_ptr< GameEntity > e)
Check if the entity intersects with another entity. Only checks for collision2D components.
void SetRenderable(bool value)
Set the renderable flag.
Definition GameEntity.hpp:97