Remade spawn coordinates system

This commit is contained in:
Marco 2025-01-27 17:13:26 +01:00
commit d633618633
5 changed files with 85 additions and 12 deletions

View file

@ -6,18 +6,58 @@ public static class Tools
{
public static T CreateChild<T>(this Node2D node, PackedScene prefab) where T : Node2D
{
return CreateChild<T>(node, prefab, node.Position);
return CreateChild<T>(node, prefab, node.GlobalPosition);
}
public static T CreateChild<T>(this Node2D node, PackedScene prefab, Vector2 position) where T : Node2D
{
if (prefab == null) return null;
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
{
return CreateSibling<T>(node, prefab, node.GlobalPosition);
}
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;
}
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;
//node.GetParent().CallDeferred("add_child", newInstance);
parentNode.CallDeferred("add_child", newInstance);
//newInstance.Transform = node.GlobalTransform;
newInstance.Position = parentNode.ToLocal(position);
return newInstance;
}