Skip to content

Commit

Permalink
Tests - add Test_Tree_traverse() example
Browse files Browse the repository at this point in the history
  • Loading branch information
friflo committed Aug 11, 2024
1 parent fd6b977 commit e883d5d
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/Tests/ECS/Entity/Test_Tree.cs
Original file line number Diff line number Diff line change
@@ -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<GlobalPosition>().value);
AreEqual(new Vector3(0,3,0), child2.GetComponent<GlobalPosition>().value);
}

private static void TraverseHierarchy(Entity parent)
{
var parentGlobal = parent.GetComponent<GlobalPosition>().value;
foreach (var child in parent.ChildEntities) {
var data = child.Data;
if (!data.Has<Position>()) {
continue;
}
data.Get<GlobalPosition>().value = parentGlobal + data.Get<Position>().value;
TraverseHierarchy(child);
}
}
}
}

0 comments on commit e883d5d

Please sign in to comment.