2024-08-17 17:00:50 +02:00
|
|
|
using Godot;
|
2024-06-09 18:19:57 +02:00
|
|
|
|
|
|
|
|
namespace Cirno.Scripts;
|
|
|
|
|
|
|
|
|
|
public static class Tools
|
|
|
|
|
{
|
2024-08-17 17:00:50 +02:00
|
|
|
public static T CreateChild<T>(this Node2D node, PackedScene prefab) where T : Node2D
|
2025-01-22 11:35:37 +01:00
|
|
|
{
|
2025-01-27 17:13:26 +01:00
|
|
|
return CreateChild<T>(node, prefab, node.GlobalPosition);
|
2025-01-22 11:35:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T CreateChild<T>(this Node2D node, PackedScene prefab, Vector2 position) where T : Node2D
|
2024-08-17 17:00:50 +02:00
|
|
|
{
|
2025-01-27 17:13:26 +01:00
|
|
|
return CreateChildOf<T>(node, node, prefab, position);
|
|
|
|
|
|
|
|
|
|
// if (prefab == null) return null;
|
|
|
|
|
// var newInstance = prefab.Instantiate<T>();
|
|
|
|
|
// 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<T>(this Node2D node, PackedScene prefab) where T : Node2D
|
|
|
|
|
{
|
2025-01-29 14:54:01 +01:00
|
|
|
return CreateChildOf<T>(node, node.GetParent<Node2D>(), prefab, node.GlobalPosition);
|
2025-01-27 17:13:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T CreateSibling<T>(this Node2D node, PackedScene prefab, Vector2 position) where T : Node2D
|
|
|
|
|
{
|
|
|
|
|
return CreateChildOf<T>(node, node.GetParent<Node2D>(), prefab, position);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T CreateChildOf<T>(this Node2D node, Node2D parentNode, PackedScene prefab) where T : Node2D
|
|
|
|
|
{
|
|
|
|
|
return CreateChildOf<T>(node, parentNode, prefab, node.GlobalPosition);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T CreateChildOf<T>(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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-17 17:00:50 +02:00
|
|
|
var newInstance = prefab.Instantiate<T>();
|
2025-01-27 17:13:26 +01:00
|
|
|
//node.GetParent().CallDeferred("add_child", newInstance);
|
|
|
|
|
parentNode.CallDeferred("add_child", newInstance);
|
|
|
|
|
//newInstance.Transform = node.GlobalTransform;
|
|
|
|
|
newInstance.Position = parentNode.ToLocal(position);
|
2024-06-09 18:19:57 +02:00
|
|
|
|
2024-08-17 17:00:50 +02:00
|
|
|
return newInstance;
|
|
|
|
|
}
|
|
|
|
|
}
|