2022-11-25 09:11:46 -08:00
|
|
|
using Godot;
|
|
|
|
|
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>
|
|
|
|
/// Iterates through each ancestor until it finds an ancestor of type
|
|
|
|
/// <c>T</c>
|
|
|
|
/// </summary>
|
|
|
|
public static T GetAncestor<T>(this Node node) where T : Node
|
2022-11-25 09:11:46 -08:00
|
|
|
{
|
2023-06-03 18:21:46 -07:00
|
|
|
Node parent;
|
2022-11-25 09:11:46 -08:00
|
|
|
|
2023-06-03 18:21:46 -07:00
|
|
|
while ((parent = node.GetParent()) != null)
|
|
|
|
{
|
|
|
|
if (parent is T t)
|
2022-11-25 09:11:46 -08:00
|
|
|
{
|
2023-06-03 18:21:46 -07:00
|
|
|
return t;
|
2022-11-25 09:11:46 -08:00
|
|
|
}
|
|
|
|
|
2023-06-03 18:21:46 -07:00
|
|
|
node = parent;
|
2022-11-25 09:11:46 -08:00
|
|
|
}
|
2022-11-25 11:59:55 -08:00
|
|
|
|
2023-06-03 18:21:46 -07:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <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
|
|
|
}
|
|
|
|
}
|