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

85 lines
1.6 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 partial class PuzzleButton : ShapeCast2D, IPuzzle
{
[Signal]
public delegate void CallOnPressEventHandler();
[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;
public bool active { get; set; }
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;
}
}