Chira Engine
A customizable MIT-licensed game engine.
Scene.h
1 #pragma once
2 
3 #include <memory>
4 #include <string>
5 #include <unordered_map>
6 #include <entt/entt.hpp>
7 #include <math/Types.h>
8 #include "component/CameraComponent.h"
9 #include "component/NameComponent.h"
10 #include "component/UUIDComponent.h"
11 
12 namespace chira {
13 
14 class Entity;
15 
16 class Scene {
17  friend class Viewport;
18 
19 protected:
20  Scene();
21 
22  explicit Scene(const std::string& name);
23 
24 public:
25  ~Scene();
26 
27  Entity* addEntity();
28 
29  Entity* addEntity(const std::string& name);
30 
31  Entity* addEntity(uuids::uuid uuid);
32 
33  Entity* addEntity(uuids::uuid uuid, const std::string& name);
34 
35  [[nodiscard]] Entity* getEntity(uuids::uuid entityID);
36 
37  template<typename... T>
38  auto getEntities() const {
39  return this->getRegistry().view<T...>();
40  }
41 
42  template<typename... T>
43  auto getEntities(auto&& exclude) const {
44  return this->getRegistry().view<T...>(exclude);
45  }
46 
47  [[nodiscard]] const auto& getEntities() const {
48  return this->entities;
49  }
50 
51  [[nodiscard]] bool hasEntity(uuids::uuid entityID);
52 
53  void removeEntity(uuids::uuid entityID);
54 
55  template<typename T, typename... Args>
56  T& addComponent(Args&& ...args) {
57  static_assert(!CComponentHasTransform<T>, "Adding components to a scene that require a transform will break horribly!");
58  runtime_assert(!this->hasComponent<T>(), "Scene already has this component!");
59  return this->getRegistry().emplace<T>(this->handle, std::forward<Args>(args)...);
60  }
61 
62  template<typename T>
63  void addTagComponent() {
64  runtime_assert(!this->hasComponent<T>(), "Scene already has this component!");
65  this->getRegistry().emplace<T>(this->handle);
66  }
67 
68  template<typename T>
69  [[nodiscard]] T& getComponent() {
70  runtime_assert(this->hasComponent<T>(), "Scene doesn't have this component!");
71  return this->getRegistry().get<T>(this->handle);
72  }
73 
74  template<typename T>
75  [[nodiscard]] const T& getComponent() const {
76  runtime_assert(this->hasComponent<T>(), "Scene doesn't have this component!");
77  return this->getRegistry().get<T>(this->handle);
78  }
79 
80  template<typename T>
81  [[nodiscard]] T* tryGetComponent() {
82  return this->getRegistry().try_get<T>(this->handle);
83  }
84 
85  template<typename T>
86  [[nodiscard]] const T* tryGetComponent() const {
87  return this->getRegistry().try_get<T>(this->handle);
88  }
89 
90  template<typename T>
91  [[nodiscard]] bool hasComponent() const {
92  return this->getRegistry().all_of<T>(this->handle);
93  }
94 
95  template<typename T>
96  void tryRemoveComponent() {
97  this->getRegistry().remove<T>(this->handle);
98  }
99 
100  template<typename T>
101  void removeComponent() {
102  runtime_assert(this->hasComponent<T>(), "Scene doesn't have this component!");
103  this->getRegistry().erase<T>(this->handle);
104  }
105 
106  CameraComponent* getCamera();
107 
108  void setupForRender(glm::vec2i size);
109 
110  [[nodiscard]] entt::registry& getRegistry();
111 
112  [[nodiscard]] const entt::registry& getRegistry() const;
113 
114  [[nodiscard]] std::string getName() const {
115  if (auto nameComponent = this->getRegistry().try_get<NameComponent>(this->handle)) {
116  return nameComponent->name;
117  }
118  return uuids::to_string(this->getRegistry().get<UUIDComponent>(this->handle).uuid);
119  }
120 
121  [[nodiscard]] uuids::uuid getUUID() const {
122  return this->getRegistry().get<UUIDComponent>(this->handle).uuid;
123  }
124 
125  [[nodiscard]] entt::entity getRawHandle() const {
126  return this->handle;
127  }
128 
129  bool operator==(const Scene& other) const {
130  return this->handle == other.handle;
131  }
132 
133  bool operator!=(const Scene& other) const {
134  return !(*this == other);
135  }
136 
137 private:
138  std::unordered_map<uuids::uuid, std::unique_ptr<Entity>> entities;
139  entt::registry registry;
140  entt::entity handle;
141 };
142 
143 } // namespace chira
The base entity class.
Definition: Entity.h:17