using Godot; using IEnumerableNode = System.Collections.Generic.IEnumerable; namespace SupaLidlGame.Extensions; public static class NodeExtensions { /// /// A version GetNode that returns null rather than cause an /// exception if the node is not found or is not the same type. /// /// /// null if name does not match /// a valid Node /// public static T GetN(this Node node, string name) where T : Node { return node.GetNode(name) as T; } public static T FindChildOfType(this Node node) where T : Node { foreach (Node child in node.GetChildren()) { if (child is T t) { return t; } } return null; } public static State.Global.GlobalState GetGlobalState(this Node node) { return node.GetNode("/root/GlobalState"); } public static Events.EventBus GetEventBus(this Node node) { return node.GetNode("/root/EventBus"); } public static Utils.World GetWorld(this Node node) { return node.GetNode("/root/World"); } public static CanvasLayer GetUI(this Node node) { return node.GetNode("/root/BaseUI"); } public static UI.UIController GetMainUI(this Node node) { return node.GetNode("/root/BaseUI/" + "SubViewportContainer/UIViewport/CanvasLayer/MainUILayer/Main"); } public static IEnumerableNode GetChildrenEnumerable(this Node node) { int childCount = node.GetChildCount(); for (int i = 0; i < childCount; i++) { yield return node.GetChild(i); } } }