159 lines
3.7 KiB
C#
159 lines
3.7 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Reflection;
|
|
using Godot;
|
|
using Godot.Collections;
|
|
|
|
public partial class Common
|
|
{
|
|
public partial class RayCast
|
|
{
|
|
public static PhysicsDirectSpaceState2D world;
|
|
|
|
public Vector2 pos;
|
|
|
|
public Node obj;
|
|
|
|
public Shape2D shape;
|
|
}
|
|
|
|
public static IEnumerator Move(Node2D obj, Vector2 pos, float frameTime, bool local = false, Vector2? start = null)
|
|
{
|
|
start.GetValueOrDefault();
|
|
if (!start.HasValue)
|
|
{
|
|
Vector2 value = (local ? obj.Position : obj.GlobalPosition);
|
|
start = value;
|
|
}
|
|
for (float a = 0f; a < frameTime; a += Main.deltaTime)
|
|
{
|
|
if (local)
|
|
{
|
|
obj.Position = start.Value.Lerp(pos, a / frameTime);
|
|
}
|
|
else
|
|
{
|
|
obj.GlobalPosition = start.Value.Lerp(pos, a / frameTime);
|
|
}
|
|
yield return null;
|
|
}
|
|
if (local)
|
|
{
|
|
obj.Position = pos;
|
|
}
|
|
else
|
|
{
|
|
obj.GlobalPosition = pos;
|
|
}
|
|
}
|
|
|
|
public static Vector2[] PosFromNode(in IList<Node2D> list)
|
|
{
|
|
Vector2[] array = new Vector2[list.Count];
|
|
for (int i = 0; i < array.Length; i++)
|
|
{
|
|
array[i] = list[i].GlobalPosition;
|
|
}
|
|
return array;
|
|
}
|
|
|
|
public static void FillArray(ref List<int> array, in int max, in int start = 0)
|
|
{
|
|
array.Clear();
|
|
for (int i = start; i < max; i++)
|
|
{
|
|
array.Add(i);
|
|
}
|
|
}
|
|
|
|
public static void SwapType<BaseClass, TargetClass>(ref BaseClass en, in string subFolder = "", bool callReady = false) where BaseClass : Node where TargetClass : BaseClass
|
|
{
|
|
FieldInfo[] fields = typeof(BaseClass).GetFields();
|
|
object[] array = new object[fields.Length];
|
|
for (int i = 0; i < fields.Length; i++)
|
|
{
|
|
array[i] = fields[i].GetValue(en);
|
|
}
|
|
ulong instanceId = en.GetInstanceId();
|
|
en.SetScript(GD.Load<Script>("res://Scripts/" + subFolder + typeof(TargetClass)?.ToString() + ".cs"));
|
|
en = GodotObject.InstanceFromId(instanceId) as BaseClass;
|
|
for (int j = 0; j < fields.Length; j++)
|
|
{
|
|
try
|
|
{
|
|
fields[j].SetValue(en, array[j]);
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
}
|
|
if (callReady)
|
|
{
|
|
en._Ready();
|
|
}
|
|
}
|
|
|
|
public static T[] FindChildOfType<T>(Node parent, bool includeSelf = true) where T : Node
|
|
{
|
|
List<T> list = new List<T>();
|
|
if (includeSelf && parent is T item)
|
|
{
|
|
list.Add(item);
|
|
}
|
|
if (parent.GetChildCount() > 0)
|
|
{
|
|
foreach (Node child in parent.GetChildren())
|
|
{
|
|
list.AddRange(FindChildOfType<T>(child));
|
|
}
|
|
}
|
|
return list.ToArray();
|
|
}
|
|
|
|
public static float Distance(float a, float b)
|
|
{
|
|
return Mathf.Abs(a - b);
|
|
}
|
|
|
|
public static RayCast DoRaycast(Vector2 pos, uint layers = uint.MaxValue)
|
|
{
|
|
return DoRaycast(pos, pos, layers);
|
|
}
|
|
|
|
public static RayCast DoRaycast(Vector2 pos, Vector2 end, uint layers = uint.MaxValue, Array<Rid> ignore = null)
|
|
{
|
|
RayCast.world = Main.instance.GetWorld2D().DirectSpaceState;
|
|
RayCast rayCast = new RayCast();
|
|
PhysicsRayQueryParameters2D parameters = PhysicsRayQueryParameters2D.Create(pos, end, layers, ignore);
|
|
Dictionary dictionary = RayCast.world.IntersectRay(parameters);
|
|
if (dictionary == null || dictionary.Count == 0)
|
|
{
|
|
return null;
|
|
}
|
|
rayCast.obj = (Node)(GodotObject)dictionary["collider"];
|
|
rayCast.pos = (Vector2)dictionary["position"];
|
|
rayCast.shape = (Shape2D)(GodotObject)dictionary["shape"];
|
|
return rayCast;
|
|
}
|
|
|
|
public static Vector2 Beizier(Vector2 a, Vector2 b, Vector2 c, float t)
|
|
{
|
|
return a.Lerp(b, t).Lerp(b.Lerp(c, t), t);
|
|
}
|
|
|
|
public static IEnumerator Arc(Node2D obj, Vector2 start, Vector2 mid, Vector2 end, float frameTime)
|
|
{
|
|
for (float a = 0f; a < frameTime; a += Main.deltaTime)
|
|
{
|
|
obj.GlobalPosition = Beizier(start, mid, end, a / frameTime);
|
|
yield return null;
|
|
}
|
|
obj.GlobalPosition = end;
|
|
}
|
|
|
|
public static float SinOverTime(float frequency = 1f, float variance = 0f)
|
|
{
|
|
return Mathf.Sin(Mathf.DegToRad((float)Time.GetTicksMsec() * frequency + variance));
|
|
}
|
|
}
|