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

415 lines
12 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using Godot;
using Godot.Bridge;
using Godot.NativeInterop;
[ScriptPath("res://Scripts/Puzzles/PuzzleButton.cs")]
public class PuzzleButton : ShapeCast2D, IPuzzle
{
[Signal]
public delegate void CallOnPressEventHandler();
public new class MethodName : ShapeCast2D.MethodName
{
public new static readonly StringName _EnterTree = "_EnterTree";
public new static readonly StringName _Process = "_Process";
}
public new class PropertyName : ShapeCast2D.PropertyName
{
public static readonly StringName active = "active";
public static readonly StringName onlyCrates = "onlyCrates";
public static readonly StringName repeatableCall = "repeatableCall";
public static readonly StringName texs = "texs";
public static readonly StringName sprite = "sprite";
public static readonly StringName pressSound = "pressSound";
public static readonly StringName lastState = "lastState";
public static readonly StringName firstStep = "firstStep";
public static readonly StringName lastPress = "lastPress";
public static readonly StringName pressed = "pressed";
}
public new class SignalName : ShapeCast2D.SignalName
{
public static readonly StringName CallOnPress = "CallOnPress";
}
[Export(PropertyHint.None, "")]
private bool onlyCrates;
[Export(PropertyHint.None, "")]
private bool repeatableCall;
[Export(PropertyHint.None, "")]
private Texture2D[] texs;
[Export(PropertyHint.None, "")]
private Sprite2D sprite;
[Export(PropertyHint.None, "")]
private AudioStream pressSound;
private bool lastState;
private bool firstStep;
private bool lastPress;
private bool pressed;
private CallOnPressEventHandler backing_CallOnPress;
public bool active { get; set; }
public event CallOnPressEventHandler CallOnPress
{
add
{
backing_CallOnPress = (CallOnPressEventHandler)Delegate.Combine(backing_CallOnPress, value);
}
remove
{
backing_CallOnPress = (CallOnPressEventHandler)Delegate.Remove(backing_CallOnPress, value);
}
}
public override void _EnterTree()
{
}
public override void _Process(double delta)
{
int collisionCount = GetCollisionCount();
active = false;
pressed = false;
if (collisionCount > 0)
{
for (int i = 0; i < collisionCount; i++)
{
if (GetCollider(i) is CratePush)
{
active = true;
pressed = true;
}
else if (GetCollider(i) is Player || GetCollider(i) is Follower)
{
active = !onlyCrates;
pressed = true;
}
}
}
if (lastPress != pressed && pressSound != null)
{
Audio.PlaySound(pressSound);
}
sprite.Texture = texs[pressed ? 1u : 0u];
if (active != lastState)
{
if (active)
{
if (repeatableCall || !firstStep)
{
EmitSignal(SignalName.CallOnPress);
}
firstStep = true;
}
lastState = active;
}
lastPress = pressed;
}
[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._Process, new PropertyInfo(Variant.Type.Nil, "", PropertyHint.None, "", PropertyUsageFlags.Default, exported: false), MethodFlags.Normal, new List<PropertyInfo>
{
new PropertyInfo(Variant.Type.Float, "delta", PropertyHint.None, "", PropertyUsageFlags.Default, exported: false)
}, 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._Process && args.Count == 1)
{
_Process(VariantUtils.ConvertTo<double>(in args[0]));
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._Process)
{
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.active)
{
active = VariantUtils.ConvertTo<bool>(in value);
return true;
}
if (name == PropertyName.onlyCrates)
{
onlyCrates = VariantUtils.ConvertTo<bool>(in value);
return true;
}
if (name == PropertyName.repeatableCall)
{
repeatableCall = VariantUtils.ConvertTo<bool>(in value);
return true;
}
if (name == PropertyName.texs)
{
texs = VariantUtils.ConvertToSystemArrayOfGodotObject<Texture2D>(in value);
return true;
}
if (name == PropertyName.sprite)
{
sprite = VariantUtils.ConvertTo<Sprite2D>(in value);
return true;
}
if (name == PropertyName.pressSound)
{
pressSound = VariantUtils.ConvertTo<AudioStream>(in value);
return true;
}
if (name == PropertyName.lastState)
{
lastState = VariantUtils.ConvertTo<bool>(in value);
return true;
}
if (name == PropertyName.firstStep)
{
firstStep = VariantUtils.ConvertTo<bool>(in value);
return true;
}
if (name == PropertyName.lastPress)
{
lastPress = VariantUtils.ConvertTo<bool>(in value);
return true;
}
if (name == PropertyName.pressed)
{
pressed = VariantUtils.ConvertTo<bool>(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.active)
{
value = VariantUtils.CreateFrom<bool>(active);
return true;
}
if (name == PropertyName.onlyCrates)
{
value = VariantUtils.CreateFrom(in onlyCrates);
return true;
}
if (name == PropertyName.repeatableCall)
{
value = VariantUtils.CreateFrom(in repeatableCall);
return true;
}
if (name == PropertyName.texs)
{
GodotObject[] array = texs;
value = VariantUtils.CreateFromSystemArrayOfGodotObject(array);
return true;
}
if (name == PropertyName.sprite)
{
value = VariantUtils.CreateFrom(in sprite);
return true;
}
if (name == PropertyName.pressSound)
{
value = VariantUtils.CreateFrom(in pressSound);
return true;
}
if (name == PropertyName.lastState)
{
value = VariantUtils.CreateFrom(in lastState);
return true;
}
if (name == PropertyName.firstStep)
{
value = VariantUtils.CreateFrom(in firstStep);
return true;
}
if (name == PropertyName.lastPress)
{
value = VariantUtils.CreateFrom(in lastPress);
return true;
}
if (name == PropertyName.pressed)
{
value = VariantUtils.CreateFrom(in pressed);
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.Bool, PropertyName.onlyCrates, PropertyHint.None, "", PropertyUsageFlags.Default | PropertyUsageFlags.ScriptVariable, exported: true),
new PropertyInfo(Variant.Type.Bool, PropertyName.repeatableCall, PropertyHint.None, "", PropertyUsageFlags.Default | PropertyUsageFlags.ScriptVariable, exported: true),
new PropertyInfo(Variant.Type.Array, PropertyName.texs, PropertyHint.TypeString, "24/17:Texture2D", PropertyUsageFlags.Default | PropertyUsageFlags.ScriptVariable, exported: true),
new PropertyInfo(Variant.Type.Object, PropertyName.sprite, PropertyHint.NodeType, "Sprite2D", PropertyUsageFlags.Default | PropertyUsageFlags.ScriptVariable, exported: true),
new PropertyInfo(Variant.Type.Object, PropertyName.pressSound, PropertyHint.ResourceType, "AudioStream", PropertyUsageFlags.Default | PropertyUsageFlags.ScriptVariable, exported: true),
new PropertyInfo(Variant.Type.Bool, PropertyName.active, PropertyHint.None, "", PropertyUsageFlags.ScriptVariable, exported: false),
new PropertyInfo(Variant.Type.Bool, PropertyName.lastState, PropertyHint.None, "", PropertyUsageFlags.ScriptVariable, exported: false),
new PropertyInfo(Variant.Type.Bool, PropertyName.firstStep, PropertyHint.None, "", PropertyUsageFlags.ScriptVariable, exported: false),
new PropertyInfo(Variant.Type.Bool, PropertyName.lastPress, PropertyHint.None, "", PropertyUsageFlags.ScriptVariable, exported: false),
new PropertyInfo(Variant.Type.Bool, PropertyName.pressed, PropertyHint.None, "", PropertyUsageFlags.ScriptVariable, exported: false)
};
}
[EditorBrowsable(EditorBrowsableState.Never)]
protected override void SaveGodotObjectData(GodotSerializationInfo info)
{
base.SaveGodotObjectData(info);
info.AddProperty(PropertyName.active, Variant.From<bool>(active));
info.AddProperty(PropertyName.onlyCrates, Variant.From(in onlyCrates));
info.AddProperty(PropertyName.repeatableCall, Variant.From(in repeatableCall));
StringName name = PropertyName.texs;
GodotObject[] array = texs;
info.AddProperty(name, Variant.CreateFrom(array));
info.AddProperty(PropertyName.sprite, Variant.From(in sprite));
info.AddProperty(PropertyName.pressSound, Variant.From(in pressSound));
info.AddProperty(PropertyName.lastState, Variant.From(in lastState));
info.AddProperty(PropertyName.firstStep, Variant.From(in firstStep));
info.AddProperty(PropertyName.lastPress, Variant.From(in lastPress));
info.AddProperty(PropertyName.pressed, Variant.From(in pressed));
info.AddSignalEventDelegate(SignalName.CallOnPress, backing_CallOnPress);
}
[EditorBrowsable(EditorBrowsableState.Never)]
protected override void RestoreGodotObjectData(GodotSerializationInfo info)
{
base.RestoreGodotObjectData(info);
if (info.TryGetProperty(PropertyName.active, out var value))
{
active = value.As<bool>();
}
if (info.TryGetProperty(PropertyName.onlyCrates, out var value2))
{
onlyCrates = value2.As<bool>();
}
if (info.TryGetProperty(PropertyName.repeatableCall, out var value3))
{
repeatableCall = value3.As<bool>();
}
if (info.TryGetProperty(PropertyName.texs, out var value4))
{
texs = value4.AsGodotObjectArray<Texture2D>();
}
if (info.TryGetProperty(PropertyName.sprite, out var value5))
{
sprite = value5.As<Sprite2D>();
}
if (info.TryGetProperty(PropertyName.pressSound, out var value6))
{
pressSound = value6.As<AudioStream>();
}
if (info.TryGetProperty(PropertyName.lastState, out var value7))
{
lastState = value7.As<bool>();
}
if (info.TryGetProperty(PropertyName.firstStep, out var value8))
{
firstStep = value8.As<bool>();
}
if (info.TryGetProperty(PropertyName.lastPress, out var value9))
{
lastPress = value9.As<bool>();
}
if (info.TryGetProperty(PropertyName.pressed, out var value10))
{
pressed = value10.As<bool>();
}
if (info.TryGetSignalEventDelegate<CallOnPressEventHandler>(SignalName.CallOnPress, out var value11))
{
backing_CallOnPress = value11;
}
}
[EditorBrowsable(EditorBrowsableState.Never)]
internal static List<MethodInfo> GetGodotSignalList()
{
return new List<MethodInfo>(1)
{
new MethodInfo(SignalName.CallOnPress, new PropertyInfo(Variant.Type.Nil, "", PropertyHint.None, "", PropertyUsageFlags.Default, exported: false), MethodFlags.Normal, null, null)
};
}
protected void EmitSignalCallOnPress()
{
EmitSignal(SignalName.CallOnPress);
}
[EditorBrowsable(EditorBrowsableState.Never)]
protected override void RaiseGodotClassSignalCallbacks(in godot_string_name signal, NativeVariantPtrArgs args)
{
if (signal == SignalName.CallOnPress && args.Count == 0)
{
backing_CallOnPress?.Invoke();
}
else
{
base.RaiseGodotClassSignalCallbacks(in signal, args);
}
}
[EditorBrowsable(EditorBrowsableState.Never)]
protected override bool HasGodotClassSignal(in godot_string_name signal)
{
if (signal == SignalName.CallOnPress)
{
return true;
}
return base.HasGodotClassSignal(in signal);
}
}