diff options
Diffstat (limited to 'DungeonSystem/scripts/Door.cs')
| -rw-r--r-- | DungeonSystem/scripts/Door.cs | 34 |
1 files changed, 22 insertions, 12 deletions
diff --git a/DungeonSystem/scripts/Door.cs b/DungeonSystem/scripts/Door.cs index 0a970b3..c30c462 100644 --- a/DungeonSystem/scripts/Door.cs +++ b/DungeonSystem/scripts/Door.cs @@ -11,17 +11,27 @@ public partial class Door : Node2D 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!; + private CollisionShape2D? _barrierCollision; + private Area2D? _triggerArea; + private AnimationPlayer? _animPlayer; public override void _Ready() { - _barrierCollision = GetNode<CollisionShape2D>("Barrier/CollisionShape2D"); - _triggerArea = GetNode<Area2D>("TriggerArea"); - _animPlayer = GetNode<AnimationPlayer>("AnimationPlayer"); + _barrierCollision = GetNodeOrNull<CollisionShape2D>("Barrier/CollisionShape2D") + ?? GetNodeOrNull<CollisionShape2D>("StaticBody2D/CollisionShape2D"); + _triggerArea = GetNodeOrNull<Area2D>("TriggerArea") + ?? GetNodeOrNull<Area2D>("Area2D"); + _animPlayer = GetNodeOrNull<AnimationPlayer>("AnimationPlayer"); + + if (_triggerArea != null) + { + _triggerArea.CollisionMask |= 1u << 1; + _triggerArea.Monitoring = true; + } + + if (_triggerArea != null) + _triggerArea.BodyEntered += OnBodyEntered; - _triggerArea.BodyEntered += OnBodyEntered; UnlockDoor(); } @@ -30,18 +40,18 @@ public partial class Door : Node2D if (CurrentState == DoorState.Locked) return; CurrentState = DoorState.Locked; - _barrierCollision.SetDeferred(CollisionShape2D.PropertyName.Disabled, false); + _barrierCollision?.SetDeferred(CollisionShape2D.PropertyName.Disabled, false); - if (_animPlayer.HasAnimation("lock")) + if (_animPlayer != null && _animPlayer.HasAnimation("lock")) _animPlayer.Play("lock"); } public void UnlockDoor() { CurrentState = DoorState.Open; - _barrierCollision.SetDeferred(CollisionShape2D.PropertyName.Disabled, true); + _barrierCollision?.SetDeferred(CollisionShape2D.PropertyName.Disabled, true); - if (_animPlayer.HasAnimation("unlock")) + if (_animPlayer != null && _animPlayer.HasAnimation("unlock")) _animPlayer.Play("unlock"); } @@ -52,4 +62,4 @@ public partial class Door : Node2D EmitSignal(SignalName.PlayerEnteredDoorway, this); } } -}
\ No newline at end of file +} |
