70 lines
1.7 KiB
C#
70 lines
1.7 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Godot;
|
|
using Org.BouncyCastle.Crypto.Digests;
|
|
|
|
namespace BITKit;
|
|
/// <summary>
|
|
/// 为Godot.Node提供数学工具
|
|
/// </summary>
|
|
public static partial class MathNode
|
|
{
|
|
public static T Cast<T>(this Node node) where T : Node
|
|
{
|
|
return (T)node.GetNode(new NodePath(node.Name));
|
|
}
|
|
/// <summary>
|
|
/// 获取Node下所有的子Node节点
|
|
/// </summary>
|
|
/// <param name="self">Root Node</param>
|
|
/// <returns></returns>
|
|
public static IEnumerable<Node> GetAllNode(Node self)
|
|
{
|
|
List<Node> nodes = new() { self };
|
|
For(self);
|
|
return nodes.Distinct();
|
|
|
|
void For(Node node)
|
|
{
|
|
foreach (var x in node.GetChildren())
|
|
{
|
|
For(x);
|
|
nodes.Add(x);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static IEnumerable<Node> GetNodesInParent(Node self)
|
|
{
|
|
var nodes = new List<Node>() { self };
|
|
var parent = self.GetParent();
|
|
while (parent is not null)
|
|
{
|
|
nodes.Add(parent);
|
|
parent = parent.GetParent();
|
|
}
|
|
return nodes.Distinct();
|
|
}
|
|
public static bool TryGetNodeInParent<T>(Node self, out T node)
|
|
{
|
|
node = default;
|
|
var nodes = GetNodesInParent(self);
|
|
if (nodes.FirstOrDefault(x => x is T) is not T t) return false;
|
|
node = t;
|
|
return true;
|
|
}
|
|
public static void ClearChild(Node self)
|
|
{
|
|
foreach (var x in self.GetChildren())
|
|
{
|
|
x.QueueFree();
|
|
}
|
|
}
|
|
|
|
public static T Create<T>(this Node self) where T : Node,new()
|
|
{
|
|
var t = new T();
|
|
self.AddChild(t);
|
|
return t;
|
|
}
|
|
} |