diff --git a/src/Tests/ECS/Entity/Test_Tree.cs b/src/Tests/ECS/Entity/Test_Tree.cs new file mode 100644 index 00000000..03cf8c86 --- /dev/null +++ b/src/Tests/ECS/Entity/Test_Tree.cs @@ -0,0 +1,48 @@ +using System.Numerics; +using Friflo.Engine.ECS; +using NUnit.Framework; +using static NUnit.Framework.Assert; + +// ReSharper disable InconsistentNaming +// ReSharper disable once CheckNamespace +namespace Tests.ECS +{ +public struct GlobalPosition : IComponent +{ + public Vector3 value; + public readonly override string ToString() => value.ToString(); +} + +public static class Test_Tree +{ + + [Test] + public static void Test_Tree_traverse() + { + var store = new EntityStore(); + var root = store.CreateEntity(new GlobalPosition()); + var child1 = store.CreateEntity(new GlobalPosition(), new Position(5, 0, 0)); + var child2 = store.CreateEntity(new GlobalPosition(), new Position(0, 3, 0)); + root.AddChild(child1); + root.AddChild(child2); + + TraverseHierarchy(root); + + AreEqual(new Vector3(5,0,0), child1.GetComponent().value); + AreEqual(new Vector3(0,3,0), child2.GetComponent().value); + } + + private static void TraverseHierarchy(Entity parent) + { + var parentGlobal = parent.GetComponent().value; + foreach (var child in parent.ChildEntities) { + var data = child.Data; + if (!data.Has()) { + continue; + } + data.Get().value = parentGlobal + data.Get().value; + TraverseHierarchy(child); + } + } +} +} \ No newline at end of file