From 37d35f990d1094a629358b8c127b4f7ed90f4b6a Mon Sep 17 00:00:00 2001 From: LandaMm Date: Wed, 22 Oct 2025 16:14:20 +0200 Subject: [PATCH] fix: entity works --- engine/include/engine/scene/scene.h | 20 +++++++++++++------- engine/src/renderer/core.cpp | 4 ++-- engine/src/renderer/renderer.cpp | 2 +- engine/src/scene/scene.cpp | 6 +++++- sandbox/src/main.cpp | 6 +++++- 5 files changed, 26 insertions(+), 12 deletions(-) diff --git a/engine/include/engine/scene/scene.h b/engine/include/engine/scene/scene.h index 93b8cea..df52440 100644 --- a/engine/include/engine/scene/scene.h +++ b/engine/include/engine/scene/scene.h @@ -1,14 +1,13 @@ #pragma once #include -#include +#include "engine/export.h" namespace Engine { class Entity; -class Scene { -private: +class ENGINE_API Scene { friend class Entity; public: Scene() = default; @@ -19,25 +18,32 @@ private: friend class Renderer; }; -class Entity { +class ENGINE_API Entity { public: Entity() = default; Entity(entt::entity entity, Scene* scene) : m_entity(entity), m_scene(scene) {} Entity(const Entity& other) = default; template - inline auto AddComponent(Args &&...args) { + auto AddComponent(Args &&...args) { + assert(this->m_entity != entt::null && "Entity is empty"); assert(this->m_scene != nullptr && "Scene has not been assigned to the entity"); return m_scene->m_registry.emplace(m_entity, std::forward(args)...); } template - [[nodiscard]] inline auto GetComponent() { + [[nodiscard]] auto GetComponent() const { + assert(this->m_entity != entt::null && "Entity is empty"); assert(this->m_scene != nullptr && "Scene has not been assigned to the entity"); return m_scene->m_registry.get(m_entity); } + + template + [[nodiscard]] bool HasComponent() const { + return m_scene->m_registry.all_of(m_entity); + } private: - entt::entity m_entity { 0 }; + entt::entity m_entity { entt::null }; Scene *m_scene = nullptr; }; diff --git a/engine/src/renderer/core.cpp b/engine/src/renderer/core.cpp index 974154a..960a581 100644 --- a/engine/src/renderer/core.cpp +++ b/engine/src/renderer/core.cpp @@ -11,8 +11,8 @@ Engine* Engine::s_instance = nullptr; void Engine::Run(std::unique_ptr app) { m_scene = std::make_shared(); - m_renderer = std::make_unique(m_scene); m_window = Window::GetInstance(); + m_renderer = std::make_unique(m_scene); m_app = std::move(app); m_running = true; @@ -51,7 +51,7 @@ void Engine::OnEvent(const Event& event) { m_app->OnEvent(event); if (event.GetCategory() == Event::EventCategory::WINDOW) { if (event.GetType() == EventType::WINDOW_RESIZE) { - auto e = static_cast(event); + const auto e = dynamic_cast(event); m_renderer->OnWindowResized(e.GetWidth(), e.GetHeight()); } if (event.GetType() == EventType::WINDOW_CLOSE) { diff --git a/engine/src/renderer/renderer.cpp b/engine/src/renderer/renderer.cpp index 8ba21d0..48062d3 100644 --- a/engine/src/renderer/renderer.cpp +++ b/engine/src/renderer/renderer.cpp @@ -110,7 +110,7 @@ void Renderer::UpdateView() { auto camView = m_scene->m_registry.view(); auto camTransform = camView.size() > 0 ? m_scene->m_registry.get(camView.back()) : - transform {glm::vec3(0.f, 0.f, 0.f), glm::vec3(0.f, 0.f, 0.f), glm::vec3(0.f, 0.f, 0.f)}; + transform {glm::vec3(0.f, 0.f, 0.f), glm::vec3(0.f, 0.f, 0.f), glm::vec3(1.f, 1.f, 1.f)}; m_view = glm::lookAt( camTransform.position, diff --git a/engine/src/scene/scene.cpp b/engine/src/scene/scene.cpp index e3fc563..c3bb868 100644 --- a/engine/src/scene/scene.cpp +++ b/engine/src/scene/scene.cpp @@ -1,9 +1,13 @@ #include "engine/scene/scene.h" +#include + namespace Engine { Entity Scene::CreateEntity() { - return { m_registry.create(), this }; + Entity entity = { m_registry.create(), this }; + // std::cout << "Entities: " << (int)m_registry.view().size() << std::endl; + return entity; } } diff --git a/sandbox/src/main.cpp b/sandbox/src/main.cpp index 9ddb0a3..1f292d5 100644 --- a/sandbox/src/main.cpp +++ b/sandbox/src/main.cpp @@ -27,7 +27,6 @@ using namespace Engine; class Game : public IApplication { public: - Game() {} ~Game() override {} void OnInit(std::shared_ptr scene) override { @@ -38,6 +37,7 @@ public: lightEntity.AddComponent(glm::vec3(5.f, 5.f, 5.f), glm::vec3(0.f)); lightEntity.AddComponent(light::LightType::DIRECTIONAL, glm::vec3(1.f, 1.f, 1.f), 1.5f); lightEntity.AddComponent(std::shared_ptr(lightObj)); + assert(lightEntity.HasComponent() && "light doesn't have any mesh!"); cameraEntity = scene->CreateEntity(); cameraEntity.AddComponent(); @@ -48,6 +48,7 @@ public: modelEntity.AddComponent(glm::vec3(0.f, 0.0f, 0.f)); modelEntity.AddComponent(std::shared_ptr(targetObj)); modelEntity.AddComponent(); + assert(modelEntity.HasComponent() && "model doesn't have any mesh!"); // Object* grass = Object::LoadFile("./assets/common/cube/cube.obj"); // const auto cubeEntity = scene->m_registry.create(); @@ -59,6 +60,8 @@ public: auto batchEntt = scene->CreateEntity(); auto cubeBatch = batchEntt.AddComponent(); batchEntt.AddComponent(cubeObj); + assert(batchEntt.HasComponent() && "batch doesn't have any batch component!"); + assert(batchEntt.HasComponent() && "batch doesn't have any mesh component!"); // auto cubeBatch = scene->m_registry.get(batchEntt); // Generate 1000 random cubes for (int i = 0; i < 1000; ++i) { @@ -77,6 +80,7 @@ public: auto floorEntt = scene->CreateEntity(); floorEntt.AddComponent(glm::vec3(0.f)); floorEntt.AddComponent(std::shared_ptr(floorObj)); + assert(floorEntt.HasComponent() && "floor doesn't have any mesh component!"); std::cout << "Game initialized" << std::endl;