2025-05-03 20:36:17 +08:00

192 lines
5.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using Godot;
using Godot.Bridge;
using Godot.NativeInterop;
[ScriptPath("res://Scripts/Puzzles/CratePush.cs")]
public class CratePush : AnimatableBody2D, Interacteable
{
public new class MethodName : AnimatableBody2D.MethodName
{
public new static readonly StringName _EnterTree = "_EnterTree";
public static readonly StringName Interact = "Interact";
}
public new class PropertyName : AnimatableBody2D.PropertyName
{
public static readonly StringName start = "start";
public static readonly StringName pushSound = "pushSound";
}
public new class SignalName : AnimatableBody2D.SignalName
{
}
public Vector2 start;
private Coroutine pushing;
[Export(PropertyHint.None, "")]
private AudioStream pushSound;
private static readonly List<Vector2> possible = new List<Vector2>
{
Vector2.Up,
Vector2.Left,
Vector2.Down,
Vector2.Right
};
public override void _EnterTree()
{
start = base.GlobalPosition;
}
public void Interact()
{
if (pushing == null || pushing.done)
{
pushing = Coroutine.Start(Push(Player.instance.controlling.GlobalPosition.DirectionTo(base.GlobalPosition).Snapped(Vector2.One)));
}
else
{
Player.instance.canInput = true;
}
}
private IEnumerator Push(Vector2 direction)
{
if (possible.Contains(direction))
{
Vector2 p = base.GlobalPosition;
Vector2 e = base.GlobalPosition + direction * 20f;
if (Common.DoRaycast(p, e) == null)
{
if (pushSound != null)
{
Audio.PlaySound(pushSound);
}
Player.instance.controlling.StopMoving(Entity.Animations.Idle);
Player.instance.controlling.LookAt(base.GlobalPosition);
for (float a = 0f; a < 20f; a += Main.deltaTime)
{
base.GlobalPosition = p.Lerp(e, a / 20f);
yield return null;
}
base.GlobalPosition = e.Snapped(Vector2.One * 10f);
}
}
Player.instance.canInput = true;
}
[EditorBrowsable(EditorBrowsableState.Never)]
internal static List<MethodInfo> GetGodotMethodList()
{
return new List<MethodInfo>(2)
{
new MethodInfo(MethodName._EnterTree, new PropertyInfo(Variant.Type.Nil, "", PropertyHint.None, "", PropertyUsageFlags.Default, exported: false), MethodFlags.Normal, null, null),
new MethodInfo(MethodName.Interact, new PropertyInfo(Variant.Type.Nil, "", PropertyHint.None, "", PropertyUsageFlags.Default, exported: false), MethodFlags.Normal, null, null)
};
}
[EditorBrowsable(EditorBrowsableState.Never)]
protected override bool InvokeGodotClassMethod(in godot_string_name method, NativeVariantPtrArgs args, out godot_variant ret)
{
if (method == MethodName._EnterTree && args.Count == 0)
{
_EnterTree();
ret = default(godot_variant);
return true;
}
if (method == MethodName.Interact && args.Count == 0)
{
Interact();
ret = default(godot_variant);
return true;
}
return base.InvokeGodotClassMethod(in method, args, out ret);
}
[EditorBrowsable(EditorBrowsableState.Never)]
protected override bool HasGodotClassMethod(in godot_string_name method)
{
if (method == MethodName._EnterTree)
{
return true;
}
if (method == MethodName.Interact)
{
return true;
}
return base.HasGodotClassMethod(in method);
}
[EditorBrowsable(EditorBrowsableState.Never)]
protected override bool SetGodotClassPropertyValue(in godot_string_name name, in godot_variant value)
{
if (name == PropertyName.start)
{
start = VariantUtils.ConvertTo<Vector2>(in value);
return true;
}
if (name == PropertyName.pushSound)
{
pushSound = VariantUtils.ConvertTo<AudioStream>(in value);
return true;
}
return base.SetGodotClassPropertyValue(in name, in value);
}
[EditorBrowsable(EditorBrowsableState.Never)]
protected override bool GetGodotClassPropertyValue(in godot_string_name name, out godot_variant value)
{
if (name == PropertyName.start)
{
value = VariantUtils.CreateFrom(in start);
return true;
}
if (name == PropertyName.pushSound)
{
value = VariantUtils.CreateFrom(in pushSound);
return true;
}
return base.GetGodotClassPropertyValue(in name, out value);
}
[EditorBrowsable(EditorBrowsableState.Never)]
internal static List<PropertyInfo> GetGodotPropertyList()
{
return new List<PropertyInfo>
{
new PropertyInfo(Variant.Type.Vector2, PropertyName.start, PropertyHint.None, "", PropertyUsageFlags.ScriptVariable, exported: false),
new PropertyInfo(Variant.Type.Object, PropertyName.pushSound, PropertyHint.ResourceType, "AudioStream", PropertyUsageFlags.Default | PropertyUsageFlags.ScriptVariable, exported: true)
};
}
[EditorBrowsable(EditorBrowsableState.Never)]
protected override void SaveGodotObjectData(GodotSerializationInfo info)
{
base.SaveGodotObjectData(info);
info.AddProperty(PropertyName.start, Variant.From(in start));
info.AddProperty(PropertyName.pushSound, Variant.From(in pushSound));
}
[EditorBrowsable(EditorBrowsableState.Never)]
protected override void RestoreGodotObjectData(GodotSerializationInfo info)
{
base.RestoreGodotObjectData(info);
if (info.TryGetProperty(PropertyName.start, out var value))
{
start = value.As<Vector2>();
}
if (info.TryGetProperty(PropertyName.pushSound, out var value2))
{
pushSound = value2.As<AudioStream>();
}
}
}