mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-08 06:25:54 +00:00
Automatic box deselection
This commit is contained in:
parent
cc5376e94e
commit
ad985ce1ac
6 changed files with 73 additions and 19 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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");
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue