using Godot; namespace Cirno.Scripts; public static class Tools { public static T CreateChild(this Node2D node, PackedScene prefab) where T : Node2D { return CreateChild(node, prefab, node.GlobalPosition); } public static T CreateChild(this Node2D node, PackedScene prefab, Vector2 position) where T : Node2D { return CreateChildOf(node, node, prefab, position); // if (prefab == null) return null; // var newInstance = prefab.Instantiate(); // node.GetParent().CallDeferred("add_child", newInstance); // // Need to use parent instead of owner because tilemap scenes have no owner // //node.Owner.CallDeferred("add_child", newInstance); // newInstance.Transform = node.GlobalTransform; // newInstance.Position = position; // // return newInstance; } public static T CreateSibling(this Node2D node, PackedScene prefab) where T : Node2D { return CreateSibling(node, prefab, node.GlobalPosition); } public static T CreateSibling(this Node2D node, PackedScene prefab, Vector2 position) where T : Node2D { return CreateChildOf(node, node.GetParent(), prefab, position); } public static T CreateChildOf(this Node2D node, Node2D parentNode, PackedScene prefab) where T : Node2D { return CreateChildOf(node, parentNode, prefab, node.GlobalPosition); } public static T CreateChildOf(this Node2D node, Node2D parentNode, PackedScene prefab, Vector2 position) where T : Node2D { if (prefab == null) { GD.PrintErr("Tried to instantiate a null prefab"); return null; } if (parentNode == null) { GD.PrintErr("Tried to instantiate child of a null parent"); return null; } var newInstance = prefab.Instantiate(); //node.GetParent().CallDeferred("add_child", newInstance); parentNode.CallDeferred("add_child", newInstance); //newInstance.Transform = node.GlobalTransform; newInstance.Position = parentNode.ToLocal(position); return newInstance; } }