mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-01 09:55:35 +00:00
100 lines
No EOL
2.1 KiB
C#
100 lines
No EOL
2.1 KiB
C#
using Godot;
|
|
|
|
namespace Cirno.Scripts.Interactables._3D;
|
|
|
|
[Tool]
|
|
public partial class AnimatedSwitch3D : Switch3D
|
|
{
|
|
[Export] public DoorState State { get; set; } = DoorState.Closed;
|
|
|
|
[Signal]
|
|
public delegate void OpeningEventHandler();
|
|
|
|
[Signal]
|
|
public delegate void ClosingEventHandler();
|
|
|
|
[Signal]
|
|
public delegate void SetClosedEventHandler();
|
|
|
|
[Signal]
|
|
public delegate void SetOpenEventHandler();
|
|
|
|
private bool _isAnimating = false;
|
|
|
|
public override void _Ready()
|
|
{
|
|
if (Engine.IsEditorHint()) return;
|
|
base._Ready();
|
|
|
|
SyncAnimation();
|
|
}
|
|
|
|
private void SyncAnimation()
|
|
{
|
|
if (State is DoorState.Closed)
|
|
{
|
|
EmitSignalSetClosed();
|
|
}
|
|
else
|
|
{
|
|
EmitSignalSetOpen();
|
|
}
|
|
}
|
|
|
|
public void ClosedAnimationFinished()
|
|
{
|
|
State = DoorState.Closed;
|
|
_isAnimating = false;
|
|
}
|
|
|
|
public void OpenAnimationFinished()
|
|
{
|
|
State = DoorState.Open;
|
|
_isAnimating = false;
|
|
}
|
|
|
|
private void ChangeState(DoorState newState)
|
|
{
|
|
if (_isAnimating) return;
|
|
if (State == newState) return;
|
|
|
|
switch (newState)
|
|
{
|
|
case DoorState.Open:
|
|
EmitSignalOpening();
|
|
break;
|
|
case DoorState.Closed:
|
|
EmitSignalClosing();
|
|
break;
|
|
}
|
|
|
|
_isAnimating = true;
|
|
}
|
|
|
|
public override bool Activate(ActivationType activationType = ActivationType.Toggle)
|
|
{
|
|
if (_isAnimating) return false;
|
|
|
|
switch (State)
|
|
{
|
|
case DoorState.Closed:
|
|
if (base.Activate(ActivationType.Open))
|
|
{
|
|
ChangeState(DoorState.Open);
|
|
return true;
|
|
}
|
|
|
|
break;
|
|
case DoorState.Open:
|
|
if (base.Activate(ActivationType.Close))
|
|
{
|
|
ChangeState(DoorState.Closed);
|
|
return true;
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
} |