Traveller/scripts/utils/NodeUtils.cs

95 lines
3.0 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using System.Threading.Tasks;
using Godot;
2024-04-28 13:55:19 +00:00
namespace ColdMint.scripts.utils;
public class NodeUtils
{
/// <summary>
/// <para>Delete all child nodes</para>
/// <para>删除所有子节点</para>
/// </summary>
/// <param name="parent"></param>
public static int DeleteAllChild(Node parent)
2024-04-28 13:55:19 +00:00
{
var deleteNumber = 0;
2024-04-28 13:55:19 +00:00
var count = parent.GetChildCount();
if (count <= 0) return deleteNumber;
for (var i = 0; i < count; i++)
2024-04-28 13:55:19 +00:00
{
var node = parent.GetChild(0);
parent.RemoveChild(node);
node.QueueFree();
deleteNumber++;
2024-04-28 13:55:19 +00:00
}
return deleteNumber;
}
/// <summary>
/// <para>All child nodes are removed asynchronously</para>
/// <para>异步删除所有子节点</para>
/// </summary>
/// <param name="parent"></param>
/// <returns></returns>
public static async Task<int> DeleteAllChildAsync(Node parent)
{
return await Task.Run(() => DeleteAllChild(parent));
2024-04-28 13:55:19 +00:00
}
/// <summary>
/// <para>Gets the node closest to the origin</para>
/// <para>获取距离原点最近的节点</para>
/// </summary>
/// <param name="origin">
///<para>origin</para>
///<para>原点</para>
/// </param>
/// <param name="array">
///<para>Node array</para>
///<para>节点数组</para>
/// </param>
/// <param name="exclude">
///<para>Which nodes are excluded</para>
///<para>排除哪些节点</para>
/// </param>
/// <param name="excludeInvisibleNodes">
///<para>Whether or not unseen nodes should be excluded</para>
///<para>是否排除不可见的节点</para>
/// </param>
/// <returns></returns>
public static Node2D? GetTheNearestNode(Node2D origin, Node[] array, HashSet<Node>? exclude = null,
bool excludeInvisibleNodes = true)
{
var closestDistance = float.MaxValue;
Node2D? closestNode = null;
foreach (var node in array)
{
if (node is not Node2D node2D) continue;
if (excludeInvisibleNodes && !node2D.Visible)
{
//If invisible nodes are excluded and the current node is invisible, then the next.
//如果排除不可见的节点,且当前节点就是不可见的,那么下一个。
continue;
}
if (exclude != null && exclude.Contains(node))
{
//If the current node, is within our exclusion project. So the next one.
//如果当前节点,在我们的排除项目内。那么下一个。
continue;
}
var distance = node2D.GlobalPosition - origin.GlobalPosition;
var distanceLength = distance.Length();
if (distanceLength < closestDistance)
{
closestDistance = distanceLength;
closestNode = node2D;
}
}
return closestNode;
}
2024-04-28 13:55:19 +00:00
}