Automatic box deselection

This commit is contained in:
Marco 2025-01-30 17:43:39 +01:00
commit ad985ce1ac
6 changed files with 73 additions and 19 deletions

View file

@ -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;
}
}

View file

@ -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
}]

View file

@ -8,19 +8,46 @@ public partial class Chest : Interactable
{
[Export] public Array<LootItem> LootTable = new Array<LootItem>();
[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>("AnimatedSprite2D");
}
public override bool CanActivate()
{
return State == ChestState.Closed;
}
}
public enum ChestState
{
Closed,
Open,
Destroyed
}

View file

@ -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;
}
}

View file

@ -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");

View file

@ -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);