fix: entity works

This commit is contained in:
2025-10-22 16:14:20 +02:00
parent 4798c52e51
commit 37d35f990d
5 changed files with 26 additions and 12 deletions

View File

@ -1,14 +1,13 @@
#pragma once
#include <entt/entt.hpp>
#include <memory>
#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<typename Type, typename... Args>
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<Type>(m_entity, std::forward<Args>(args)...);
}
template<typename Type>
[[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<Type>(m_entity);
}
template<typename Type>
[[nodiscard]] bool HasComponent() const {
return m_scene->m_registry.all_of<Type>(m_entity);
}
private:
entt::entity m_entity { 0 };
entt::entity m_entity { entt::null };
Scene *m_scene = nullptr;
};

View File

@ -11,8 +11,8 @@ Engine* Engine::s_instance = nullptr;
void Engine::Run(std::unique_ptr<IApplication> app) {
m_scene = std::make_shared<Scene>();
m_renderer = std::make_unique<Renderer>(m_scene);
m_window = Window::GetInstance();
m_renderer = std::make_unique<Renderer>(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<const WindowResizeEvent&>(event);
const auto e = dynamic_cast<const WindowResizeEvent&>(event);
m_renderer->OnWindowResized(e.GetWidth(), e.GetHeight());
}
if (event.GetType() == EventType::WINDOW_CLOSE) {

View File

@ -110,7 +110,7 @@ void Renderer::UpdateView() {
auto camView = m_scene->m_registry.view<camera>();
auto camTransform = camView.size() > 0 ?
m_scene->m_registry.get<transform>(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,

View File

@ -1,9 +1,13 @@
#include "engine/scene/scene.h"
#include <iostream>
namespace Engine {
Entity Scene::CreateEntity() {
return { m_registry.create(), this };
Entity entity = { m_registry.create(), this };
// std::cout << "Entities: " << (int)m_registry.view<entt::entity>().size() << std::endl;
return entity;
}
}