cirnogodot/Scripts/Door.cs

169 lines
3.4 KiB
C#
Raw Normal View History

2025-01-20 12:17:27 +01:00
using Godot;
using System;
using System.Diagnostics;
using Cirno.Scripts;
public partial class Door : Activable
{
2025-03-03 17:55:53 +01:00
// protected AnimatedSprite2D _animatedSprite;
//protected CollisionShape2D _collisionShape;
//protected CollisionShape2D _solidShape;
2025-03-01 23:46:25 +01:00
protected AudioStreamPlayer2D _activationSound;
protected AudioStreamPlayer2D _deactivationSound;
2025-01-20 12:17:27 +01:00
// Called when the node enters the scene tree for the first time.
[Export]
public DoorState State { get; set; }
2025-02-23 01:27:14 +01:00
[Signal]
public delegate void DoorOpenedEventHandler();
[Signal]
public delegate void DoorClosedEventHandler();
2025-03-03 17:55:53 +01:00
[Signal]
public delegate void DestroyedEventHandler();
2025-02-23 01:27:14 +01:00
2025-01-20 12:17:27 +01:00
public override void _Ready()
{
2025-03-03 17:55:53 +01:00
// _animatedSprite = GetNode<AnimatedSprite2D>("AnimatedSprite2D");
//_collisionShape = GetNode<CollisionShape2D>("CollisionShape2D");
//_solidShape = GetNode<CollisionShape2D>("RigidBody2D/CollisionShape2D");
2025-01-20 12:17:27 +01:00
2025-03-01 23:46:25 +01:00
_activationSound = GetNodeOrNull<AudioStreamPlayer2D>("ActivationSound");
_deactivationSound = GetNodeOrNull<AudioStreamPlayer2D>("DeactivationSound");
2025-01-20 12:17:27 +01:00
SetState(State);
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
}
2025-02-23 01:24:29 +01:00
public virtual void Open()
2025-01-20 12:17:27 +01:00
{
2025-03-03 17:55:53 +01:00
// _animatedSprite.Play("Opening");
2025-01-20 12:17:27 +01:00
State = DoorState.Open;
2025-02-21 16:27:57 +01:00
CallDeferred(MethodName.DeferredDisableCollision, true);
2025-03-01 23:46:25 +01:00
_deactivationSound?.Play();
2025-03-03 17:55:53 +01:00
EmitSignal(SignalName.DoorOpened);
2025-02-21 16:27:57 +01:00
//_collisionShape.Disabled = true;
//_solidShape.Disabled = true;
2025-01-20 12:17:27 +01:00
}
2025-02-23 01:24:29 +01:00
public virtual void Close()
2025-01-20 12:17:27 +01:00
{
2025-03-03 17:55:53 +01:00
// _animatedSprite.Play("Closing");
2025-01-20 12:17:27 +01:00
State = DoorState.Closed;
2025-02-21 16:27:57 +01:00
CallDeferred(MethodName.DeferredDisableCollision, false);
2025-03-01 23:46:25 +01:00
_activationSound?.Play();
2025-03-03 17:55:53 +01:00
EmitSignal(SignalName.DoorClosed);
2025-02-21 16:27:57 +01:00
//_collisionShape.Disabled = false;
//_solidShape.Disabled = false;
2025-01-20 12:17:27 +01:00
}
2025-02-21 18:57:00 +01:00
public void Destroy()
{
2025-03-03 17:55:53 +01:00
// _animatedSprite.Play("Destroyed");
2025-02-21 18:57:00 +01:00
State = DoorState.Destroyed;
CallDeferred(MethodName.DeferredDisableCollision, true);
2025-03-03 17:55:53 +01:00
EmitSignal(SignalName.Destroyed);
2025-02-21 18:57:00 +01:00
}
2025-02-21 16:27:57 +01:00
private void DeferredDisableCollision(bool state)
{
2025-03-03 17:55:53 +01:00
//_collisionShape.Disabled = state;
//_solidShape.Disabled = state;
2025-02-21 16:27:57 +01:00
}
2025-03-09 21:58:25 +01:00
public override bool Activate(ActivationType activationType = ActivationType.Toggle)
2025-01-20 12:17:27 +01:00
{
2025-02-13 16:10:22 +01:00
switch (activationType)
2025-01-20 12:17:27 +01:00
{
2025-02-13 16:10:22 +01:00
case ActivationType.Toggle:
2025-02-21 18:57:00 +01:00
ToggleDoor();
2025-02-13 16:10:22 +01:00
break;
case ActivationType.Enable:
2025-02-21 18:57:00 +01:00
Close();
2025-01-20 12:17:27 +01:00
break;
2025-02-13 16:10:22 +01:00
case ActivationType.Disable:
2025-02-21 18:57:00 +01:00
Open();
2025-01-20 12:17:27 +01:00
break;
2025-02-13 16:10:22 +01:00
case ActivationType.Use:
2025-02-21 18:57:00 +01:00
ToggleDoor();
2025-02-13 16:10:22 +01:00
break;
case ActivationType.Destroy:
2025-02-21 18:57:00 +01:00
Destroy();
break;
case ActivationType.Open:
Open();
break;
case ActivationType.Close:
Close();
2025-02-13 16:10:22 +01:00
break;
2025-01-20 12:17:27 +01:00
default:
2025-02-21 18:57:00 +01:00
ToggleDoor();
break;
}
2025-03-09 21:58:25 +01:00
return true;
2025-02-21 18:57:00 +01:00
}
private void ToggleDoor()
{
switch (State)
{
case DoorState.Closed:
Open();
break;
case DoorState.Open:
Close();
break;
default:
throw new ArgumentOutOfRangeException();
2025-01-20 12:17:27 +01:00
}
}
private void SetState(DoorState state)
{
switch (state)
{
case DoorState.Closed:
Close();
break;
case DoorState.Open:
Open();
break;
2025-02-21 18:57:00 +01:00
case DoorState.Destroyed:
Destroy();
break;
2025-01-20 12:17:27 +01:00
default:
throw new ArgumentOutOfRangeException();
}
}
2025-03-03 17:55:53 +01:00
// public void _on_animated_sprite_2d_animation_changed()
// {
// switch (_animatedSprite.Animation)
// {
// case "Opening":
//
// break;
// case "Closing":
//
// break;
// default:
// break;
// }
// }
2025-01-20 12:17:27 +01:00
}
public enum DoorState
{
Closed,
2025-02-21 18:57:00 +01:00
Open,
Destroyed
2025-01-20 12:17:27 +01:00
}