This commit is contained in:
Simon Lübeß
2022-11-16 13:09:26 +01:00
parent a42544f171
commit 0155a38ae6
8 changed files with 367 additions and 21 deletions
+53
View File
@@ -0,0 +1,53 @@
using Bon;
using System;
using Bon.Integrated;
using System.Collections;
namespace GlitchyEngine.Core
{
[BonTarget]
struct UUID : IHashable
{
[BonInclude]
private uint64 _uuid;
private static Random s_Random = new .() ~ delete _;
static this()
{
gBonEnv.typeHandlers.Add(typeof(UUID),
((.)new => Serialize, (.)new => Deserialize));
}
/// Creates a new random UUID.
public this()
{
_uuid = s_Random.NextU64();
}
/// Creates a new UUID with the given value.
public this(uint64 uuid)
{
_uuid = uuid;
}
public int GetHashCode()
{
return (int)_uuid;
}
static void Serialize(BonWriter writer, ValueView val, BonEnvironment env)
{
UUID uuid = *(UUID*)val.dataPtr;
Bon.Integrated.Serialize.[Friend]Integer(typeof(uint64), writer, ValueView(typeof(uint64), &uuid._uuid));
}
public static Result<void> Deserialize(BonReader reader, ValueView val, BonEnvironment env)
{
Bon.Integrated.Deserialize.[Friend]Integer!(typeof(uint64), reader, val);
return .Ok;
}
}
}
+17
View File
@@ -1,6 +1,7 @@
using System;
using GlitchyEngine.Math;
using GlitchyEngine.Renderer;
using GlitchyEngine.Core;
namespace GlitchyEngine.World
{
@@ -15,6 +16,22 @@ namespace GlitchyEngine.World
}
}
struct IDComponent
{
public readonly UUID ID;
/// Create aa new IDComponent with a random UUID.
public this()
{
ID = UUID();
}
public this(UUID id)
{
ID = id;
}
}
/// If an entity has the EditorComponent it won't be displayed in the scene hierarchy.
struct EditorComponent
{
+3
View File
@@ -1,5 +1,6 @@
using System;
using System.Collections;
using GlitchyEngine.Core;
using internal GlitchyEngine.World;
@@ -63,6 +64,8 @@ namespace GlitchyEngine.World
}
}
public UUID UUID => GetComponent<IDComponent>().ID;
public TransformComponent* Transform => GetComponent<TransformComponent>();
public T* AddComponent<T>(T value = T()) where T: struct, new
+26 -1
View File
@@ -3,6 +3,7 @@ using GlitchyEngine.Renderer;
using System;
using System.Collections;
using Box2D;
using GlitchyEngine.Core;
namespace GlitchyEngine.World
{
@@ -24,6 +25,9 @@ namespace GlitchyEngine.World
private Effect _gammaCorrectEffect ~ _.ReleaseRef();
// Maps ids to the entities they represent.
private Dictionary<UUID, EcsEntity> _idToEntity = new .() ~ delete _;
public Entity ActiveCamera => {
Entity cameraEntity = .();
@@ -205,6 +209,8 @@ namespace GlitchyEngine.World
}
}
renderTarget = _cameraTarget..AddRef();
// 3D render
Renderer.BeginScene(*primaryCamera, primaryCameraTransform, renderTarget, _compositeTarget);
@@ -468,13 +474,20 @@ namespace GlitchyEngine.World
}
/// Creates a new Entity with the given name.
public Entity CreateEntity(String name = "")
public Entity CreateEntity(String name = "", UUID id = default)
{
Entity entity = Entity(_ecsWorld.NewEntity(), this);
entity.AddComponent<TransformComponent>();
let nameComponent = entity.AddComponent<DebugNameComponent>();
nameComponent.SetName(name.IsEmpty ? "Entity" : name);
// If no id is given generate a random one.
IDComponent idComponent = (id == default) ? IDComponent() : IDComponent(id);
entity.AddComponent<IDComponent>(idComponent);
_idToEntity.Add(idComponent.ID, entity.Handle);
return entity;
}
@@ -485,6 +498,8 @@ namespace GlitchyEngine.World
*/
public void DestroyEntity(Entity entity, bool destroyChildren = false)
{
_idToEntity.Remove(entity.UUID);
if (destroyChildren)
{
for (Entity child in entity.EnumerateChildren)
@@ -496,6 +511,16 @@ namespace GlitchyEngine.World
_ecsWorld.RemoveEntity(entity.Handle);
}
public Result<Entity> GetEntityByID(UUID id)
{
if (_idToEntity.TryGetValue(id, let ecsEntity))
{
return .Ok(Entity(ecsEntity, this));
}
return .Err;
}
private uint32 ViewportWidth, ViewportHeight;
/// Sets the size of the viewport into which the scene will be rendered.
+44 -9
View File
@@ -4,6 +4,8 @@ using System;
using System.Reflection;
using System.IO;
using GlitchyEngine.Math;
using GlitchyEngine.Core;
using System.Collections;
namespace GlitchyEngine.World
{
@@ -13,6 +15,11 @@ namespace GlitchyEngine.World
{
private Scene _scene;
// Maps from ParentID to ChildEntity
private Dictionary<UUID, Entity> _parentIdToChild;
private List<(Entity Entity, UUID ParentId)> _entitiesMissingParent;
public this(Scene scene)
{
_scene = scene;
@@ -60,14 +67,13 @@ namespace GlitchyEngine.World
File.WriteAllText(filePath, buffer);
}
static void SerializeEntity(BonWriter writer, Entity entity)
void SerializeEntity(BonWriter writer, Entity entity)
{
writer.EntryStart();
using (writer.ObjectBlock())
{
// TODO: Entity GUID goes here!
Serialize.Value(writer, "Id", entity.Handle.Index);
Serialize.Value(writer, "Id", entity.UUID);
SerializeComponent<EditorComponent>(writer, entity, "EditorComponent", scope (component) => {});
@@ -88,7 +94,10 @@ namespace GlitchyEngine.World
{
// TODO: Use GUIDs
if (component.Parent != .InvalidEntity)
Serialize.Value(writer, "ParentId", component.Parent.Index);
{
Entity parent = Entity(component.Parent, _scene);
Serialize.Value(writer, "ParentId", parent.UUID);
}
Serialize.Value(writer, "Position", component.Position);
Serialize.Value(writer, "Rotation", component.Rotation);
@@ -177,6 +186,9 @@ namespace GlitchyEngine.World
{
Debug.Profiler.ProfileResourceFunction!();
_parentIdToChild = scope Dictionary<UUID, Entity>();
_entitiesMissingParent = scope List<(Entity Entity, UUID ParentId)>();
String buffer = scope String();
File.ReadAllText(filePath, buffer);
@@ -217,6 +229,19 @@ namespace GlitchyEngine.World
Try!(Deserialize.End(reader));
// Find parents for entities that don't have their parent yet
for ((Entity Entity, UUID ParentId) entry in _entitiesMissingParent)
{
let parentResult = _scene.GetEntityByID(entry.ParentId);
Log.EngineLogger.Assert(parentResult case .Ok, "Parent entity does not exist.");
if (parentResult case .Ok(let parent))
{
entry.Entity.Parent = parent;
}
}
return .Ok;
}
@@ -224,10 +249,9 @@ namespace GlitchyEngine.World
{
Try!(reader.ObjectBlock());
Entity entity = _scene.CreateEntity();
Deserialize.Value<uint64>(reader, "Id", let uuid);
// TODO: GUID?
Deserialize.Value<uint32>(reader, "Id", let id);
Entity entity = _scene.CreateEntity("", UUID(uuid));
while(reader.ObjectHasMore())
{
@@ -282,10 +306,21 @@ namespace GlitchyEngine.World
let nextId = Try!(reader.Identifier());
if (nextId == "ParentId")
{
// TODO: Use GUIDs
uint32 pId;
UUID pId;
Deserialize.Value(reader, out pId);
reader.EntryEnd();
var parentEntity = _scene.GetEntityByID(pId);
if (parentEntity case .Ok(let parent))
{
component.Parent = parent.Handle;
}
else
{
_entitiesMissingParent.Add((entity, pId));
}
Deserialize.Value(reader, "Position", out component.[Friend]_position);
reader.EntryEnd();
}