UI Tweaks, ChildEnumerator, ComponentAdded-Handlers

This commit is contained in:
Simon Lübeß
2022-04-12 00:48:20 +02:00
parent bec202e23a
commit 863c264001
7 changed files with 278 additions and 83 deletions
+78 -1
View File
@@ -1,4 +1,5 @@
using System;
using System.Collections;
using internal GlitchyEngine.World;
@@ -24,13 +25,53 @@ namespace GlitchyEngine.World
_scene = scene;
}
public ChildEnumerator EnumerateChildren => .(this);
public bool IsValid => _entity.IsValid;
public Entity? Parent
{
get
{
var cmp = GetComponent<TransformComponent>();
if (cmp.Parent == .InvalidEntity)
return null;
return .(cmp.Parent, _scene);
}
set
{
if (value == null)
{
var cmp = GetComponent<TransformComponent>();
cmp.Parent = .InvalidEntity;
}
else
{
Entity parent = value.Value;
if (parent.Scene != _scene)
{
Log.EngineLogger.AssertDebug(false);
return;
}
var cmp = GetComponent<TransformComponent>();
cmp.Parent = parent._entity;
}
}
}
public T* AddComponent<T>(T value = T()) where T: struct, new
{
Log.EngineLogger.AssertDebug(!HasComponent<T>(), scope $"Entity already has component.");
return _scene._ecsWorld.AssignComponent<T>(_entity, value);
T* component = _scene._ecsWorld.AssignComponent<T>(_entity, value);
_scene.[Friend]OnComponentAdded(this, typeof(T), component);
return component;
}
public T* GetComponent<T>() where T: struct, new
@@ -51,5 +92,41 @@ namespace GlitchyEngine.World
_scene._ecsWorld.RemoveComponent<T>(_entity);
}
public struct ChildEnumerator : IEnumerator<Entity>, IDisposable
{
private WorldEnumerator<TransformComponent> _transformEnum;
private EcsEntity _entity;
private Entity _currentChild;
public this(Entity entity)
{
_entity = entity.Handle;
_transformEnum = entity.Scene._ecsWorld.Enumerate<TransformComponent>();
_currentChild = .(.InvalidEntity, entity.Scene);
}
public Entity Current => _currentChild;
public Result<Entity> GetNext() mut
{
while (true)
{
(EcsEntity entity, TransformComponent* transform) = Try!(_transformEnum.GetNext());
if (transform.Parent == _entity)
{
_currentChild.[Friend]_entity = entity;
return .Ok(_currentChild);
}
}
}
public void Dispose()
{
_transformEnum.Dispose();
}
}
}
}