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 {
2025-07-05 13:28:41 +03:30
Scene::Scene(): m_Registry()
2022-03-07 21:57:00 +03:30
{
}
Scene::~Scene()
{
}
2022-03-07 21:57:00 +03:30
void Scene::OnCreate()
{
/* native scripts */
{
2025-07-05 13:28:41 +03:30
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->OnCreate();
}
});
}
2022-03-07 21:57:00 +03:30
}
2022-03-07 21:57:00 +03:30
void Scene::OnUpdate(float deltaTime)
{
/* native scripts */
{
2025-07-05 13:28:41 +03:30
m_Registry.view<NativeScriptComponent>().each([=](NativeScriptComponent &nsc) {
2022-03-07 21:57:00 +03:30
nsc.instance->OnUpdate(deltaTime);
});
}
2022-03-07 21:57:00 +03:30
}
2025-07-05 13:28:41 +03:30
void Scene::OnRender(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 */
{
2025-07-05 13:28:41 +03:30
m_Registry.group(entt::get<TransformComponent, CameraComponent>)
.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)
{
2022-03-07 21:57:00 +03:30
Renderer::BeginScene(sceneCamera, *sceneCameraTransform, targetFrameBuffer);
2025-07-05 13:28:41 +03:30
m_Registry.group(entt::get<TransformComponent, SpriteRendererComponent>)
.each([](TransformComponent &transformComp,
SpriteRendererComponent &spriteRendererComp) {
Renderer::DrawQuad(
transformComp,
spriteRendererComp.tint,
spriteRendererComp.texture
);
});
2022-03-07 21:57:00 +03:30
Renderer::EndScene();
}
}
2022-03-07 21:57:00 +03:30
}
2025-07-05 13:28:41 +03:30
Entity Scene::CreateEntity(const std::string &name, const TransformComponent &transform)
2022-03-07 21:57:00 +03:30
{
return CreateEntityWithUUID(name, UUID(), transform);
}
2025-07-05 13:28:41 +03:30
Entity Scene::GetEntityByTag(const std::string &tag)
2022-03-07 21:57:00 +03:30
{
// TagComponent tagComp(tag);
// entt::entity entity = entt::to_entity(m_Registry, tagComp);
Entity entity;
2025-07-05 13:28:41 +03:30
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
});
2022-03-07 21:57:00 +03:30
if (entity.IsValid())
return entity;
2022-03-07 21:57:00 +03:30
else
{
ASSERT("Scene::GetEntityByTag: failed to find entity by tag: {}", tag);
return Entity();
}
2022-03-07 21:57:00 +03:30
}
2025-07-05 13:28:41 +03:30
Entity Scene::CreateEntityWithUUID(
const std::string &name,
UUID uuid,
const TransformComponent &transform
)
2022-03-07 21:57:00 +03:30
{
Entity entity { m_Registry.create(), this };
entity.AddComponent<TagComponent>(name);
entity.AddComponent<TransformComponent>(transform);
entity.AddComponent<UUIDComponent>(uuid);
return entity;
}
2022-03-07 21:57:00 +03:30
} // namespace Light