cirnogodot/Scripts/Door.cs

114 lines
2.2 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
{
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;
}
2025-02-06 17:57:06 +01:00
public override void 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:
switch (State)
{
case DoorState.Closed:
Open();
break;
case DoorState.Open:
Close();
break;
default:
throw new ArgumentOutOfRangeException();
}
break;
case ActivationType.Enable:
2025-01-20 12:17:27 +01:00
Open();
break;
2025-02-13 16:10:22 +01:00
case ActivationType.Disable:
2025-01-20 12:17:27 +01:00
Close();
break;
2025-02-13 16:10:22 +01:00
case ActivationType.Use:
break;
case ActivationType.Destroy:
break;
2025-01-20 12:17:27 +01:00
default:
2025-02-13 16:10:22 +01:00
throw new ArgumentOutOfRangeException(nameof(activationType), activationType, null);
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;
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
}