From Cobalt Frog, 2 Months ago, written in Plain Text.
This paste will die in 8 Months.
Embed
  1. #ifndef ENTITY_HPP
  2. #define ENTITY_HPP
  3.  
  4. #include "Component.hpp"
  5. #include <string>
  6. #include <map>
  7. #include <typeindex>
  8.  
  9. class Entity {
  10.  
  11.     private:
  12.         std::string type;
  13.         std::map<std::type_index, Component*> components;
  14.         int entityID;
  15.     public:
  16.         ~Entity(){
  17.             for (auto& c : components) {
  18.                 delete c.second;
  19.             }
  20.         }
  21.         void addComponent(std::type_index type, Component* c){
  22.             components[type] = c;
  23.         }
  24.         template <typename T>
  25.         T* get() {
  26.             auto it = components.find(std::type_index(typeid(T)));
  27.             if (it != components.end()) {
  28.                 return dynamic_cast<T*>(it->second);
  29.             }
  30.             return nullptr;
  31.         }
  32.         void setType(const std::string& type) {
  33.             this->type = type;
  34.         }
  35.         std::string getType() const {
  36.             return type;
  37.         }
  38.  
  39. };
  40. #endif /* ENTITY_HPP */