Skip to content

Instantly share code, notes, and snippets.

@dfkeenan
Created December 22, 2016 12:40
Show Gist options
  • Select an option

  • Save dfkeenan/1431de970f3d867c179ced9a66fabe3e to your computer and use it in GitHub Desktop.

Select an option

Save dfkeenan/1431de970f3d867c179ced9a66fabe3e to your computer and use it in GitHub Desktop.
Xenko Entity Extensions
using System.Collections.Generic;
using SiliconStudio.Xenko.Engine;
namespace MyGame
{
public static class EntityExtensions
{
public static IEnumerable<T> GetComponentsInChildren<T>(this Entity entity) where T : EntityComponent
{
//depth first
var stack = new Stack<Entity>();
stack.Push(entity);
while (stack.Count > 0)
{
var current = stack.Pop();
foreach (var component in current.GetAll<T>())
{
yield return component;
}
var children = current.Transform.Children;
for (int i = 0; i < children.Count; i++)
{
stack.Push(children[i].Entity);
}
}
}
public static T GetComponentInChildren<T>(this Entity entity) where T : EntityComponent
{
//breadth first
var queue = new Queue<Entity>();
queue.Enqueue(entity);
while (queue.Count > 0)
{
var current = queue.Dequeue();
var component = current.Get<T>();
if (component != null)
return component;
var children = current.Transform.Children;
for (int i = 0; i < children.Count; i++)
{
queue.Enqueue(children[i].Entity);
}
}
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment