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

290 lines
7.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using Godot;
using Godot.Bridge;
using Godot.NativeInterop;
[ScriptPath("res://Scripts/Puzzles/MemGameCards.cs")]
public class MemGameCards : Area2D, Interacteable
{
public new class MethodName : Area2D.MethodName
{
public new static readonly StringName _Ready = "_Ready";
public static readonly StringName Reset = "Reset";
public static readonly StringName Interact = "Interact";
}
public new class PropertyName : Area2D.PropertyName
{
public static readonly StringName parent = "parent";
public static readonly StringName sprite = "sprite";
public static readonly StringName flipped = "flipped";
public static readonly StringName id = "id";
}
public new class SignalName : Area2D.SignalName
{
}
private MemoryGame parent;
public Sprite2D sprite;
public bool flipped;
public int id = 1;
public override void _Ready()
{
sprite = GetParent<Sprite2D>();
parent = sprite.GetParent<MemoryGame>();
}
public void Reset()
{
flipped = false;
Coroutine.Start(FlipAnim(0));
}
public void Interact()
{
if (parent.running && !flipped)
{
Main.inEvent = Coroutine.Start(FlipCard());
}
Player.instance.canInput = true;
}
private IEnumerator FlipCard()
{
Party.StopMoving();
Coroutine c = Coroutine.Start(FlipAnim(id));
while (!c.done)
{
yield return null;
}
if (parent.last != null)
{
if (!parent.flipped.Contains(id))
{
Audio.PlaySound("snd_error.wav");
for (float a = 0f; a < 30f; a += Main.deltaTime)
{
yield return null;
}
parent.lives--;
parent.last.Reset();
parent.flipped.RemoveAt(parent.flipped.Count - 1);
for (float a = 0f; a < 30f; a += Main.deltaTime)
{
yield return null;
}
c = Coroutine.Start(FlipAnim(0));
while (!c.done)
{
yield return null;
}
}
else
{
Audio.PlaySound("snd_shineselect.wav");
parent.flipped.Add(id);
flipped = true;
}
parent.last = null;
if (parent.flipped.Count == 18 || parent.lives == 0)
{
for (float a = 0f; a < 30f; a += Main.deltaTime)
{
yield return null;
}
Main.inEvent = Coroutine.Start(parent.EndGame(parent.flipped.Count == 18));
}
}
else
{
parent.flipped.Add(id);
flipped = true;
parent.last = this;
}
}
public IEnumerator FlipAnim(int changeTo, bool noSound = false)
{
if (!noSound)
{
Audio.PlaySound("snd_wing_ch1.wav", 1.1f);
}
float a = 0f;
float b;
for (b = 10f; a < b; a += Main.deltaTime)
{
sprite.Scale = new Vector2(Mathf.Lerp(1f, 0f, a / b), 1f);
yield return null;
}
sprite.Frame = changeTo;
b = 0f;
for (a = 10f; b < a; b += Main.deltaTime)
{
sprite.Scale = new Vector2(Mathf.Lerp(0f, 1f, b / a), 1f);
yield return null;
}
sprite.Scale = Vector2.One;
}
[EditorBrowsable(EditorBrowsableState.Never)]
internal static List<MethodInfo> GetGodotMethodList()
{
return new List<MethodInfo>(3)
{
new MethodInfo(MethodName._Ready, new PropertyInfo(Variant.Type.Nil, "", PropertyHint.None, "", PropertyUsageFlags.Default, exported: false), MethodFlags.Normal, null, null),
new MethodInfo(MethodName.Reset, 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._Ready && args.Count == 0)
{
_Ready();
ret = default(godot_variant);
return true;
}
if (method == MethodName.Reset && args.Count == 0)
{
Reset();
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._Ready)
{
return true;
}
if (method == MethodName.Reset)
{
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.parent)
{
parent = VariantUtils.ConvertTo<MemoryGame>(in value);
return true;
}
if (name == PropertyName.sprite)
{
sprite = VariantUtils.ConvertTo<Sprite2D>(in value);
return true;
}
if (name == PropertyName.flipped)
{
flipped = VariantUtils.ConvertTo<bool>(in value);
return true;
}
if (name == PropertyName.id)
{
id = VariantUtils.ConvertTo<int>(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.parent)
{
value = VariantUtils.CreateFrom(in parent);
return true;
}
if (name == PropertyName.sprite)
{
value = VariantUtils.CreateFrom(in sprite);
return true;
}
if (name == PropertyName.flipped)
{
value = VariantUtils.CreateFrom(in flipped);
return true;
}
if (name == PropertyName.id)
{
value = VariantUtils.CreateFrom(in id);
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.Object, PropertyName.parent, PropertyHint.None, "", PropertyUsageFlags.ScriptVariable, exported: false),
new PropertyInfo(Variant.Type.Object, PropertyName.sprite, PropertyHint.None, "", PropertyUsageFlags.ScriptVariable, exported: false),
new PropertyInfo(Variant.Type.Bool, PropertyName.flipped, PropertyHint.None, "", PropertyUsageFlags.ScriptVariable, exported: false),
new PropertyInfo(Variant.Type.Int, PropertyName.id, PropertyHint.None, "", PropertyUsageFlags.ScriptVariable, exported: false)
};
}
[EditorBrowsable(EditorBrowsableState.Never)]
protected override void SaveGodotObjectData(GodotSerializationInfo info)
{
base.SaveGodotObjectData(info);
info.AddProperty(PropertyName.parent, Variant.From(in parent));
info.AddProperty(PropertyName.sprite, Variant.From(in sprite));
info.AddProperty(PropertyName.flipped, Variant.From(in flipped));
info.AddProperty(PropertyName.id, Variant.From(in id));
}
[EditorBrowsable(EditorBrowsableState.Never)]
protected override void RestoreGodotObjectData(GodotSerializationInfo info)
{
base.RestoreGodotObjectData(info);
if (info.TryGetProperty(PropertyName.parent, out var value))
{
parent = value.As<MemoryGame>();
}
if (info.TryGetProperty(PropertyName.sprite, out var value2))
{
sprite = value2.As<Sprite2D>();
}
if (info.TryGetProperty(PropertyName.flipped, out var value3))
{
flipped = value3.As<bool>();
}
if (info.TryGetProperty(PropertyName.id, out var value4))
{
id = value4.As<int>();
}
}
}