added UUID

This commit is contained in:
Erris
2026-03-06 18:07:51 +01:00
parent 5be3c61859
commit 2d3fe57d5a
10 changed files with 111 additions and 23 deletions

View File

@@ -1,4 +1,3 @@
#include "open_engine/scene/components.hpp"
#include "open_engine/scene/entity.hpp"
#include "editor_component.hpp"

View File

@@ -1,6 +1,6 @@
[Window][WindowOverViewport_11111111]
Pos=0,24
Size=2560,1371
Size=1272,1363
Collapsed=0
[Window][Debug##Default]
@@ -10,31 +10,31 @@ Collapsed=0
[Window][Statistics]
Pos=0,24
Size=224,439
Size=224,437
Collapsed=0
DockId=0x00000003,0
[Window][Properties]
Pos=2110,24
Size=450,810
Pos=822,24
Size=450,805
Collapsed=0
DockId=0x00000007,0
[Window][Viewport]
Pos=226,61
Size=1882,964
Size=594,956
Collapsed=0
DockId=0x00000012,0
[Window][Dear ImGui Demo]
Pos=2110,836
Size=450,559
Pos=822,831
Size=450,556
Collapsed=0
DockId=0x00000008,0
[Window][Scene]
Pos=0,465
Size=224,930
Pos=0,463
Size=224,924
Collapsed=0
DockId=0x00000004,0
@@ -143,8 +143,8 @@ Collapsed=0
DockId=0x00000012,1
[Window][Assets]
Pos=226,1027
Size=1882,368
Pos=226,1019
Size=594,368
Collapsed=0
DockId=0x0000000C,0
@@ -156,12 +156,12 @@ DockId=0x0000000F,0
[Window][##play_state_bar]
Pos=226,24
Size=1882,35
Size=594,35
Collapsed=0
DockId=0x00000011,0
[Docking][Data]
DockSpace ID=0x08BD597D Window=0x1BBC0F80 Pos=0,24 Size=2560,1371 Split=X
DockSpace ID=0x08BD597D Window=0x1BBC0F80 Pos=0,24 Size=1272,1363 Split=X
DockNode ID=0x00000005 Parent=0x08BD597D SizeRef=820,1386 Split=X
DockNode ID=0x00000001 Parent=0x00000005 SizeRef=224,1386 Split=Y Selected=0xE601B12F
DockNode ID=0x00000003 Parent=0x00000001 SizeRef=255,417 Selected=0x553E127E
@@ -178,6 +178,6 @@ DockSpace ID=0x08BD597D Window=0x1BBC0F80 Pos=0,24 Size=2560,1371
DockNode ID=0x0000000C Parent=0x00000009 SizeRef=409,368 Selected=0x42C24103
DockNode ID=0x0000000A Parent=0x00000002 SizeRef=483,600 Selected=0x1BCA3180
DockNode ID=0x00000006 Parent=0x08BD597D SizeRef=450,1386 Split=Y Selected=0x8C72BEA8
DockNode ID=0x00000007 Parent=0x00000006 SizeRef=444,810 Selected=0x8C72BEA8
DockNode ID=0x00000008 Parent=0x00000006 SizeRef=444,559 Selected=0x5E5F7166
DockNode ID=0x00000007 Parent=0x00000006 SizeRef=444,805 Selected=0x8C72BEA8
DockNode ID=0x00000008 Parent=0x00000006 SizeRef=444,556 Selected=0x5E5F7166

View File

@@ -0,0 +1,36 @@
#ifndef UUID_HPP
#define UUID_HPP
#include <cstdint>
namespace OpenEngine {
class UUID
{
public:
UUID();
UUID(uint64_t uuid);
UUID(const UUID&) = default;
operator uint64_t() const { return uuid; };
private:
uint64_t uuid;
};
}
namespace std {
template<>
struct hash<OpenEngine::UUID>
{
std::size_t operator()(const OpenEngine::UUID& uuid) const
{
return hash<uint64_t>()((uint64_t)uuid);
}
};
}
#endif // UUID_HPP

View File

@@ -1,12 +1,12 @@
#ifndef COMPONENTS_HPP
#define COMPONENTS_HPP
#include "open_engine/scene/native_scriptable_entity.hpp"
#include "open_engine/renderer/renderer3d.hpp"
#include "open_engine/scene/scene_camera.hpp"
#include "open_engine/renderer/texture.hpp"
#include "open_engine/renderer/renderer3d.hpp"
#include "open_engine/physics.hpp"
#include "open_engine/core/uuid.hpp"
#include <Jolt/Physics/Body/BodyCreationSettings.h>
#include <Jolt/Physics/Body/BodyInterface.h>
@@ -28,6 +28,15 @@
namespace OpenEngine {
struct IDComponent
{
UUID id;
IDComponent() = default;
IDComponent(const IDComponent&) = default;
IDComponent(const UUID& uuid) : id(uuid) {};
};
struct TagComponent
{
std::string tag;
@@ -74,6 +83,8 @@ namespace OpenEngine {
CameraComponent(const CameraComponent&) = default;
};
class NativeScriptableEntity;
struct NativeScriptComponent
{
NativeScriptableEntity* instance = nullptr;
@@ -81,9 +92,6 @@ namespace OpenEngine {
NativeScriptableEntity* (*InstanciateScript)();
void (*DestroyInstanceScript)(NativeScriptComponent*);
void OnImGuiRender(Entity& entity)
{};
template <typename T>
void Bind()
{

View File

@@ -3,6 +3,8 @@
#include "open_engine/core.hpp"
#include "open_engine/core/uuid.hpp"
#include "open_engine/scene/components.hpp"
#include "open_engine/scene/scene.hpp"
#include <cstdint>
@@ -48,6 +50,8 @@ namespace OpenEngine {
return scene->registry.all_of<T>(handle);
};
UUID GetUUID() { return GetComponents<IDComponent>().id; };
operator bool() const { return handle != entt::null; };
operator entt::entity() const { return handle; };
operator uint32_t() const { return (uint32_t)handle; };

View File

@@ -1,6 +1,7 @@
#ifndef NATIVE_SCRIPTABLE_ENTITY_HPP
#define NATIVE_SCRIPTABLE_ENTITY_HPP
#include "open_engine/scene/native_scriptable_entity.hpp"
#include "open_engine/scene/entity.hpp"
namespace OpenEngine {

View File

@@ -1,6 +1,7 @@
#ifndef SCENE_HPP
#define SCENE_HPP
#include "open_engine/core/uuid.hpp"
#include "open_engine/renderer/editor_camera.hpp"
#include "open_engine/physics.hpp"
@@ -24,6 +25,7 @@ namespace OpenEngine {
void OnRuntimeStop();
Entity CreateEntity(const std::string& name = std::string());
Entity CreateEntityWithUUID(UUID uuid, const std::string& name = std::string());
void DeleteEntity(entt::entity entity);
void MarkEntityForDeletion(Entity entity);

View File

@@ -0,0 +1,24 @@
#include <cstdint>
#include <pch.hpp>
#include <core/uuid.hpp>
#include <random>
namespace OpenEngine {
static std::random_device s_random_device;
static std::mt19937_64 s_engine(s_random_device());
static std::uniform_int_distribution<uint64_t> s_uniform_distribution;
UUID::UUID()
: uuid(s_uniform_distribution(s_engine))
{
}
UUID::UUID(uint64_t uuid)
: uuid(uuid)
{
}
}

View File

@@ -8,6 +8,7 @@
#include <renderer/renderer3d.hpp>
#include <scene/components.hpp>
#include <open_engine/scene/native_scriptable_entity.hpp>
#include <scene/entity.hpp>
#include <scene/scene.hpp>
#include <core/time.hpp>
@@ -98,9 +99,15 @@ namespace OpenEngine {
}
Entity Scene::CreateEntity(const std::string& name)
{
return CreateEntityWithUUID(UUID(), name);
}
Entity Scene::CreateEntityWithUUID(UUID uuid, const std::string& name)
{
Entity entity = { registry.create(), this };
entity.AddComponent<IDComponent>(uuid);
auto& tag = entity.AddComponent<TagComponent>();
tag.tag = name.empty() ? "Entity" : name;
@@ -340,4 +347,9 @@ namespace OpenEngine {
void Scene::OnComponentAdded<BoxShapeComponent>(Entity entity, BoxShapeComponent& component)
{
}
template<>
void Scene::OnComponentAdded<IDComponent>(Entity entity, IDComponent& component)
{
}
}

View File

@@ -88,8 +88,10 @@ namespace OpenEngine {
static void SerializeEntity(YAML::Emitter& out, Entity entity)
{
OE_CORE_ASSERT(entity.HasComponent<IDComponent>(), "Entity is missing UUID.");
out << YAML::BeginMap;
out << YAML::Key << "Entity" << YAML::Value << "412741205"; // Needs random
out << YAML::Key << "Entity" << YAML::Value << entity.GetUUID(); // Needs random
if (entity.HasComponent<TagComponent>())
{
@@ -258,7 +260,7 @@ namespace OpenEngine {
{
for (auto entity : entities)
{
uint64_t uuid = entity["Entity"].as<uint64_t>(); // TODO
uint64_t uuid = entity["Entity"].as<uint64_t>();
std::string name;
auto tagComponent = entity["TagComponent"];
@@ -267,7 +269,7 @@ namespace OpenEngine {
OE_CORE_TRACE("Deserialized entity with ID = {0}, name = {1}", uuid, name);
Entity deserializedEntity = context->CreateEntity(name);
Entity deserializedEntity = context->CreateEntityWithUUID(uuid, name);
auto transformComponent = entity["TransformComponent"];
if (transformComponent)