mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-01 11:15:33 +00:00
129 lines
3.1 KiB
C#
129 lines
3.1 KiB
C#
using Godot;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
public partial class Selector : Node2D
|
|
{
|
|
private Interactable _lastInteractable;
|
|
|
|
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.IsActionJustPressed("scan"))
|
|
{
|
|
SelectNext();
|
|
}
|
|
|
|
// if (SelectedInteractable is not null) {
|
|
// this.Visible = true;
|
|
// this.GlobalPosition = SelectedInteractable.GlobalPosition;
|
|
// }
|
|
// else
|
|
// {
|
|
// this.Visible = false;
|
|
// }
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
}
|