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

111 lines
2.6 KiB
C++
Raw Normal View History

#include <renderer/renderer.hpp>
2025-07-05 13:28:41 +03:30
#include <engine/scene/components.hpp>
#include <engine/scene/entity.hpp>
#include <engine/scene/scene.hpp>
#include <glm/glm.hpp>
namespace Light {
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-06 14:02:50 +03:30
auto *sceneCamera = (Camera *) {};
auto *sceneCameraTransform = (TransformComponent *) {};
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)
{
2025-07-06 14:02:50 +03:30
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) {
2025-07-06 14:02:50 +03:30
Renderer::draw_quad(
2025-07-05 13:28:41 +03:30
transformComp,
spriteRendererComp.tint,
spriteRendererComp.texture
);
});
2025-07-06 14:02:50 +03:30
Renderer::end_scene();
}
}
2022-03-07 21:57:00 +03:30
}
2025-07-06 14:02:50 +03:30
auto Scene::create_entity(const std::string &name, const TransformComponent &transform) -> Entity
2022-03-07 21:57:00 +03:30
{
return create_entity_with_uuid(name, UUID(), transform);
2022-03-07 21:57:00 +03:30
}
2025-07-06 14:02:50 +03:30
auto Scene::get_entity_by_tag(const std::string &tag) -> Entity
2022-03-07 21:57:00 +03:30
{
// TagComponent tagComp(tag);
// entt::entity entity = entt::to_entity(m_registry, tagComp);
2025-07-06 14:02:50 +03:30
auto 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())
2022-03-07 21:57:00 +03:30
{
2025-07-06 16:30:38 +03:30
return entity;
}
2025-07-06 16:30:38 +03:30
lt_assert(false, "Scene::get_entity_by_tag: failed to find entity by tag: {}", tag);
return {};
2022-03-07 21:57:00 +03:30
}
2025-07-06 14:02:50 +03:30
auto Scene::create_entity_with_uuid(
2025-07-05 13:28:41 +03:30
const std::string &name,
UUID uuid,
const TransformComponent &transform
2025-07-06 14:02:50 +03:30
) -> Entity
2022-03-07 21:57:00 +03:30
{
2025-07-06 14:02:50 +03:30
auto entity = Entity { m_registry.create(), this };
entity.add_component<TagComponent>(name);
entity.add_component<TransformComponent>(transform);
entity.add_component<UUIDComponent>(uuid);
2022-03-07 21:57:00 +03:30
return entity;
}
2022-03-07 21:57:00 +03:30
} // namespace Light