2025-05-13 19:22:01 +08:00

132 lines
2.5 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using Godot;
using Godot.Bridge;
using Godot.Collections;
using Godot.NativeInterop;
[ScriptPath("res://Scripts/Puzzles/PuzzleGate.cs")]
public partial class PuzzleGate : StaticBody2D, IPuzzle
{
[Signal]
public delegate void GateOpenEventHandler();
[Export(PropertyHint.None, "")]
private Sprite2D[] sprite;
[Export(PropertyHint.None, "")]
private Texture2D[] tex;
[Export(PropertyHint.None, "")]
private Node[] linked;
[Export(PropertyHint.None, "")]
private bool onlyOnce = true;
[Export(PropertyHint.None, "")]
private bool shakeOnCompletion = true;
[Export(PropertyHint.None, "")]
private Array<SaveFile.Flags> flags;
[Export(PropertyHint.None, "")]
private CollisionShape2D collider;
[Export(PropertyHint.None, "")]
private AudioStream activeSound;
private IPuzzle[] linkedp;
public bool overrideState;
public bool active { get; set; }
public override void _EnterTree()
{
linkedp = new IPuzzle[linked.Length];
for (int i = 0; i < linked.Length; i++)
{
linkedp[i] = (IPuzzle)linked[i];
}
Array<SaveFile.Flags> array = flags;
if (array != null && array.Count > 0 && SaveFile.HasFlags(flags))
{
Active(doEffects: false);
}
base.CollisionLayer = 524289u;
}
public void Active(bool doEffects = true)
{
active = true;
for (int i = 0; i < sprite.Length; i++)
{
Sprite2D obj = sprite[i];
Texture2D[] array = tex;
obj.Texture = ((array != null && array.Length != 0) ? tex[1] : null);
}
collider?.SetDeferred("disabled", true);
if (!doEffects)
{
return;
}
if (shakeOnCompletion)
{
CameraController.Shake(15f, 2f);
}
if (activeSound != null)
{
Audio.PlaySound(activeSound);
}
Array<SaveFile.Flags> array2 = flags;
if (array2 != null && array2.Count > 0)
{
for (int j = 0; j < flags.Count; j++)
{
SaveFile.AddFlag(flags[j]);
}
}
EmitSignal(SignalName.GateOpen);
}
public void Deactivate()
{
active = false;
collider?.SetDeferred("disabled", false);
for (int i = 0; i < sprite.Length; i++)
{
sprite[i].Texture = tex[0];
}
Party.MoveAndSlide(delayed: true);
}
public override void _Process(double delta)
{
if (!active)
{
if (CheckLinked() || overrideState)
{
Active();
}
}
else if (!onlyOnce && !CheckLinked() && !overrideState)
{
Deactivate();
}
}
private bool CheckLinked()
{
for (int i = 0; i < linkedp.Length; i++)
{
if (!linkedp[i].active)
{
return false;
}
}
return true;
}
}