cirnogodot/Scripts/Selector.cs

149 lines
3.6 KiB
C#
Raw Normal View History

2025-01-30 08:34:09 +01:00
using Godot;
using System;
using System.Collections.Generic;
2025-03-09 21:58:25 +01:00
using Cirno.Scripts.Interactables;
2025-01-30 08:34:09 +01:00
public partial class Selector : Node2D
{
private Interactable _lastInteractable;
2025-03-09 21:58:25 +01:00
private List<IInteractable> _interactables = [];
2025-01-30 08:34:09 +01:00
private int _selectedInteractable;
2025-03-28 12:26:34 +01:00
private int SelectedInteractableIndex
{
get => _selectedInteractable;
set
{
if (value >= _interactables.Count)
{
_selectedInteractable = 0;
}
if (value < 0)
{
_selectedInteractable = _interactables.Count - 1;
}
}
}
2025-03-09 21:58:25 +01:00
//[Signal]
//public delegate void SelectedItemInteractableChangedEventHandler(IInteractable interactable);
2025-03-28 12:26:34 +01:00
2025-03-09 21:58:25 +01:00
public IInteractable SelectedInteractable
2025-01-30 15:45:29 +01:00
{
get =>
_interactables.Count > 0
2025-03-28 12:26:34 +01:00
? SelectedInteractableIndex >= _interactables.Count
2025-01-30 15:45:29 +01:00
? _interactables[^1]
2025-03-28 12:26:34 +01:00
: _interactables[SelectedInteractableIndex]
2025-01-30 15:45:29 +01:00
: null;
set
{
// Passing null deselects the interactable
if (value == null)
{
_selectedInteractable = -1;
NotifyChanged(null);
return;
}
2025-03-28 12:26:34 +01:00
2025-01-30 15:45:29 +01:00
// If it's already in the list, set the current one to it, otherwise add it
if (!_interactables.Contains(value))
{
AddInteractable(value);
}
2025-03-28 12:26:34 +01:00
SelectedInteractableIndex = _interactables.IndexOf(value);
2025-01-30 15:45:29 +01:00
NotifyChanged(value);
}
}
2025-03-01 18:02:11 +01:00
// 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;
// // }
// }
2025-01-30 17:43:39 +01:00
public void SelectNext()
{
2025-03-28 12:26:34 +01:00
SelectedInteractableIndex += 1;
// _selectedInteractable += 1;
// if (_selectedInteractable >= _interactables.Count)
// {
// _selectedInteractable = 0;
// }
2025-01-30 17:43:39 +01:00
if (_interactables.Count > 0)
{
2025-01-30 15:58:55 +01:00
SelectedInteractable = _interactables[_selectedInteractable];
2025-01-30 15:45:29 +01:00
}
2025-01-30 17:43:39 +01:00
else
{
_selectedInteractable = -1;
}
2025-03-28 12:26:34 +01:00
2025-01-30 17:43:39 +01:00
UpdatePosition();
2025-01-30 15:45:29 +01:00
}
2025-03-09 21:58:25 +01:00
private void NotifyChanged(IInteractable interactable)
2025-01-30 15:45:29 +01:00
{
2025-03-09 21:58:25 +01:00
//EmitSignal(nameof(SelectedItemInteractableChanged), interactable);
2025-01-30 15:45:29 +01:00
}
2025-01-30 08:34:09 +01:00
2025-03-09 21:58:25 +01:00
public void AddInteractable(IInteractable interactable)
2025-01-30 08:34:09 +01:00
{
2025-03-28 12:26:34 +01:00
if (!_interactables.Contains(interactable))
{
2025-01-30 08:34:09 +01:00
_interactables.Add(interactable);
2025-01-30 15:45:29 +01:00
if (_interactables.Count == 1)
{
_selectedInteractable = 0;
}
2025-01-30 08:34:09 +01:00
}
2025-03-28 12:26:34 +01:00
2025-01-30 15:45:29 +01:00
UpdatePosition();
2025-01-30 08:34:09 +01:00
}
2025-03-09 21:58:25 +01:00
public void RemoveInteractable(IInteractable interactable)
2025-01-30 08:34:09 +01:00
{
2025-01-30 15:45:29 +01:00
if (_interactables.Contains(interactable))
{
_interactables.Remove(interactable);
}
2025-03-28 12:26:34 +01:00
2025-01-30 15:45:29 +01:00
UpdatePosition();
}
2025-01-30 08:34:09 +01:00
2025-01-30 15:45:29 +01:00
public void Deselect()
{
_selectedInteractable = -1;
this.Visible = false;
2025-01-30 08:34:09 +01:00
}
2025-01-30 15:45:29 +01:00
public void UpdatePosition()
{
if (SelectedInteractable != null)
{
2025-03-23 17:08:04 +01:00
this.GlobalPosition = SelectedInteractable.GetGlobalPosition();
2025-01-30 15:45:29 +01:00
this.Visible = true;
}
else
{
this.Visible = false;
}
}
2025-03-28 12:26:34 +01:00
}