cirnogodot/Scripts/Door.cs
2025-02-06 17:57:06 +01:00

97 lines
1.8 KiB
C#

using Godot;
using System;
using System.Diagnostics;
using Cirno.Scripts;
public partial class Door : Activable
{
private AnimatedSprite2D _animatedSprite;
private CollisionShape2D _collisionShape;
private CollisionShape2D _solidShape;
// Called when the node enters the scene tree for the first time.
[Export]
public DoorState State { get; set; }
public override void _Ready()
{
_animatedSprite = GetNode<AnimatedSprite2D>("AnimatedSprite2D");
_collisionShape = GetNode<CollisionShape2D>("CollisionShape2D");
_solidShape = GetNode<CollisionShape2D>("RigidBody2D/CollisionShape2D");
SetState(State);
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
}
public void Open()
{
_animatedSprite.Play("Opening");
State = DoorState.Open;
_collisionShape.Disabled = true;
_solidShape.Disabled = true;
}
public void Close()
{
_animatedSprite.Play("Closing");
State = DoorState.Closed;
_collisionShape.Disabled = false;
_solidShape.Disabled = false;
}
public override void Activate(ActivationType activationType = ActivationType.Toggle)
{
switch (State)
{
case DoorState.Closed:
Open();
break;
case DoorState.Open:
Close();
break;
default:
throw new ArgumentOutOfRangeException();
}
}
private void SetState(DoorState state)
{
switch (state)
{
case DoorState.Closed:
Close();
break;
case DoorState.Open:
Open();
break;
default:
throw new ArgumentOutOfRangeException();
}
}
public void _on_animated_sprite_2d_animation_changed()
{
switch (_animatedSprite.Animation)
{
case "Opening":
break;
case "Closing":
break;
default:
break;
}
}
}
public enum DoorState
{
Closed,
Open
}