SupaLidlGame/Extensions/Node.cs

69 lines
1.8 KiB
C#
Raw Normal View History

2022-11-25 09:11:46 -08:00
using Godot;
using IEnumerableNode = System.Collections.Generic.IEnumerable<Godot.Node>;
2022-11-25 09:11:46 -08:00
2023-06-03 18:21:46 -07:00
namespace SupaLidlGame.Extensions;
public static class NodeExtensions
2022-11-25 09:11:46 -08:00
{
2023-06-03 18:21:46 -07:00
/// <summary>
/// A version <c>GetNode</c> that returns null rather than cause an
/// exception if the node is not found or is not the same type.
/// </summary>
/// <returns>
/// <see langword="null">null</see> if <param>name</param> does not match
/// a valid Node
/// </returns>
public static T GetN<T>(this Node node, string name) where T : Node
{
return node.GetNode(name) as T;
2022-11-25 09:11:46 -08:00
}
2023-07-23 23:39:20 -07:00
public static T FindChildOfType<T>(this Node node) where T : Node
{
foreach (Node child in node.GetChildren())
{
if (child is T t)
{
return t;
}
}
return null;
}
2023-08-01 23:49:48 -07:00
public static State.Global.GlobalState GetGlobalState(this Node node)
{
return node.GetNode<State.Global.GlobalState>("/root/GlobalState");
}
public static Events.EventBus GetEventBus(this Node node)
{
return node.GetNode<Events.EventBus>("/root/EventBus");
}
2023-08-03 16:17:34 -07:00
public static Utils.World GetWorld(this Node node)
{
return node.GetNode<Utils.World>("/root/World");
}
2023-08-13 16:49:18 -07:00
public static CanvasLayer GetUI(this Node node)
{
return node.GetNode<CanvasLayer>("/root/BaseUI");
}
public static UI.UIController GetMainUI(this Node node)
{
return node.GetNode<UI.UIController>("/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);
}
}
2022-11-25 09:11:46 -08:00
}