using Godot.Collections; namespace Godot.Sharp.Extended.Tools; public static class NodeExtensions { public static void AddToGroup(this Node node) => node.AddToGroup(node.GetType().Name); public static T GetNode(this Node node) where T : Node => node.GetNode(typeof(T).Name); public static Array GetChildrenOfType(this Node node) where T : Node { var children = node.GetChildren(); return new Array(children.OfType().ToArray()); } public static T? GetFirstChildOfType(this Node node) where T : Node => GetChildrenOfType(node).FirstOrDefault(); public static void RemoveAllChildren(this Node node) { foreach (var child in node.GetChildren()) node.RemoveChild(child); } public static void RemoveAndQueueFreeChildren(this Node node) { foreach (var child in node.GetChildren()) { node.RemoveChild(child); child.QueueFree(); } } public static void QueueFreeChildren(this Node node) { foreach (var child in node.GetChildren()) node.QueueFree(); } public static T? GetAncestor(this Node node) where T : Node { Node current = node; Node root = node.GetTree().Root; while (current != root && !(current is T)) current = current.GetParent(); return current as T; } public static Node? GetLastChild(this Node node) { var count = node.GetChildCount(); if (count == 0) return null; return node.GetChild(count - 1); } public static T? GetLastChild(this Node node) where T : Node { Node? last = null; foreach (var child in node.GetChildren()) { if (child is T) last = child; } return last as T; } }