Simple C++ Game
Entity.hpp
1 #pragma once
2 
3 #include <string>
4 #include <map>
5 #include <memory>
6 
13 class Entity
14 {
15 public:
26  class Component
27  {
28  public:
34  Component(const std::string id);
35 
39  virtual ~Component();
40 
44  const std::string getId() const;
45 
46  private:
47  const std::string _id;
48  };
49 
53  Entity();
54 
60  Entity(int id);
61 
67  int getId() const;
68 
77  void add(const std::shared_ptr<Component> &component);
78 
86  bool hasComponent(const std::string id) const;
87 
94  std::shared_ptr<Component> getComponent(const std::string id) const;
95 
96 private:
97  int _id;
98  std::map<std::string, std::shared_ptr<Component>> _components;
99 };
A component of an entity in the game's component entity system.
Definition: Entity.hpp:27
virtual ~Component()
This is only declared to make the class polymorphic so that dymaic_cast between Component types works...
const std::string getId() const
Get the ID of the component. This will return the same value for all instances of a class of Componen...
Component(const std::string id)
Construct a new Component object.
An entity in the game's component entity system.
Definition: Entity.hpp:14
int getId() const
Get the ID of the Entity.
bool hasComponent(const std::string id) const
Check if the Entity has a component of the specified type.
std::shared_ptr< Component > getComponent(const std::string id) const
Get the Component object.
void add(const std::shared_ptr< Component > &component)
Adds the provided component to the entity.
Entity()
Construct a new Entity with a random ID.
Entity(int id)
Construct a new Entity object with a specific ID.