engine/src/scene.cpp

68 lines
1.6 KiB
C++
Raw Normal View History

2023-05-01 13:13:35 +00:00
#include "scene.h"
2022-11-30 00:46:03 +00:00
2023-05-01 13:13:35 +00:00
#include "components/transform.h"
#include "components/renderable.h"
#include "components/collider.h"
2023-05-25 17:35:32 +00:00
#include "components/custom.h"
2023-05-01 13:13:35 +00:00
#include "systems/transform.h"
#include "systems/render.h"
#include "systems/collisions.h"
2023-05-25 17:35:32 +00:00
#include "systems/custom_behaviour.h"
2022-12-20 23:51:04 +00:00
2022-11-30 00:46:03 +00:00
namespace engine {
2023-05-25 17:35:32 +00:00
Scene::Scene(Application* app) : app_(app) {
// event system
event_system_ = std::make_unique<EventSystem>();
2023-05-25 17:35:32 +00:00
// ecs configuration:
2023-05-25 17:35:32 +00:00
RegisterComponent<TransformComponent>();
RegisterComponent<RenderableComponent>();
RegisterComponent<ColliderComponent>();
RegisterComponent<CustomComponent>();
2023-01-18 14:42:09 +00:00
2023-05-25 17:35:32 +00:00
// Order here matters:
RegisterSystem<TransformSystem>();
RegisterSystem<PhysicsSystem>();
RegisterSystem<RenderSystem>();
RegisterSystem<CustomBehaviourSystem>();
}
2022-11-30 10:36:50 +00:00
2023-05-25 17:35:32 +00:00
Scene::~Scene() {}
2023-01-02 17:24:20 +00:00
2023-05-25 17:35:32 +00:00
uint32_t Scene::CreateEntity(const std::string& tag, uint32_t parent) {
uint32_t id = next_entity_id_++;
2023-01-02 17:24:20 +00:00
2023-05-25 17:35:32 +00:00
signatures_.emplace(id, std::bitset<kMaxComponents>{});
2023-01-02 21:11:29 +00:00
2023-05-25 17:35:32 +00:00
auto t = AddComponent<TransformComponent>(id);
2023-01-02 21:11:29 +00:00
2023-05-25 17:35:32 +00:00
t->position = {0.0f, 0.0f, 0.0f};
t->rotation = {};
t->scale = {1.0f, 1.0f, 1.0f};
2023-01-02 17:24:20 +00:00
2023-05-25 17:35:32 +00:00
t->tag = tag;
t->parent = parent;
2023-01-02 17:24:20 +00:00
2023-05-25 17:35:32 +00:00
return id;
}
2023-01-02 17:24:20 +00:00
2023-05-25 17:35:32 +00:00
uint32_t Scene::getEntity(const std::string& tag, uint32_t parent) {
return GetSystem<TransformSystem>()->GetChildEntity(parent, tag);
}
2023-05-25 17:35:32 +00:00
size_t Scene::GetComponentSignaturePosition(size_t hash) {
return component_signature_positions_.at(hash);
}
2023-01-02 17:24:20 +00:00
2023-05-25 17:35:32 +00:00
void Scene::Update(float ts) {
for (auto& [name, system] : systems_) {
system->OnUpdate(ts);
}
2022-12-15 10:07:22 +00:00
2023-05-25 17:35:32 +00:00
event_system_->DespatchEvents(); // clears event queue
2022-12-14 22:50:17 +00:00
}
2023-05-25 17:35:32 +00:00
} // namespace engine