mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-09 18:45:55 +00:00
FSM Selector with sound
This commit is contained in:
parent
f6d159b9c4
commit
ee09c50dbd
20 changed files with 330 additions and 28 deletions
106
Scripts/Components/Actors/ActivationProvider.cs
Normal file
106
Scripts/Components/Actors/ActivationProvider.cs
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
using Godot;
|
||||
|
||||
namespace Cirno.Scripts.Components.Actors;
|
||||
|
||||
public partial class ActivationProvider : Area2D
|
||||
{
|
||||
public bool Enabled { get; set; } = false;
|
||||
|
||||
private Selector _selector;
|
||||
|
||||
[Export]
|
||||
public PackedScene SelectorScene { get; set; }
|
||||
|
||||
[Export]
|
||||
private InputProvider _inputProvider;
|
||||
|
||||
[Export] private AudioStreamPlayer2D _errorSound;
|
||||
|
||||
[Signal]
|
||||
public delegate void InteractableAreaEnteredEventHandler(Interactable interactable);
|
||||
|
||||
[Signal]
|
||||
public delegate void InteractableAreaExitedEventHandler(Interactable interactable);
|
||||
|
||||
public void Init()
|
||||
{
|
||||
if (SelectorScene is not null && _selector is null)
|
||||
{
|
||||
_selector = this.CreateSibling<Selector>(SelectorScene, this.GlobalPosition);
|
||||
_selector.Visible = false;
|
||||
}
|
||||
}
|
||||
|
||||
public void HandleInteraction()
|
||||
{
|
||||
if (_inputProvider.GetUseJustPressed())
|
||||
{
|
||||
TrySelect();
|
||||
}
|
||||
|
||||
if (_inputProvider.GetScanJustPressed())
|
||||
{
|
||||
_selector.SelectNext();
|
||||
}
|
||||
}
|
||||
|
||||
private void TrySelect()
|
||||
{
|
||||
var selected = _selector.SelectedInteractable;
|
||||
if (selected is null)
|
||||
{
|
||||
_errorSound?.Play();
|
||||
return;
|
||||
};
|
||||
if (!selected.CanActivate())
|
||||
{
|
||||
_errorSound?.Play();
|
||||
return;
|
||||
};
|
||||
bool success = selected.Activate();
|
||||
|
||||
if (success)
|
||||
{
|
||||
// Deselect and scan for next
|
||||
_selector.RemoveInteractable(selected);
|
||||
//_selector.SelectedInteractable = null;
|
||||
//_selector.SelectNext();
|
||||
}
|
||||
else
|
||||
{
|
||||
_errorSound?.Play();
|
||||
}
|
||||
|
||||
//var spaceState = GetWorld2D().DirectSpaceState;
|
||||
|
||||
//var query = PhysicsRayQueryParameters2D.Create(Vector2.Zero, )
|
||||
}
|
||||
|
||||
private void _on_interaction_controller_area_entered(Area2D area)
|
||||
{
|
||||
if (!Enabled) return;
|
||||
if (area.IsInGroup("Interactable") && area is Interactable interactable && interactable.CanActivate())
|
||||
{
|
||||
EmitSignal(nameof(InteractableAreaEntered), interactable);
|
||||
|
||||
if (_selector == null) return;
|
||||
|
||||
_selector.AddInteractable(interactable);
|
||||
|
||||
//_selector.SelectedInteractable = interactable;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void _on_interaction_controller_area_exited(Area2D area)
|
||||
{
|
||||
if (!Enabled) return;
|
||||
if (area.IsInGroup("Interactable") && area is Interactable interactable)
|
||||
{
|
||||
EmitSignal(nameof(InteractableAreaExited), interactable);
|
||||
|
||||
if (_selector == null) return;
|
||||
_selector.RemoveInteractable(interactable);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue