Created
December 22, 2016 12:40
-
-
Save dfkeenan/1431de970f3d867c179ced9a66fabe3e to your computer and use it in GitHub Desktop.
Xenko Entity Extensions
This file contains hidden or 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
| 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