iFactory.Godot/BITKit/Scripts/Extensions/Node.cs

70 lines
1.7 KiB
C#
Raw Permalink Normal View History

2023-06-12 15:51:41 +08:00
using System.Collections.Generic;
using System.Linq;
using Godot;
2024-04-16 04:15:21 +08:00
using Org.BouncyCastle.Crypto.Digests;
2023-06-12 15:51:41 +08:00
namespace BITKit;
/// <summary>
/// 为Godot.Node提供数学工具
/// </summary>
public static partial class MathNode
{
2024-04-16 04:15:21 +08:00
public static T Cast<T>(this Node node) where T : Node
{
return (T)node.GetNode(new NodePath(node.Name));
}
2023-06-12 15:51:41 +08:00
/// <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);
2024-04-16 04:15:21 +08:00
return nodes.Distinct();
2023-06-12 15:51:41 +08:00
void For(Node node)
{
foreach (var x in node.GetChildren())
{
For(x);
nodes.Add(x);
}
}
}
2023-07-08 00:02:32 +08:00
2024-04-16 04:15:21 +08:00
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)
2023-07-08 00:02:32 +08:00
{
foreach (var x in self.GetChildren())
{
x.QueueFree();
}
}
2023-07-17 04:10:14 +08:00
public static T Create<T>(this Node self) where T : Node,new()
{
var t = new T();
self.AddChild(t);
return t;
}
2023-06-12 15:51:41 +08:00
}