First ECS test in the editor working (Tag component editable using a temp-testing entity in the editor)

gui_api_redesign
Joey Pollack 4 years ago
parent e237d0dcb5
commit 9dede57b96

@ -59,15 +59,16 @@ Core:
☐ Provide Methods that give access to the C++ code ☐ Provide Methods that give access to the C++ code
ECS: ECS:
Research EnTT for the ECS (https://github.com/skypjack/entt/) Research EnTT for the ECS (https://github.com/skypjack/entt/) @done(22-06-01 14:01)
Research using ECS with a quadtree (regions may essentially be a quadtree) Research using ECS with a quadtree (regions may essentially be a quadtree) @done(22-06-01 14:01)
☐ Use 1 Entt registry with a component that stores the region ID (Index?) ☐ Use 1 Entt registry with a component that stores the region ID (Index?)
Enitity: Enitity:
☐ Single UUID ✔ Single UUID @done(22-06-01 14:01)
Functionality for adding/working with components (through EnTT) Functionality for adding/working with components (through EnTT) @done(22-06-01 14:01)
Components: Components:
☐ Tag
☐ Transform ☐ Transform
☐ SpriteRenderer ☐ SpriteRenderer
☐ Animation Controller ☐ Animation Controller
@ -78,12 +79,15 @@ Core:
☐ Box Collider (Box2D) ☐ Box Collider (Box2D)
World (Lunariums version of a "Scene"): World (Lunariums version of a "Scene"):
☐ Track/manage loaded regions ☐ Implement the world without Regions first
☐ Render loaded Regions
Implement Regions:
☐ Track/manage active regions
☐ Render active Regions
Region: Region:
☐ List of renderable images for each layer ☐ List of renderable images for each layer
☐ List of game objects (by reference) in this Region ☐ List of entities (by entt id) in this Region
☐ Implement image grid within regions ☐ Implement image grid within regions
[Regions could potentially be split into multiple images (an internal grid). [Regions could potentially be split into multiple images (an internal grid).
To support larger region sizes without needing single images that are like 1048576x1048576 or some nonsense.] To support larger region sizes without needing single images that are like 1048576x1048576 or some nonsense.]

@ -5,6 +5,7 @@ set(EDITOR_SRC
"editor_helpers.cpp" "editor_helpers.cpp"
"panel_manager.cpp" "panel_manager.cpp"
"project.cpp" "project.cpp"
"component_guis.cpp"
"panels/about.cpp" "panels/about.cpp"
"panels/asset_browser.cpp" "panels/asset_browser.cpp"
"panels/world_tree.cpp" "panels/world_tree.cpp"

@ -0,0 +1,27 @@
/******************************************************************************
* File - component_guis.cpp
* Author - Joey Pollack
* Date - 2022/06/01 (y/m/d)
* Mod Date - 2022/06/01 (y/m/d)
* Description - Functions to render component editing GUIs
******************************************************************************/
#include "component_guis.h"
#include <editor/editor.h>
#include <utils/logger.h>
#include <dearimgui/imgui.h>
namespace lunarium { namespace editor
{
void CompGui::RenderTagComp(TagComponent& comp)
{
ImGui::Text("Tag:");
ImGui::SameLine();
ImGui::InputText("##Tag", comp.Info.data(), comp.Info.capacity())
}
void CompGui::RenderTransformComp(TransformComponent& comp)
{
}
}}

@ -0,0 +1,25 @@
/******************************************************************************
* File - component_guis.h
* Author - Joey Pollack
* Date - 2022/06/01 (y/m/d)
* Mod Date - 2022/06/01 (y/m/d)
* Description - Functions to render component editing GUIs
******************************************************************************/
#ifndef LUNARIUM_COMPONENT_GUIS_H_
#define LUNARIUM_COMPONENT_GUIS_H_
#include <world/components.h>
namespace lunarium { namespace editor
{
class CompGui
{
public:
static void RenderTagComp(TagComponent& comp);
static void RenderTransformComp(TransformComponent& comp);
};
}}
#endif // LUNARIUM_COMPONENT_GUIS_H_

@ -28,6 +28,11 @@
#include <world/components.h> #include <world/components.h>
// TEST STUFF
#include <world/world.h>
#include <world/entity.h>
#include <world/components.h>
using namespace lunarium::gui; using namespace lunarium::gui;
// ERROR LOG MACRO // ERROR LOG MACRO
@ -47,7 +52,6 @@ namespace editor
OpRes Editor::Initialize() OpRes Editor::Initialize()
{ {
LabelComponent test;
LogCat = Logger::RegisterCategory("EDITOR"); LogCat = Logger::RegisterCategory("EDITOR");
// Initialize internal data // Initialize internal data
@ -69,6 +73,12 @@ namespace editor
mPanelManager.AddPanel(new WorldView(), mPanels.WorldView).LogIfFailed(LogCat); mPanelManager.AddPanel(new WorldView(), mPanels.WorldView).LogIfFailed(LogCat);
mPanelManager.AddPanel(new PropertiesView(), mPanels.PropertiesView).LogIfFailed(LogCat); mPanelManager.AddPanel(new PropertiesView(), mPanels.PropertiesView).LogIfFailed(LogCat);
// TEST ENTITY
mTestWorld = new lunarium::World;
mTestEntity = mTestWorld->CreateEntity();
mTestWorld->GetEntity(mTestEntity)->AddComponent<TagComponent>();
((PropertiesView*)mPanelManager.GetPanel(mPanels.PropertiesView))->SetSelection(mTestWorld->GetEntity(mTestEntity));
return OpRes::OK(); return OpRes::OK();
} }

@ -10,6 +10,7 @@
#define EDITOR_H_ #define EDITOR_H_
#include <core/run_mode.h> #include <core/run_mode.h>
#include <core/common_defs.h>
#include <utils/op_res.h> #include <utils/op_res.h>
#include "project.h" #include "project.h"
@ -21,7 +22,7 @@
namespace lunarium namespace lunarium
{ {
class FileBrowser; class World;
} }
namespace lunarium { namespace editor namespace lunarium { namespace editor
@ -60,6 +61,7 @@ namespace lunarium { namespace editor
private: // Data private: // Data
PanelManager mPanelManager; PanelManager mPanelManager;
lunarium::World* mTestWorld;
Project mProject; Project mProject;
// Tools // Tools
@ -86,6 +88,9 @@ namespace lunarium { namespace editor
bool mDoSaveProject; bool mDoSaveProject;
bool mDoSaveAs; bool mDoSaveAs;
// TEST DATA
LUUID mTestEntity;
private: // HELPERS private: // HELPERS
void RenderWindow(); void RenderWindow();

@ -10,6 +10,9 @@
#include "properties_view.h" #include "properties_view.h"
#include <dearimgui/imgui.h> #include <dearimgui/imgui.h>
#include <editor/editor.h> #include <editor/editor.h>
#include <world/entity.h>
#include <world/components.h>
#include <editor/component_guis.h>
namespace lunarium { namespace editor namespace lunarium { namespace editor
{ {
@ -30,9 +33,30 @@ namespace lunarium { namespace editor
return false; return false;
} }
if (mpSelectedEntity)
{
// TODO: iterate through components
if (mpSelectedEntity->HasComponent<TagComponent>())
{
CompGui::RenderTagComp(mpSelectedEntity->GetComponent<TagComponent>());
}
}
ImGui::End(); ImGui::End();
return true; return true;
} }
void PropertiesView::SetSelection(Entity* pEntity)
{
mpSelectedAsset = nullptr;
mpSelectedEntity = pEntity;
}
void PropertiesView::SetSelection(EditorAsset* pAsset)
{
mpSelectedEntity = nullptr;
mpSelectedAsset = pAsset;
}
}} }}

@ -15,19 +15,28 @@
namespace lunarium namespace lunarium
{ {
class Entity;
namespace editor namespace editor
{ {
class CustromProperty; class CustromProperty;
class EditorAsset;
class PropertiesView : public gui::Panel class PropertiesView : public gui::Panel
{ {
public: public:
PropertiesView(); PropertiesView();
void SetSelection(Entity* pEntity);
void SetSelection(EditorAsset* pAsset);
bool DoFrame(); bool DoFrame();
private: private:
std::vector<CustromProperty*> mCustomProps; std::vector<CustromProperty*> mCustomProps;
// Only ONE of these should be set at a time
Entity* mpSelectedEntity;
EditorAsset* mpSelectedAsset;
}; };
} }
} }

@ -21,12 +21,12 @@ namespace editor
} }
void WorldTree::SetWorld(lunarium::game::World* pWorld) void WorldTree::SetWorld(lunarium::World* pWorld)
{ {
mpWorld = pWorld; mpWorld = pWorld;
} }
lunarium::game::World* WorldTree::GetWorld() lunarium::World* WorldTree::GetWorld()
{ {
return mpWorld; return mpWorld;
} }

@ -13,7 +13,7 @@
namespace lunarium namespace lunarium
{ {
namespace game { class World; } // TODO: Use the editor::World class instead? class World;
namespace editor namespace editor
{ {
@ -22,13 +22,13 @@ namespace editor
public: public:
WorldTree(); WorldTree();
void SetWorld(lunarium::game::World* pWorld); void SetWorld(lunarium::World* pWorld);
lunarium::game::World* GetWorld(); lunarium::World* GetWorld();
bool DoFrame(); bool DoFrame();
private: private:
lunarium::game::World* mpWorld; lunarium::World* mpWorld;
}; };
} }
} }

@ -10,14 +10,38 @@
#define LUNARIUM_COMPONENTS_H_ #define LUNARIUM_COMPONENTS_H_
#include <core/common_defs.h> #include <core/common_defs.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/quaternion.hpp>
#include <string> #include <string>
namespace lunarium namespace lunarium
{ {
struct LabelComponent struct TagComponent
{
std::string Info;
TagComponent()
{
Info.reserve(256);
}
};
struct TransformComponent
{ {
std::string Label; glm::vec3 Position = { 0.0f, 0.0f, 0.0f };
glm::vec3 Rotation = { 0.0f, 0.0f, 0.0f };
glm::vec3 Scale = { 1.0f, 1.0f, 1.0f };;
glm::mat4 GetTransform()
{
// Quaternion code taken from Hazel engine
// https://github.com/TheCherno/Hazel/blob/dev/Hazel/src/Hazel/Scene/Components.h
return (glm::translate(glm::mat4(1.0f), Position) * glm::toMat4(glm::quat(Rotation)) * glm::scale(glm::mat4(1.0f), Scale ));
}
}; };
} }

@ -7,17 +7,30 @@
******************************************************************************/ ******************************************************************************/
#include "entity.h" #include "entity.h"
#include "world.h"
namespace lunarium namespace lunarium
{ {
Entity::Entity(entt::registry* r) Entity::Entity(World& w)
: mHandle(entt::null), mpRegistry(r) : mHandle(entt::null), mWorld(w)
{ {
mUUID = UUID::GetNewID(); mUUID = UUID::GetNewID();
Init();
} }
Entity::Entity(entt::registry* r, LUUID uuid) Entity::Entity(World& w, LUUID uuid, entt::entity handle)
: mHandle(entt::null), mpRegistry(r), mUUID(uuid) : mHandle(handle), mWorld(w), mUUID(uuid)
{ {
Init();
}
void Entity::Init()
{
mHandle = mWorld.GetEntityRegistry()->create();
}
LUUID Entity::GetUUID() const
{
return mUUID;
} }
} }

@ -12,6 +12,7 @@
#include <utils/uuid.h> #include <utils/uuid.h>
#include <utils/logger.h> #include <utils/logger.h>
#include <entt/entt.hpp> #include <entt/entt.hpp>
#include <world/world.h>
namespace lunarium namespace lunarium
{ {
@ -19,8 +20,8 @@ namespace lunarium
{ {
public: public:
// TODO: Maybe change this to take the World instead of the registry // TODO: Maybe change this to take the World instead of the registry
Entity(entt::registry* r); Entity(World& w);
Entity(entt::registry* r, LUUID uuid); Entity(World& w, LUUID uuid, entt::entity handle = entt::null);
// These component methods are based on methods from the Hazel engine, written by Cherno // These component methods are based on methods from the Hazel engine, written by Cherno
// https://github.com/TheCherno/Hazel/blob/dev/Hazel/src/Hazel/Scene/Entity.h // https://github.com/TheCherno/Hazel/blob/dev/Hazel/src/Hazel/Scene/Entity.h
@ -29,11 +30,10 @@ namespace lunarium
{ {
if (HasComponent<T>()) if (HasComponent<T>())
{ {
Log::Error(LogCategory::GAME_SYSTEM, "Entity already has component!"); Logger::Error(LogCategory::GAME_SYSTEM, "Entity already has component!");
return GetComponent<T>(); return GetComponent<T>();
} }
T& component = mpRegistry->emplace<T>(mHandle, std::forward<Args>(args)...); T& component = mWorld.GetEntityRegistry()->emplace<T>(mHandle, std::forward<Args>(args)...);
//m_Scene->OnComponentAdded<T>(*this, component);
return component; return component;
} }
@ -42,15 +42,15 @@ namespace lunarium
{ {
if (!HasComponent<T>()) if (!HasComponent<T>())
{ {
Log::Error(LogCategory::GAME_SYSTEM, "Entity does not have component!"); Logger::Error(LogCategory::GAME_SYSTEM, "Entity does not have component!");
} }
return mpRegistry->get<T>(mHandle); return mWorld.GetEntityRegistry()->get<T>(mHandle);
} }
template<typename T> template<typename T>
bool HasComponent() bool HasComponent()
{ {
return mpRegistry->all_of<T>(mHandle); return mWorld.GetEntityRegistry()->all_of<T>(mHandle);
} }
template<typename T> template<typename T>
@ -58,15 +58,21 @@ namespace lunarium
{ {
if (!HasComponent<T>()) if (!HasComponent<T>())
{ {
Log::Error(LogCategory::GAME_SYSTEM, "Entity does not have component!"); Logger::Error(LogCategory::GAME_SYSTEM, "Entity does not have component!");
} }
m_Scene->m_Registry.remove<T>(mHandle); mWorld.GetEntityRegistry()->remove<T>(mHandle);
} }
LUUID GetUUID() const;
private: private:
LUUID mUUID; LUUID mUUID;
entt::entity mHandle; entt::entity mHandle;
entt::registry* mpRegistry; World& mWorld; // The world the entity is contained within
//entt::registry* mpRegistry;
private:
void Init();
}; };
} }

@ -10,17 +10,24 @@
******************************************************************************/ ******************************************************************************/
#include "world.h" #include "world.h"
#include <utils/logger.h>
#include <assets/types/script.h> #include <assets/types/script.h>
#include <assets/types/image.h> #include <assets/types/image.h>
#include <graphics/graphics.h> #include <graphics/graphics.h>
#include <graphics/camera.h> #include <graphics/camera.h>
#include "entity.h"
namespace lunarium namespace lunarium
{ {
World::World(Camera* pCam, Sizei region_size, Sizei world_size) // World::World(Camera* pCam, Sizei region_size, Sizei world_size)
: mpCamera(pCam), mRegionSize(region_size), mpWorldScript(nullptr), mWorldSize(world_size) // : mpCamera(pCam), mRegionSize(region_size), mpWorldScript(nullptr), mWorldSize(world_size)
// {
// mActiveRegion = { 0, 0 };
// }
World::World()
{ {
mActiveRegion = { 0, 0 };
} }
void World::OnLoad() void World::OnLoad()
@ -69,4 +76,23 @@ namespace lunarium
return mpWorldScript; return mpWorldScript;
} }
entt::registry* World::GetEntityRegistry()
{
return &mECSRegistry;
}
LUUID World::CreateEntity()
{
//Logger::Error(LogCategory::GAME_SYSTEM, "World::CreateEntity not implemented!");
Entity* new_ent = new Entity(*this);
mEntities[new_ent->GetUUID()] = new_ent;
return new_ent->GetUUID();
}
Entity* World::GetEntity(LUUID id)
{
return mEntities[id];
}
} }

@ -12,6 +12,7 @@
#ifndef WORLD_H_ #ifndef WORLD_H_
#define WORLD_H_ #define WORLD_H_
#include <core/common_defs.h>
#include <core/types.h> #include <core/types.h>
#include <utils/op_res.h> #include <utils/op_res.h>
#include <entt/entt.hpp> #include <entt/entt.hpp>
@ -24,8 +25,9 @@ namespace lunarium
class Graphics; class Graphics;
class Image; class Image;
class Script; class Script;
class Camera; //class Camera;
class GameObject; //class GameObject;
class Entity;
} }
namespace lunarium namespace lunarium
@ -35,16 +37,17 @@ namespace lunarium
public: public:
struct Region struct Region
{ {
lunarium::Script* mRegionScript; bool IsActive;
Vec2i mCoords; lunarium::Script* RegionScript;
std::vector<GameObject*> mObjects; Vec2i Coords;
std::vector<Entity*> Entities;
std::vector<lunarium::Image*> mLayers; std::vector<lunarium::Image*> mLayers;
// Colliders
}; };
public: // INTERFACE public: // INTERFACE
World(Camera* pCam, Sizei region_size, Sizei world_size); //World(Camera* pCam, Sizei region_size, Sizei world_size);
World();
void OnLoad(); void OnLoad();
void OnUnload(); void OnUnload();
@ -59,17 +62,25 @@ namespace lunarium
void SetWorldScript(Script* pScript); void SetWorldScript(Script* pScript);
lunarium::Script* GetWorldScript(); lunarium::Script* GetWorldScript();
entt::registry* GetEntityRegistry();
LUUID CreateEntity();
Entity* GetEntity(LUUID id);
private: private:
entt::registry mECSRegistry; entt::registry mECSRegistry;
Camera* mpCamera; //Camera* mpCamera;
std::vector<GameObject*> mWorldObjects; //std::vector<GameObject*> mWorldObjects;
lunarium::Script* mpWorldScript; lunarium::Script* mpWorldScript;
Sizei mRegionSize; // in pixels Sizei mRegionSize; // in tiles
Sizei mWorldSize; // num regions ex. 10x10 Sizei mWorldSize; // num regions ex. 10x10
Vec2i mActiveRegion; Vec2i mActiveRegion;
// TEST STUFF
std::map<LUUID, Entity*> mEntities;
}; };
} }

Loading…
Cancel
Save