2025-07-10 13:55:18 +03:30
|
|
|
#include <camera/component.hpp>
|
2025-07-11 01:16:52 +03:30
|
|
|
#include <ecs/components.hpp>
|
|
|
|
#include <ecs/entity.hpp>
|
|
|
|
#include <ecs/scene.hpp>
|
2021-07-25 17:50:08 +04:30
|
|
|
#include <glm/glm.hpp>
|
2025-07-10 13:55:18 +03:30
|
|
|
#include <renderer/renderer.hpp>
|
2021-07-25 17:50:08 +04:30
|
|
|
|
2025-07-11 00:05:48 +03:30
|
|
|
namespace lt {
|
2021-07-25 17:50:08 +04:30
|
|
|
|
2025-07-05 15:36:53 +03:30
|
|
|
void Scene::on_create()
|
2022-03-07 21:57:00 +03:30
|
|
|
{
|
|
|
|
/* native scripts */
|
2021-07-25 17:50:08 +04:30
|
|
|
{
|
2025-07-05 14:23:01 +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();
|
2025-07-05 15:36:53 +03:30
|
|
|
nsc.instance->on_create();
|
2022-03-07 21:57:00 +03: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 15:36:53 +03:30
|
|
|
void Scene::on_update(float deltaTime)
|
2022-03-07 21:57:00 +03:30
|
|
|
{
|
|
|
|
/* native scripts */
|
2021-07-31 09:37:09 +04:30
|
|
|
{
|
2025-07-05 14:23:01 +03:30
|
|
|
m_registry.view<NativeScriptComponent>().each([=](NativeScriptComponent &nsc) {
|
2025-07-05 15:36:53 +03:30
|
|
|
nsc.instance->on_update(deltaTime);
|
2022-03-07 21:57:00 +03:30
|
|
|
});
|
2021-07-31 09:37:09 +04:30
|
|
|
}
|
2022-03-07 21:57:00 +03:30
|
|
|
}
|
|
|
|
|
2025-07-05 15:36:53 +03:30
|
|
|
void Scene::on_render(const Ref<Framebuffer> &targetFrameBuffer /* = nullptr */)
|
2022-03-07 21:57:00 +03:30
|
|
|
{
|
2025-07-12 14:40:10 +03:30
|
|
|
auto *sceneCamera = (Camera *) nullptr;
|
|
|
|
auto *sceneCameraTransform = (TransformComponent *) nullptr;
|
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 14:23:01 +03:30
|
|
|
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;
|
|
|
|
}
|
|
|
|
});
|
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
|
|
|
{
|
2025-07-06 14:02:50 +03:30
|
|
|
Renderer::begin_scene(sceneCamera, *sceneCameraTransform, targetFrameBuffer);
|
2021-07-30 14:40:34 +04:30
|
|
|
|
2025-07-05 14:23:01 +03:30
|
|
|
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
|
|
|
|
);
|
|
|
|
});
|
2021-07-30 14:40:34 +04:30
|
|
|
|
2025-07-06 14:02:50 +03:30
|
|
|
Renderer::end_scene();
|
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-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
|
|
|
{
|
2025-07-05 15:36:53 +03:30
|
|
|
return create_entity_with_uuid(name, UUID(), transform);
|
2022-03-07 21:57:00 +03:30
|
|
|
}
|
2021-10-07 01:13:18 +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);
|
2025-07-05 14:23:01 +03:30
|
|
|
// entt::entity entity = entt::to_entity(m_registry, tagComp);
|
2025-07-06 14:02:50 +03:30
|
|
|
auto entity = Entity {};
|
2021-10-07 01:13:18 +03:30
|
|
|
|
2025-07-05 14:23:01 +03:30
|
|
|
m_registry.view<TagComponent>().each([&](TagComponent &tagComp) {
|
2025-07-05 11:33:43 +03:30
|
|
|
// if (tagComp.tag == tag)
|
2025-07-05 15:36:53 +03:30
|
|
|
// 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
|
|
|
|
2025-07-05 15:36:53 +03:30
|
|
|
if (entity.is_valid())
|
2022-03-07 21:57:00 +03:30
|
|
|
{
|
2025-07-06 16:30:38 +03:30
|
|
|
return entity;
|
2021-07-25 17:50:08 +04:30
|
|
|
}
|
2025-07-06 16:30:38 +03:30
|
|
|
|
2025-07-11 02:12:55 +03:30
|
|
|
ensure(false, "Scene::get_entity_by_tag: failed to find entity by tag: {}", tag);
|
2025-07-06 16:30:38 +03:30
|
|
|
return {};
|
2022-03-07 21:57:00 +03:30
|
|
|
}
|
2021-07-25 17:50:08 +04: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;
|
2021-07-25 17:50:08 +04:30
|
|
|
}
|
2022-03-07 21:57:00 +03:30
|
|
|
|
2025-07-11 00:05:48 +03:30
|
|
|
} // namespace lt
|