fix: entity works
This commit is contained in:
@ -1,14 +1,13 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <entt/entt.hpp>
|
#include <entt/entt.hpp>
|
||||||
#include <memory>
|
#include "engine/export.h"
|
||||||
|
|
||||||
namespace Engine {
|
namespace Engine {
|
||||||
|
|
||||||
class Entity;
|
class Entity;
|
||||||
|
|
||||||
class Scene {
|
class ENGINE_API Scene {
|
||||||
private:
|
|
||||||
friend class Entity;
|
friend class Entity;
|
||||||
public:
|
public:
|
||||||
Scene() = default;
|
Scene() = default;
|
||||||
@ -19,25 +18,32 @@ private:
|
|||||||
friend class Renderer;
|
friend class Renderer;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Entity {
|
class ENGINE_API Entity {
|
||||||
public:
|
public:
|
||||||
Entity() = default;
|
Entity() = default;
|
||||||
Entity(entt::entity entity, Scene* scene) : m_entity(entity), m_scene(scene) {}
|
Entity(entt::entity entity, Scene* scene) : m_entity(entity), m_scene(scene) {}
|
||||||
Entity(const Entity& other) = default;
|
Entity(const Entity& other) = default;
|
||||||
|
|
||||||
template<typename Type, typename... Args>
|
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");
|
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)...);
|
return m_scene->m_registry.emplace<Type>(m_entity, std::forward<Args>(args)...);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Type>
|
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");
|
assert(this->m_scene != nullptr && "Scene has not been assigned to the entity");
|
||||||
return m_scene->m_registry.get<Type>(m_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:
|
private:
|
||||||
entt::entity m_entity { 0 };
|
entt::entity m_entity { entt::null };
|
||||||
Scene *m_scene = nullptr;
|
Scene *m_scene = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -11,8 +11,8 @@ Engine* Engine::s_instance = nullptr;
|
|||||||
|
|
||||||
void Engine::Run(std::unique_ptr<IApplication> app) {
|
void Engine::Run(std::unique_ptr<IApplication> app) {
|
||||||
m_scene = std::make_shared<Scene>();
|
m_scene = std::make_shared<Scene>();
|
||||||
m_renderer = std::make_unique<Renderer>(m_scene);
|
|
||||||
m_window = Window::GetInstance();
|
m_window = Window::GetInstance();
|
||||||
|
m_renderer = std::make_unique<Renderer>(m_scene);
|
||||||
m_app = std::move(app);
|
m_app = std::move(app);
|
||||||
m_running = true;
|
m_running = true;
|
||||||
|
|
||||||
@ -51,7 +51,7 @@ void Engine::OnEvent(const Event& event) {
|
|||||||
m_app->OnEvent(event);
|
m_app->OnEvent(event);
|
||||||
if (event.GetCategory() == Event::EventCategory::WINDOW) {
|
if (event.GetCategory() == Event::EventCategory::WINDOW) {
|
||||||
if (event.GetType() == EventType::WINDOW_RESIZE) {
|
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());
|
m_renderer->OnWindowResized(e.GetWidth(), e.GetHeight());
|
||||||
}
|
}
|
||||||
if (event.GetType() == EventType::WINDOW_CLOSE) {
|
if (event.GetType() == EventType::WINDOW_CLOSE) {
|
||||||
|
|||||||
@ -110,7 +110,7 @@ void Renderer::UpdateView() {
|
|||||||
auto camView = m_scene->m_registry.view<camera>();
|
auto camView = m_scene->m_registry.view<camera>();
|
||||||
auto camTransform = camView.size() > 0 ?
|
auto camTransform = camView.size() > 0 ?
|
||||||
m_scene->m_registry.get<transform>(camView.back()) :
|
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(
|
m_view = glm::lookAt(
|
||||||
camTransform.position,
|
camTransform.position,
|
||||||
|
|||||||
@ -1,9 +1,13 @@
|
|||||||
#include "engine/scene/scene.h"
|
#include "engine/scene/scene.h"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
namespace Engine {
|
namespace Engine {
|
||||||
|
|
||||||
Entity Scene::CreateEntity() {
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -27,7 +27,6 @@ using namespace Engine;
|
|||||||
|
|
||||||
class Game : public IApplication {
|
class Game : public IApplication {
|
||||||
public:
|
public:
|
||||||
Game() {}
|
|
||||||
~Game() override {}
|
~Game() override {}
|
||||||
|
|
||||||
void OnInit(std::shared_ptr<Scene> scene) override {
|
void OnInit(std::shared_ptr<Scene> scene) override {
|
||||||
@ -38,6 +37,7 @@ public:
|
|||||||
lightEntity.AddComponent<transform>(glm::vec3(5.f, 5.f, 5.f), glm::vec3(0.f));
|
lightEntity.AddComponent<transform>(glm::vec3(5.f, 5.f, 5.f), glm::vec3(0.f));
|
||||||
lightEntity.AddComponent<light>(light::LightType::DIRECTIONAL, glm::vec3(1.f, 1.f, 1.f), 1.5f);
|
lightEntity.AddComponent<light>(light::LightType::DIRECTIONAL, glm::vec3(1.f, 1.f, 1.f), 1.5f);
|
||||||
lightEntity.AddComponent<mesh>(std::shared_ptr<Object>(lightObj));
|
lightEntity.AddComponent<mesh>(std::shared_ptr<Object>(lightObj));
|
||||||
|
assert(lightEntity.HasComponent<mesh>() && "light doesn't have any mesh!");
|
||||||
|
|
||||||
cameraEntity = scene->CreateEntity();
|
cameraEntity = scene->CreateEntity();
|
||||||
cameraEntity.AddComponent<camera>();
|
cameraEntity.AddComponent<camera>();
|
||||||
@ -48,6 +48,7 @@ public:
|
|||||||
modelEntity.AddComponent<transform>(glm::vec3(0.f, 0.0f, 0.f));
|
modelEntity.AddComponent<transform>(glm::vec3(0.f, 0.0f, 0.f));
|
||||||
modelEntity.AddComponent<mesh>(std::shared_ptr<Object>(targetObj));
|
modelEntity.AddComponent<mesh>(std::shared_ptr<Object>(targetObj));
|
||||||
modelEntity.AddComponent<rotate>();
|
modelEntity.AddComponent<rotate>();
|
||||||
|
assert(modelEntity.HasComponent<mesh>() && "model doesn't have any mesh!");
|
||||||
|
|
||||||
// Object* grass = Object::LoadFile("./assets/common/cube/cube.obj");
|
// Object* grass = Object::LoadFile("./assets/common/cube/cube.obj");
|
||||||
// const auto cubeEntity = scene->m_registry.create();
|
// const auto cubeEntity = scene->m_registry.create();
|
||||||
@ -59,6 +60,8 @@ public:
|
|||||||
auto batchEntt = scene->CreateEntity();
|
auto batchEntt = scene->CreateEntity();
|
||||||
auto cubeBatch = batchEntt.AddComponent<batch>();
|
auto cubeBatch = batchEntt.AddComponent<batch>();
|
||||||
batchEntt.AddComponent<mesh>(cubeObj);
|
batchEntt.AddComponent<mesh>(cubeObj);
|
||||||
|
assert(batchEntt.HasComponent<batch>() && "batch doesn't have any batch component!");
|
||||||
|
assert(batchEntt.HasComponent<mesh>() && "batch doesn't have any mesh component!");
|
||||||
// auto cubeBatch = scene->m_registry.get<batch>(batchEntt);
|
// auto cubeBatch = scene->m_registry.get<batch>(batchEntt);
|
||||||
// Generate 1000 random cubes
|
// Generate 1000 random cubes
|
||||||
for (int i = 0; i < 1000; ++i) {
|
for (int i = 0; i < 1000; ++i) {
|
||||||
@ -77,6 +80,7 @@ public:
|
|||||||
auto floorEntt = scene->CreateEntity();
|
auto floorEntt = scene->CreateEntity();
|
||||||
floorEntt.AddComponent<transform>(glm::vec3(0.f));
|
floorEntt.AddComponent<transform>(glm::vec3(0.f));
|
||||||
floorEntt.AddComponent<mesh>(std::shared_ptr<Object>(floorObj));
|
floorEntt.AddComponent<mesh>(std::shared_ptr<Object>(floorObj));
|
||||||
|
assert(floorEntt.HasComponent<mesh>() && "floor doesn't have any mesh component!");
|
||||||
|
|
||||||
std::cout << "Game initialized" << std::endl;
|
std::cout << "Game initialized" << std::endl;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user