mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-01 10:35:34 +00:00
Interactable handling
This commit is contained in:
parent
a4b39a97dc
commit
d804441378
5 changed files with 89 additions and 17 deletions
|
|
@ -9,18 +9,93 @@ public partial class Selector : Node2D
|
|||
private List<Interactable> _interactables = new List<Interactable>();
|
||||
|
||||
private int _selectedInteractable;
|
||||
|
||||
[Signal]
|
||||
public delegate void SelectedItemInteractableChangedEventHandler(Interactable interactable);
|
||||
|
||||
public Interactable SelectedInteractable
|
||||
{
|
||||
get =>
|
||||
_interactables.Count > 0
|
||||
? _selectedInteractable >= _interactables.Count
|
||||
? _interactables[^1]
|
||||
: _interactables[_selectedInteractable]
|
||||
: null;
|
||||
set
|
||||
{
|
||||
// Passing null deselects the interactable
|
||||
if (value == null)
|
||||
{
|
||||
_selectedInteractable = -1;
|
||||
NotifyChanged(null);
|
||||
return;
|
||||
}
|
||||
|
||||
// If it's already in the list, set the current one to it, otherwise add it
|
||||
if (!_interactables.Contains(value))
|
||||
{
|
||||
AddInteractable(value);
|
||||
}
|
||||
|
||||
_selectedInteractable = _interactables.IndexOf(value);
|
||||
NotifyChanged(value);
|
||||
}
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
if (Input.IsActionPressed("scan"))
|
||||
{
|
||||
_selectedInteractable += 1;
|
||||
UpdatePosition();
|
||||
}
|
||||
}
|
||||
|
||||
private void NotifyChanged(Interactable interactable)
|
||||
{
|
||||
EmitSignal(nameof(SelectedItemInteractableChanged), interactable);
|
||||
}
|
||||
|
||||
public void AddInteractable(Interactable interactable)
|
||||
{
|
||||
if (!_interactables.Contains(interactable)) {
|
||||
_interactables.Add(interactable);
|
||||
|
||||
if (_interactables.Count == 1)
|
||||
{
|
||||
_selectedInteractable = 0;
|
||||
}
|
||||
|
||||
}
|
||||
UpdatePosition();
|
||||
}
|
||||
|
||||
public void RemoveInteractable(Interactable interactable)
|
||||
{
|
||||
if (_interactables.Contains(interactable))
|
||||
{
|
||||
_interactables.Remove(interactable);
|
||||
}
|
||||
UpdatePosition();
|
||||
}
|
||||
|
||||
public void Deselect()
|
||||
{
|
||||
_selectedInteractable = -1;
|
||||
this.Visible = false;
|
||||
}
|
||||
|
||||
public void UpdatePosition()
|
||||
{
|
||||
if (SelectedInteractable != null)
|
||||
{
|
||||
this.Position = SelectedInteractable.Position;
|
||||
this.Visible = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Visible = false;
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveInteractable()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue