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>
|
2021-07-25 17:50:08 +04:30
|
|
|
#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()
|
|
|
|
{
|
|
|
|
}
|
2021-07-25 17:50:08 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
void Scene::OnCreate()
|
|
|
|
{
|
|
|
|
/* native scripts */
|
2021-07-25 17:50:08 +04:30
|
|
|
{
|
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();
|
|
|
|
}
|
|
|
|
});
|
2021-07-25 17:50:08 +04:30
|
|
|
}
|
2022-03-07 21:57:00 +03:30
|
|
|
}
|
2021-07-25 17:50:08 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
void Scene::OnUpdate(float deltaTime)
|
|
|
|
{
|
|
|
|
/* native scripts */
|
2021-07-31 09:37:09 +04:30
|
|
|
{
|
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);
|
|
|
|
});
|
2021-07-31 09:37:09 +04:30
|
|
|
}
|
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;
|
2021-07-31 09:37:09 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
/* scene camera */
|
2021-07-31 09:37:09 +04:30
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
});
|
2021-07-31 09:37:09 +04:30
|
|
|
}
|
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
/* draw quads */
|
2021-07-25 17:50:08 +04:30
|
|
|
{
|
2022-03-07 21:57:00 +03:30
|
|
|
if (sceneCamera)
|
2021-07-30 14:40:34 +04:30
|
|
|
{
|
2022-03-07 21:57:00 +03:30
|
|
|
Renderer::BeginScene(sceneCamera, *sceneCameraTransform, targetFrameBuffer);
|
2021-07-30 14:40:34 +04:30
|
|
|
|
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
|
|
|
|
);
|
|
|
|
});
|
2021-07-30 14:40:34 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
Renderer::EndScene();
|
2021-07-30 14:40:34 +04:30
|
|
|
}
|
2021-07-25 17:50:08 +04:30
|
|
|
}
|
2022-03-07 21:57:00 +03:30
|
|
|
}
|
2021-07-25 17:50:08 +04: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);
|
|
|
|
}
|
2021-10-07 01:13:18 +03:30
|
|
|
|
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;
|
2021-10-07 01:13:18 +03:30
|
|
|
|
2025-07-05 13:28:41 +03:30
|
|
|
m_Registry.view<TagComponent>().each([&](TagComponent &tagComp) {
|
2025-07-05 11:33:43 +03:30
|
|
|
// if (tagComp.tag == tag)
|
|
|
|
// entity = Entity(entt::to_entity(m_Registry, tagComp), this);
|
2022-03-07 21:57:00 +03:30
|
|
|
});
|
2021-07-25 17:50:08 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
if (entity.IsValid())
|
2021-07-25 17:50:08 +04:30
|
|
|
return entity;
|
2022-03-07 21:57:00 +03:30
|
|
|
else
|
|
|
|
{
|
|
|
|
ASSERT("Scene::GetEntityByTag: failed to find entity by tag: {}", tag);
|
|
|
|
return Entity();
|
2021-07-25 17:50:08 +04:30
|
|
|
}
|
2022-03-07 21:57:00 +03:30
|
|
|
}
|
2021-07-25 17:50:08 +04: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;
|
2021-07-25 17:50:08 +04:30
|
|
|
}
|
2022-03-07 21:57:00 +03:30
|
|
|
|
|
|
|
} // namespace Light
|