-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tests - add Test_Tree_traverse() example
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |