Chira Engine
A customizable MIT-licensed game engine.
Scene.cpp
1 #include "Scene.h"
2 
3 #include <core/Assertions.h>
4 #include <render/shader/UBO.h>
5 #include "component/TagComponents.h"
6 #include "component/TransformComponent.h"
7 #include "Entity.h"
8 
9 using namespace chira;
10 
11 Scene::Scene()
12  : handle(this->getRegistry().create()) {
13  this->getRegistry().emplace<UUIDComponent>(this->handle);
14  this->getRegistry().emplace<SceneTagComponent>(this->handle);
15 
16  //this->getRegistry().on_construct<AngelScriptComponent>().connect<&AngelScriptComponent::onConstruct>();
17  //this->getRegistry().on_destroy<AngelScriptComponent>().connect<&AngelScriptComponent::onDestroy>();
18 }
19 
20 Scene::Scene(const std::string& name)
21  : Scene() {
22  this->getRegistry().emplace<NameComponent>(this->handle, name);
23 }
24 
25 Scene::~Scene() {
26  this->getRegistry().clear();
27 
28  //this->getRegistry().on_construct<AngelScriptComponent>().disconnect<&AngelScriptComponent::onConstruct>();
29  //this->getRegistry().on_destroy<AngelScriptComponent>().disconnect<&AngelScriptComponent::onDestroy>();
30 }
31 
32 Entity* Scene::addEntity() {
33  auto uuid = UUIDGenerator::getNewUUID();
34  return this->addEntity(uuid);
35 }
36 
37 Entity* Scene::addEntity(const std::string& name) {
38  auto* entity = this->addEntity();
39  entity->addComponent<NameComponent>(name);
40  return entity;
41 }
42 
43 Entity* Scene::addEntity(uuids::uuid uuid) {
44  runtime_assert(!this->entities.contains(uuid), "Entity UUID is already present!");
45  this->entities[uuid] = std::unique_ptr<Entity>{new Entity{this}};
46  auto& entity = this->entities.at(uuid);
47  entity->addComponent<UUIDComponent>(uuid);
48  return entity.get();
49 }
50 
51 Entity* Scene::addEntity(uuids::uuid uuid, const std::string& name) {
52  auto* entity = this->addEntity(uuid);
53  entity->addComponent<NameComponent>(name);
54  return entity;
55 }
56 
57 Entity* Scene::getEntity(uuids::uuid entityID) {
58  if (!this->hasEntity(entityID)) {
59  return nullptr;
60  }
61  return this->entities.at(entityID).get();
62 }
63 
64 bool Scene::hasEntity(uuids::uuid entityID) {
65  return this->entities.contains(entityID);
66 }
67 
68 void Scene::removeEntity(uuids::uuid entityID) {
69  runtime_assert(this->hasEntity(entityID), "Trying to remove an entity with a UUID that doesn't exist!");
70  this->getRegistry().destroy(this->getEntity(entityID)->getRawHandle());
71  this->entities.erase(entityID);
72 }
73 
74 CameraComponent* Scene::getCamera() {
75  auto cameraView = this->getRegistry().view<CameraComponent>();
76  for (auto [entity, cameraComponent] : cameraView.each()) {
77  if (cameraComponent.active) {
78  return &cameraComponent;
79  }
80  }
81  return nullptr;
82 }
83 
84 void Scene::setupForRender(glm::vec2i size) {
85  const auto* camera = this->getCamera();
86  if (!camera)
87  return;
88 
89  const TransformComponent* transform = camera->transform;
90  PerspectiveViewUBO::get().update(
91  camera->getProjection(size),
92  camera->getView(),
93  transform->getPosition(),
94  transform->getFrontVector());
95 }
96 
97 entt::registry& Scene::getRegistry() {
98  return this->registry;
99 }
100 
101 const entt::registry& Scene::getRegistry() const {
102  return this->registry;
103 }
The base entity class.
Definition: Entity.h:17