Skip to content

Commit

Permalink
Added Entity class and TagComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCherno committed Jul 23, 2020
1 parent 72bc16e commit 5658efc
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 12 deletions.
1 change: 1 addition & 0 deletions Hazel/src/Hazel.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "Hazel/ImGui/ImGuiLayer.h"

#include "Hazel/Scene/Scene.h"
#include "Hazel/Scene/Entity.h"
#include "Hazel/Scene/Components.h"

// ---Renderer------------------------
Expand Down
10 changes: 10 additions & 0 deletions Hazel/src/Hazel/Scene/Components.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@

namespace Hazel {

struct TagComponent
{
std::string Tag;

TagComponent() = default;
TagComponent(const TagComponent&) = default;
TagComponent(const std::string& tag)
: Tag(tag) {}
};

struct TransformComponent
{
glm::mat4 Transform{ 1.0f };
Expand Down
11 changes: 11 additions & 0 deletions Hazel/src/Hazel/Scene/Entity.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "hzpch.h"
#include "Entity.h"

namespace Hazel {

Entity::Entity(entt::entity handle, Scene* scene)
: m_EntityHandle(handle), m_Scene(scene)
{
}

}
49 changes: 49 additions & 0 deletions Hazel/src/Hazel/Scene/Entity.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#pragma once

#include "Scene.h"

#include "entt.hpp"

namespace Hazel {

class Entity
{
public:
Entity() = default;
Entity(entt::entity handle, Scene* scene);
Entity(const Entity& other) = default;

template<typename T, typename... Args>
T& AddComponent(Args&&... args)
{
HZ_CORE_ASSERT(!HasComponent<T>(), "Entity already has component!");
return m_Scene->m_Registry.emplace<T>(m_EntityHandle, std::forward<Args>(args)...);
}

template<typename T>
T& GetComponent()
{
HZ_CORE_ASSERT(HasComponent<T>(), "Entity does not have component!");
return m_Scene->m_Registry.get<T>(m_EntityHandle);
}

template<typename T>
bool HasComponent()
{
return m_Scene->m_Registry.has<T>(m_EntityHandle);
}

template<typename T>
void RemoveComponent()
{
HZ_CORE_ASSERT(HasComponent<T>(), "Entity does not have component!");
m_Scene->m_Registry.remove<T>(m_EntityHandle);
}

operator bool() const { return m_EntityHandle != 0; }
private:
entt::entity m_EntityHandle{ 0 };
Scene* m_Scene = nullptr;
};

}
10 changes: 8 additions & 2 deletions Hazel/src/Hazel/Scene/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include <glm/glm.hpp>

#include "Entity.h"

namespace Hazel {

static void DoMath(const glm::mat4& transform)
Expand Down Expand Up @@ -49,9 +51,13 @@ namespace Hazel {
{
}

entt::entity Scene::CreateEntity()
Entity Scene::CreateEntity(const std::string& name)
{
return m_Registry.create();
Entity entity = { m_Registry.create(), this };
entity.AddComponent<TransformComponent>();
auto& tag = entity.AddComponent<TagComponent>();
tag.Tag = name.empty() ? "Entity" : name;
return entity;
}

void Scene::OnUpdate(Timestep ts)
Expand Down
9 changes: 5 additions & 4 deletions Hazel/src/Hazel/Scene/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@

namespace Hazel {

class Entity;

class Scene
{
public:
Scene();
~Scene();

entt::entity CreateEntity();

// TEMP
entt::registry& Reg() { return m_Registry; }
Entity CreateEntity(const std::string& name = std::string());

void OnUpdate(Timestep ts);
private:
entt::registry m_Registry;

friend class Entity;
};

}
19 changes: 14 additions & 5 deletions Hazelnut/src/EditorLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>


namespace Hazel {

EditorLayer::EditorLayer()
Expand All @@ -24,9 +25,9 @@ namespace Hazel {

m_ActiveScene = CreateRef<Scene>();

auto square = m_ActiveScene->CreateEntity();
m_ActiveScene->Reg().emplace<TransformComponent>(square);
m_ActiveScene->Reg().emplace<SpriteRendererComponent>(square, glm::vec4{0.0f, 1.0f, 0.0f, 1.0f});
// Entity
auto square = m_ActiveScene->CreateEntity("Green Square");
square.AddComponent<SpriteRendererComponent>(glm::vec4{0.0f, 1.0f, 0.0f, 1.0f});

m_SquareEntity = square;
}
Expand Down Expand Up @@ -142,8 +143,16 @@ namespace Hazel {
ImGui::Text("Vertices: %d", stats.GetTotalVertexCount());
ImGui::Text("Indices: %d", stats.GetTotalIndexCount());

auto& squareColor = m_ActiveScene->Reg().get<SpriteRendererComponent>(m_SquareEntity).Color;
ImGui::ColorEdit4("Square Color", glm::value_ptr(squareColor));
if (m_SquareEntity)
{
ImGui::Separator();
auto& tag = m_SquareEntity.GetComponent<TagComponent>().Tag;
ImGui::Text("%s", tag.c_str());

auto& squareColor = m_SquareEntity.GetComponent<SpriteRendererComponent>().Color;
ImGui::ColorEdit4("Square Color", glm::value_ptr(squareColor));
ImGui::Separator();
}

ImGui::End();

Expand Down
2 changes: 1 addition & 1 deletion Hazelnut/src/EditorLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace Hazel {
Ref<Framebuffer> m_Framebuffer;

Ref<Scene> m_ActiveScene;
entt::entity m_SquareEntity;
Entity m_SquareEntity;

Ref<Texture2D> m_CheckerboardTexture;

Expand Down

0 comments on commit 5658efc

Please sign in to comment.