light/modules/engine/src/scene/scene.cpp

119 lines
2.6 KiB
C++
Raw Normal View History

2025-07-05 13:28:41 +03:30
#include <engine/graphics/renderer.hpp>
#include <engine/scene/components.hpp>
#include <engine/scene/entity.hpp>
#include <engine/scene/scene.hpp>
#include <glm/glm.hpp>
namespace Light {
Scene::Scene(): m_registry()
2022-03-07 21:57:00 +03:30
{
}
Scene::~Scene()
{
}
void Scene::on_create()
2022-03-07 21:57:00 +03:30
{
/* native scripts */
{
m_registry.view<NativeScriptComponent>().each([](NativeScriptComponent &nsc) {
2022-03-07 21:57:00 +03:30
if (nsc.instance == nullptr)
{
nsc.instance = nsc.CreateInstance();
nsc.instance->on_create();
2022-03-07 21:57:00 +03:30
}
});
}
2022-03-07 21:57:00 +03:30
}
void Scene::on_update(float deltaTime)
2022-03-07 21:57:00 +03:30
{
/* native scripts */
{
m_registry.view<NativeScriptComponent>().each([=](NativeScriptComponent &nsc) {
nsc.instance->on_update(deltaTime);
2022-03-07 21:57:00 +03:30
});
}
2022-03-07 21:57:00 +03:30
}
void Scene::on_render(const Ref<Framebuffer> &targetFrameBuffer /* = nullptr */)
2022-03-07 21:57:00 +03:30
{
2025-07-05 13:28:41 +03:30
Camera *sceneCamera = nullptr;
TransformComponent *sceneCameraTransform;
2022-03-07 21:57:00 +03:30
/* scene camera */
{
m_registry.group(entt::get<TransformComponent, CameraComponent>)
2025-07-05 13:28:41 +03:30
.each([&](TransformComponent &transformComp, CameraComponent &cameraComp) {
if (cameraComp.isPrimary)
{
sceneCamera = &cameraComp.camera;
sceneCameraTransform = &transformComp;
}
});
}
2022-03-07 21:57:00 +03:30
/* draw quads */
{
2022-03-07 21:57:00 +03:30
if (sceneCamera)
{
renderer::begin_scene(sceneCamera, *sceneCameraTransform, targetFrameBuffer);
m_registry.group(entt::get<TransformComponent, SpriteRendererComponent>)
2025-07-05 13:28:41 +03:30
.each([](TransformComponent &transformComp,
SpriteRendererComponent &spriteRendererComp) {
renderer::draw_quad(
2025-07-05 13:28:41 +03:30
transformComp,
spriteRendererComp.tint,
spriteRendererComp.texture
);
});
renderer::end_scene();
}
}
2022-03-07 21:57:00 +03:30
}
Entity Scene::create_entity(const std::string &name, const TransformComponent &transform)
2022-03-07 21:57:00 +03:30
{
return create_entity_with_uuid(name, UUID(), transform);
2022-03-07 21:57:00 +03:30
}
Entity Scene::get_entity_by_tag(const std::string &tag)
2022-03-07 21:57:00 +03:30
{
// TagComponent tagComp(tag);
// entt::entity entity = entt::to_entity(m_registry, tagComp);
2022-03-07 21:57:00 +03:30
Entity entity;
m_registry.view<TagComponent>().each([&](TagComponent &tagComp) {
// if (tagComp.tag == tag)
// entity = entity(entt::to_entity(m_registry, tagComp), this);
2022-03-07 21:57:00 +03:30
});
if (entity.is_valid())
return entity;
2022-03-07 21:57:00 +03:30
else
{
lt_assert("Scene::get_entity_by_tag: failed to find entity by tag: {}", tag);
return {};
}
2022-03-07 21:57:00 +03:30
}
Entity Scene::create_entity_with_uuid(
2025-07-05 13:28:41 +03:30
const std::string &name,
UUID uuid,
const TransformComponent &transform
)
2022-03-07 21:57:00 +03:30
{
Entity entity { m_registry.create(), this };
2022-03-07 21:57:00 +03:30
entity.AddComponent<TagComponent>(name);
entity.AddComponent<TransformComponent>(transform);
entity.AddComponent<UUIDComponent>(uuid);
return entity;
}
2022-03-07 21:57:00 +03:30
} // namespace Light