mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-11 16:15:54 +00:00
Interaction manager and hud
This commit is contained in:
parent
b1afc7af1c
commit
5271b84923
20 changed files with 539 additions and 29 deletions
|
|
@ -13,8 +13,7 @@ public partial class ActivationProvider : Area2D
|
|||
[Export]
|
||||
public PackedScene SelectorScene { get; set; }
|
||||
|
||||
[Export]
|
||||
private InputProvider _inputProvider;
|
||||
[Export] private InputProvider _inputProvider;
|
||||
|
||||
[Export] private AudioStreamPlayer2D _errorSound;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
using Cirno.Scripts;
|
||||
using Cirno.Scripts.Components.Actors;
|
||||
using Godot;
|
||||
|
||||
|
|
|
|||
143
Scripts/Components/FSM/3DPlayer/IsoActivationProvider.cs
Normal file
143
Scripts/Components/FSM/3DPlayer/IsoActivationProvider.cs
Normal file
|
|
@ -0,0 +1,143 @@
|
|||
using Cirno.Scripts.Components.Actors;
|
||||
using Cirno.Scripts.Interactables;
|
||||
using Godot;
|
||||
|
||||
namespace Cirno.Scripts.Components.FSM._3DPlayer;
|
||||
|
||||
public partial class IsoActivationProvider: Area3D, IModule<PlayerState, CharacterBody3D>
|
||||
{
|
||||
|
||||
private bool _enabled = false;
|
||||
|
||||
public bool Enabled
|
||||
{
|
||||
get => _enabled;
|
||||
set
|
||||
{
|
||||
if (_enabled == value) return;
|
||||
_enabled = value;
|
||||
}
|
||||
}
|
||||
|
||||
[Export] private InputProvider _inputProvider;
|
||||
[Export] private SelectorController _selectorController;
|
||||
|
||||
[Export] private AudioStreamPlayer _errorSound;
|
||||
|
||||
[Export] public PackedScene SelectorScene { get; set; }
|
||||
|
||||
|
||||
public IStateMachine<PlayerState, CharacterBody3D> StateMachine { get; private set; }
|
||||
|
||||
public void EnterState(PlayerState state)
|
||||
{
|
||||
Enabled = true;
|
||||
AreaEntered += _on_interaction_controller_area_entered;
|
||||
AreaExited += _on_interaction_controller_area_exited;
|
||||
}
|
||||
|
||||
public void ExitState(PlayerState state)
|
||||
{
|
||||
Enabled = false;
|
||||
AreaEntered -= _on_interaction_controller_area_entered;
|
||||
AreaExited -= _on_interaction_controller_area_exited;
|
||||
}
|
||||
|
||||
public void Init(IStateMachine<PlayerState, CharacterBody3D> machine)
|
||||
{
|
||||
StateMachine = machine;
|
||||
|
||||
_selectorController.Hide();
|
||||
|
||||
// TODO: Create selector UI element and link the signals
|
||||
|
||||
// if (SelectorScene is not null && _selector is null)
|
||||
// {
|
||||
// _selector = actor.CreateSibling<Selector>(SelectorScene, this.GlobalPosition);
|
||||
// _selector.Visible = false;
|
||||
// }
|
||||
}
|
||||
|
||||
public void Process(double delta)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void PhysicsProcess(double delta)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void HandleInteraction()
|
||||
{
|
||||
if (_inputProvider.GetUseJustPressed())
|
||||
{
|
||||
if (!TrySelect())
|
||||
{
|
||||
_selectorController.SelectNext();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (_inputProvider.GetScanJustPressed())
|
||||
{
|
||||
_selectorController.SelectNext();
|
||||
}
|
||||
}
|
||||
|
||||
private bool TrySelect()
|
||||
{
|
||||
var selected = _selectorController.SelectedInteractable;
|
||||
if (selected is null)
|
||||
{
|
||||
_errorSound?.Play();
|
||||
return false;
|
||||
};
|
||||
if (!selected.CanActivate())
|
||||
{
|
||||
_errorSound?.Play();
|
||||
return true;
|
||||
};
|
||||
bool success = selected.Activate(ActivationType.Use);
|
||||
|
||||
if (success)
|
||||
{
|
||||
// Deselect and scan for next
|
||||
_selectorController.SelectNext();
|
||||
//_selector.RemoveInteractable(selected);
|
||||
}
|
||||
else
|
||||
{
|
||||
_errorSound?.Play();
|
||||
}
|
||||
|
||||
return true;
|
||||
//var spaceState = GetWorld2D().DirectSpaceState;
|
||||
|
||||
//var query = PhysicsRayQueryParameters2D.Create(Vector2.Zero, )
|
||||
}
|
||||
|
||||
private void _on_interaction_controller_area_entered(Area3D area)
|
||||
{
|
||||
if (!Enabled) return;
|
||||
if (area.IsInGroup("Interactable") && area is IInteractable interactable && interactable.CanActivate())
|
||||
{
|
||||
|
||||
//if (_selector == null) return;
|
||||
|
||||
_selectorController.AddInteractable(interactable);
|
||||
}
|
||||
}
|
||||
|
||||
private void _on_interaction_controller_area_exited(Area3D area)
|
||||
{
|
||||
//if (!Enabled) return;
|
||||
if (area.IsInGroup("Interactable") && area is IInteractable interactable)
|
||||
{
|
||||
|
||||
//if (_selector == null) return;
|
||||
_selectorController.RemoveInteractable(interactable);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
uid://vne180ohyucn
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
uid://d1ixvdcii6uy7
|
||||
Loading…
Add table
Add a link
Reference in a new issue