cirnogodot/Scripts/Tools.cs

113 lines
3.3 KiB
C#
Raw Permalink Normal View History

2025-04-14 16:50:58 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
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;
}
2025-02-06 15:57:03 +01:00
public static GameManager GetGameManager(this Node2D node)
{
2025-02-13 21:55:14 +01:00
return node.GetTree().Root.GetNode<GameManager>("GameScene");
// node.GetNode<GameManager>("/root/GameScene");
2025-02-06 15:57:03 +01:00
}
2025-02-07 14:58:59 +01:00
public static AlarmManager GetAlarmManager(this Node2D node)
{
return node.GetNodeOrNull<AlarmManager>("/root/GameScene/AlarmManager");
}
public static InventoryManager GetInventoryManager(this Node2D node)
{
return node.GetNodeOrNull<InventoryManager>("/root/GameScene/InventoryManager");
}
2025-02-18 17:40:33 +01:00
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();
}
2025-03-21 15:16:50 +01:00
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
}
}
2025-04-14 16:50:58 +02:00
public static T PickRandom<T>(this IEnumerable<T> list)
{
var arr = list.ToArray();
return arr[GD.RandRange(0, arr.Length -1)];
}
2024-08-17 17:00:50 +02:00
}