fix: entity works
This commit is contained in:
@ -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;
|
||||
};
|
||||
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -27,7 +27,6 @@ using namespace Engine;
|
||||
|
||||
class Game : public IApplication {
|
||||
public:
|
||||
Game() {}
|
||||
~Game() 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<light>(light::LightType::DIRECTIONAL, glm::vec3(1.f, 1.f, 1.f), 1.5f);
|
||||
lightEntity.AddComponent<mesh>(std::shared_ptr<Object>(lightObj));
|
||||
assert(lightEntity.HasComponent<mesh>() && "light doesn't have any mesh!");
|
||||
|
||||
cameraEntity = scene->CreateEntity();
|
||||
cameraEntity.AddComponent<camera>();
|
||||
@ -48,6 +48,7 @@ public:
|
||||
modelEntity.AddComponent<transform>(glm::vec3(0.f, 0.0f, 0.f));
|
||||
modelEntity.AddComponent<mesh>(std::shared_ptr<Object>(targetObj));
|
||||
modelEntity.AddComponent<rotate>();
|
||||
assert(modelEntity.HasComponent<mesh>() && "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<batch>();
|
||||
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);
|
||||
// Generate 1000 random cubes
|
||||
for (int i = 0; i < 1000; ++i) {
|
||||
@ -77,6 +80,7 @@ public:
|
||||
auto floorEntt = scene->CreateEntity();
|
||||
floorEntt.AddComponent<transform>(glm::vec3(0.f));
|
||||
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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user