using System; using System.Collections.Generic; using System.Linq; 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 CreateChildOf(node, node.GetParent(), 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; } public static GameManager GetGameManager(this Node2D node) { return node.GetTree().Root.GetNode("GameScene"); // node.GetNode("/root/GameScene"); } public static AlarmManager GetAlarmManager(this Node2D node) { return node.GetNodeOrNull("/root/GameScene/AlarmManager"); } public static InventoryManager GetInventoryManager(this Node2D node) { return node.GetNodeOrNull("/root/GameScene/InventoryManager"); } public static Vector2 GetSnappedDirection(Vector2 direction) { // Snap to one of 8 directions var angles = new[] { Vector2.Up, Vector2.Up + Vector2.Right, Vector2.Right, Vector2.Right + Vector2.Down, Vector2.Down, Vector2.Down + Vector2.Left, Vector2.Left, Vector2.Left + Vector2.Up }; return angles[Mathf.PosMod((int)(direction.X + 4), 8)].Normalized(); } public static Vector2 SnapToCardinal(this Vector2 input) { if (Mathf.Abs(input.X) > Mathf.Abs(input.Y)) { return new Vector2(Mathf.Sign(input.X), 0); // Left or Right } else { return new Vector2(0, Mathf.Sign(input.Y)); // Up or Down } } public static T PickRandom(this IEnumerable list) { var arr = list.ToArray(); return arr[GD.RandRange(0, arr.Length -1)]; } }