mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-01 10:05:34 +00:00
Interaction manager and hud
This commit is contained in:
parent
b1afc7af1c
commit
5271b84923
20 changed files with 539 additions and 29 deletions
139
Scripts/Components/FSM/3DPlayer/SelectorController.cs
Normal file
139
Scripts/Components/FSM/3DPlayer/SelectorController.cs
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
using System.Collections.Generic;
|
||||
using Cirno.Scripts.Interactables;
|
||||
using Godot;
|
||||
|
||||
namespace Cirno.Scripts.Components.FSM._3DPlayer;
|
||||
|
||||
public partial class SelectorController : Node
|
||||
{
|
||||
[Signal] public delegate void ShowSelectorEventHandler();
|
||||
[Signal] public delegate void HideSelectorEventHandler();
|
||||
[Signal] public delegate void ChangePositionEventHandler(Vector2 position);
|
||||
|
||||
private Interactable _lastInteractable;
|
||||
|
||||
private List<IInteractable> _interactables = [];
|
||||
|
||||
private int _selectedInteractable;
|
||||
|
||||
private int SelectedInteractableIndex
|
||||
{
|
||||
get => _selectedInteractable;
|
||||
set
|
||||
{
|
||||
if (value >= _interactables.Count)
|
||||
{
|
||||
_selectedInteractable = 0;
|
||||
}
|
||||
|
||||
if (value < 0)
|
||||
{
|
||||
_selectedInteractable = _interactables.Count - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public IInteractable SelectedInteractable
|
||||
{
|
||||
get =>
|
||||
_interactables.Count > 0
|
||||
? SelectedInteractableIndex >= _interactables.Count
|
||||
? _interactables[^1]
|
||||
: _interactables[SelectedInteractableIndex]
|
||||
: 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);
|
||||
}
|
||||
|
||||
SelectedInteractableIndex = _interactables.IndexOf(value);
|
||||
NotifyChanged(value);
|
||||
}
|
||||
}
|
||||
|
||||
public void SelectNext()
|
||||
{
|
||||
SelectedInteractableIndex += 1;
|
||||
// _selectedInteractable += 1;
|
||||
// if (_selectedInteractable >= _interactables.Count)
|
||||
// {
|
||||
// _selectedInteractable = 0;
|
||||
// }
|
||||
|
||||
if (_interactables.Count > 0)
|
||||
{
|
||||
SelectedInteractable = _interactables[_selectedInteractable];
|
||||
}
|
||||
else
|
||||
{
|
||||
_selectedInteractable = -1;
|
||||
}
|
||||
|
||||
UpdatePosition();
|
||||
}
|
||||
|
||||
public void AddInteractable(IInteractable interactable)
|
||||
{
|
||||
if (!_interactables.Contains(interactable))
|
||||
{
|
||||
_interactables.Add(interactable);
|
||||
|
||||
if (_interactables.Count == 1)
|
||||
{
|
||||
_selectedInteractable = 0;
|
||||
}
|
||||
}
|
||||
|
||||
UpdatePosition();
|
||||
}
|
||||
|
||||
public void RemoveInteractable(IInteractable interactable)
|
||||
{
|
||||
if (_interactables.Contains(interactable))
|
||||
{
|
||||
_interactables.Remove(interactable);
|
||||
}
|
||||
|
||||
UpdatePosition();
|
||||
}
|
||||
|
||||
public void Deselect()
|
||||
{
|
||||
_selectedInteractable = -1;
|
||||
EmitSignalHideSelector();
|
||||
}
|
||||
|
||||
public void UpdatePosition()
|
||||
{
|
||||
if (SelectedInteractable != null)
|
||||
{
|
||||
EmitSignalChangePosition(SelectedInteractable.GetGlobalPosition());
|
||||
EmitSignalShowSelector();
|
||||
}
|
||||
else
|
||||
{
|
||||
EmitSignalHideSelector();
|
||||
}
|
||||
}
|
||||
|
||||
private void NotifyChanged(IInteractable interactable)
|
||||
{
|
||||
//EmitSignal(nameof(SelectedItemInteractableChanged), interactable);
|
||||
}
|
||||
|
||||
public void Hide()
|
||||
{
|
||||
EmitSignalHideSelector();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue