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