2025-06-12 18:03:55 +02:00
|
|
|
|
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();
|
2025-08-07 15:09:02 +02:00
|
|
|
|
|
|
|
|
|
|
SpawnSelector();
|
2025-06-12 18:03:55 +02:00
|
|
|
|
|
|
|
|
|
|
// if (SelectorScene is not null && _selector is null)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// _selector = actor.CreateSibling<Selector>(SelectorScene, this.GlobalPosition);
|
|
|
|
|
|
// _selector.Visible = false;
|
|
|
|
|
|
// }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-07 15:09:02 +02:00
|
|
|
|
private void SpawnSelector()
|
|
|
|
|
|
{
|
|
|
|
|
|
var sel = SelectorScene.Instantiate<Node3D>();
|
|
|
|
|
|
StateMachine.MainObject.GetParent().AddChild(sel);
|
|
|
|
|
|
sel.GlobalPosition = StateMachine.MainObject.GlobalPosition;
|
|
|
|
|
|
|
|
|
|
|
|
_selectorController.ShowSelector += () =>
|
|
|
|
|
|
{
|
|
|
|
|
|
sel.Show();
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
_selectorController.HideSelector += () =>
|
|
|
|
|
|
{
|
|
|
|
|
|
sel.Reparent(this);
|
|
|
|
|
|
sel.Hide();
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
_selectorController.ChangeParent3D += parent =>
|
|
|
|
|
|
{
|
|
|
|
|
|
if (parent is not null)
|
|
|
|
|
|
{
|
|
|
|
|
|
sel.Reparent(parent);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
sel.Reparent(this);
|
|
|
|
|
|
sel.Hide();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sel.Position = Vector3.Zero;
|
|
|
|
|
|
};
|
|
|
|
|
|
// if (Hud.Instance is not null)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// Hud.Instance.CreateSelector(_selectorController);
|
|
|
|
|
|
// }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-12 18:03:55 +02:00
|
|
|
|
public void Process(double delta)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void PhysicsProcess(double delta)
|
|
|
|
|
|
{
|
2025-08-07 15:09:02 +02:00
|
|
|
|
//_selectorController.PhysicsProcess(delta);
|
2025-06-13 17:46:44 +02:00
|
|
|
|
|
|
|
|
|
|
HandleInteraction();
|
2025-06-12 18:03:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
2025-08-07 15:09:02 +02:00
|
|
|
|
_selectorController.RemoveInteractable(selected);
|
2025-06-12 18:03:55 +02:00
|
|
|
|
_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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|