diff --git a/Scenes/Interactable.cs b/Scenes/Interactable.cs index b1fd36af..d1a83872 100644 --- a/Scenes/Interactable.cs +++ b/Scenes/Interactable.cs @@ -36,8 +36,13 @@ public partial class Interactable : Area2D return true; } - public virtual void Activate() + public virtual bool Activate() { - + return true; + } + + public virtual bool CanActivate() + { + return true; } } diff --git a/Scenes/Interactable/Chest.tscn b/Scenes/Interactable/Chest.tscn index 67d8ec67..f11d1e19 100644 --- a/Scenes/Interactable/Chest.tscn +++ b/Scenes/Interactable/Chest.tscn @@ -36,7 +36,7 @@ animations = [{ "duration": 1.0, "texture": SubResource("AtlasTexture_elja3") }], -"loop": true, +"loop": false, "name": &"Closed", "speed": 5.0 }, { @@ -47,7 +47,7 @@ animations = [{ "duration": 1.0, "texture": SubResource("AtlasTexture_f10r8") }], -"loop": true, +"loop": false, "name": &"Closing", "speed": 5.0 }, { @@ -55,7 +55,7 @@ animations = [{ "duration": 1.0, "texture": SubResource("AtlasTexture_faho7") }], -"loop": true, +"loop": false, "name": &"Open", "speed": 5.0 }, { @@ -66,7 +66,7 @@ animations = [{ "duration": 1.0, "texture": SubResource("AtlasTexture_x1bfm") }], -"loop": true, +"loop": false, "name": &"Opening", "speed": 5.0 }] diff --git a/Scripts/Chest.cs b/Scripts/Chest.cs index 8460ee1c..cbd73ff5 100644 --- a/Scripts/Chest.cs +++ b/Scripts/Chest.cs @@ -8,19 +8,46 @@ public partial class Chest : Interactable { [Export] public Array LootTable = new Array(); + + [Export] public ChestState State = ChestState.Closed; - public override void Activate() + private AnimatedSprite2D _sprite; + + public override bool Activate() { - if (!MeetsRequirements()) return; + GD.Print("Attempting to open chest"); + if (State != ChestState.Closed) return false; + if (!MeetsRequirements()) return false; foreach (var item in LootTable) { _inventoryManager.AddItem(item.Item, item.Amount); } + + _sprite.Play("Opening"); + State = ChestState.Open; + + return true; } public override void _Ready() { base._Ready(); + + _sprite = GetNode("AnimatedSprite2D"); + + + } + + public override bool CanActivate() + { + return State == ChestState.Closed; } +} + +public enum ChestState +{ + Closed, + Open, + Destroyed } \ No newline at end of file diff --git a/Scripts/Interactables/Switch.cs b/Scripts/Interactables/Switch.cs index 9ce7feba..b2253444 100644 --- a/Scripts/Interactables/Switch.cs +++ b/Scripts/Interactables/Switch.cs @@ -6,11 +6,14 @@ public partial class Switch : Interactable { [Export] public Activable Target { get; set; } - public override void Activate() + public override bool Activate() { if (MeetsRequirements()) { Target?.Activate(); + return true; } + + return false; } } \ No newline at end of file diff --git a/Scripts/PlayerMovement.cs b/Scripts/PlayerMovement.cs index c66643a7..23276f32 100644 --- a/Scripts/PlayerMovement.cs +++ b/Scripts/PlayerMovement.cs @@ -121,9 +121,15 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible private void FindInteractable() { - if (Input.IsActionJustPressed("Use") && _selector.SelectedInteractable != null) + if (!Input.IsActionJustPressed("Use") || _selector.SelectedInteractable == null) return; + if (!_selector.SelectedInteractable.CanActivate()) return; + bool success = _selector.SelectedInteractable.Activate(); + + if (success) { - _selector.SelectedInteractable.Activate(); + // Deselect and scan for next + _selector.SelectedInteractable = null; + _selector.SelectNext(); } //var spaceState = GetWorld2D().DirectSpaceState; @@ -261,7 +267,7 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible private void _on_interaction_controller_area_entered(Area2D area) { // Replace with function body. - if (area.IsInGroup("Interactable") && area is Interactable interactable) + if (area.IsInGroup("Interactable") && area is Interactable interactable && interactable.CanActivate()) { Debug.WriteLine($"Interactable {area.Name} Entered"); diff --git a/Scripts/Selector.cs b/Scripts/Selector.cs index d95b0443..103258e3 100644 --- a/Scripts/Selector.cs +++ b/Scripts/Selector.cs @@ -46,18 +46,31 @@ public partial class Selector : Node2D { if (Input.IsActionJustPressed("scan")) { - _selectedInteractable += 1; - if (_selectedInteractable >= _interactables.Count) - { - _selectedInteractable = 0; - } - - SelectedInteractable = _interactables[_selectedInteractable]; - UpdatePosition(); + SelectNext(); } } + public void SelectNext() + { + _selectedInteractable += 1; + if (_selectedInteractable >= _interactables.Count) + { + _selectedInteractable = 0; + } + + if (_interactables.Count > 0) + { + SelectedInteractable = _interactables[_selectedInteractable]; + } + else + { + _selectedInteractable = -1; + } + + UpdatePosition(); + } + private void NotifyChanged(Interactable interactable) { EmitSignal(nameof(SelectedItemInteractableChanged), interactable);