diff options
Diffstat (limited to 'DungeonSystem/scripts/Door.cs')
| -rw-r--r-- | DungeonSystem/scripts/Door.cs | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/DungeonSystem/scripts/Door.cs b/DungeonSystem/scripts/Door.cs new file mode 100644 index 0000000..0a970b3 --- /dev/null +++ b/DungeonSystem/scripts/Door.cs @@ -0,0 +1,55 @@ +using Godot; + +namespace DungeonSystem; + +[GlobalClass] +public partial class Door : Node2D +{ + [Signal] + public delegate void PlayerEnteredDoorwayEventHandler(Door door); + + public enum DoorState { Open, Locked } + public DoorState CurrentState { get; private set; } = DoorState.Open; + + private CollisionShape2D _barrierCollision = null!; + private Area2D _triggerArea = null!; + private AnimationPlayer _animPlayer = null!; + + public override void _Ready() + { + _barrierCollision = GetNode<CollisionShape2D>("Barrier/CollisionShape2D"); + _triggerArea = GetNode<Area2D>("TriggerArea"); + _animPlayer = GetNode<AnimationPlayer>("AnimationPlayer"); + + _triggerArea.BodyEntered += OnBodyEntered; + UnlockDoor(); + } + + public void LockDoor() + { + if (CurrentState == DoorState.Locked) return; + + CurrentState = DoorState.Locked; + _barrierCollision.SetDeferred(CollisionShape2D.PropertyName.Disabled, false); + + if (_animPlayer.HasAnimation("lock")) + _animPlayer.Play("lock"); + } + + public void UnlockDoor() + { + CurrentState = DoorState.Open; + _barrierCollision.SetDeferred(CollisionShape2D.PropertyName.Disabled, true); + + if (_animPlayer.HasAnimation("unlock")) + _animPlayer.Play("unlock"); + } + + private void OnBodyEntered(Node2D body) + { + if (CurrentState == DoorState.Open && body.IsInGroup("player")) + { + EmitSignal(SignalName.PlayerEnteredDoorway, this); + } + } +}
\ No newline at end of file |
