using Godot; using System; using System.Diagnostics; using Cirno.Scripts; public partial class Door : Activable { protected AnimatedSprite2D _animatedSprite; protected CollisionShape2D _collisionShape; protected CollisionShape2D _solidShape; // Called when the node enters the scene tree for the first time. [Export] public DoorState State { get; set; } [Signal] public delegate void DoorOpenedEventHandler(); [Signal] public delegate void DoorClosedEventHandler(); public override void _Ready() { _animatedSprite = GetNode("AnimatedSprite2D"); _collisionShape = GetNode("CollisionShape2D"); _solidShape = GetNode("RigidBody2D/CollisionShape2D"); SetState(State); } // Called every frame. 'delta' is the elapsed time since the previous frame. public override void _Process(double delta) { } public virtual void Open() { _animatedSprite.Play("Opening"); State = DoorState.Open; CallDeferred(MethodName.DeferredDisableCollision, true); //_collisionShape.Disabled = true; //_solidShape.Disabled = true; } public virtual void Close() { _animatedSprite.Play("Closing"); State = DoorState.Closed; CallDeferred(MethodName.DeferredDisableCollision, false); //_collisionShape.Disabled = false; //_solidShape.Disabled = false; } public void Destroy() { _animatedSprite.Play("Destroyed"); State = DoorState.Destroyed; CallDeferred(MethodName.DeferredDisableCollision, true); } private void DeferredDisableCollision(bool state) { _collisionShape.Disabled = state; _solidShape.Disabled = state; } public override void Activate(ActivationType activationType = ActivationType.Toggle) { switch (activationType) { case ActivationType.Toggle: ToggleDoor(); break; case ActivationType.Enable: Close(); break; case ActivationType.Disable: Open(); break; case ActivationType.Use: ToggleDoor(); break; case ActivationType.Destroy: Destroy(); break; case ActivationType.Open: Open(); break; case ActivationType.Close: Close(); break; default: ToggleDoor(); break; } } private void ToggleDoor() { 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; case DoorState.Destroyed: Destroy(); 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, Destroyed }