NasNas
An intuitive and beginner friendly 2D game framework for C++
EntityObject.hpp
1 // Created by Modar Nasser on 28/08/2021.
2 
3 #pragma once
4 
5 #include <memory>
6 
7 #include <NasNas/ecs/Storage.hpp>
8 #include <NasNas/ecs/Registry.hpp>
9 
10 namespace ns {
11  class EntityObject {
12  ecs::Entity id;
13  protected:
14  inline auto getId() const -> ecs::Entity { return id; }
15  public:
16  EntityObject();
17  virtual ~EntityObject();
18 
19  template <class TComp, typename ...Targs>
20  TComp& add(Targs ...args) {
21  return Ecs.attach<TComp>(id, args...);
22  }
23 
24  template <typename TComp>
25  void remove() {
26  Ecs.detach<TComp>(id);
27  }
28 
29  template <typename TComp>
30  auto has() const -> bool {
31  return Ecs.has<TComp>(id);
32  }
33 
34  template <typename TComp>
35  auto get() const -> TComp& {
36  if (has<TComp>()) {
37  return Ecs.get<TComp>(id);
38  }
39  throw std::runtime_error("Entity " + std::to_string(id) + " does not have component " + typeid(TComp).name());
40  }
41 
42  };
43 }