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

@@ -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)