mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-12 05:15:54 +00:00
Selector Fix
This commit is contained in:
parent
c6fa31188a
commit
7c2d01a52e
3 changed files with 109 additions and 49 deletions
|
|
@ -6,9 +6,8 @@ using Godot;
|
|||
|
||||
namespace Cirno.Scripts.Components.FSM._3DPlayer;
|
||||
|
||||
public partial class IsoActivationProvider: Area3D, IModule<PlayerState, CharacterBody3D>
|
||||
public partial class IsoActivationProvider : Area3D, IModule<PlayerState, CharacterBody3D>
|
||||
{
|
||||
|
||||
private bool _enabled = false;
|
||||
|
||||
public bool Enabled
|
||||
|
|
@ -20,19 +19,21 @@ public partial class IsoActivationProvider: Area3D, IModule<PlayerState, Charact
|
|||
_enabled = value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[Export] private InputProvider _inputProvider;
|
||||
[Export] private SelectorController _selectorController;
|
||||
|
||||
[Export] private AudioStreamPlayer _errorSound;
|
||||
|
||||
|
||||
[Export] public PackedScene SelectorScene { get; set; }
|
||||
|
||||
|
||||
|
||||
|
||||
private Node3D _selector;
|
||||
|
||||
public IStateMachine<PlayerState, CharacterBody3D> StateMachine { get; private set; }
|
||||
|
||||
|
||||
private CollisionShape3D _collisionShape;
|
||||
|
||||
|
||||
public void EnterState(PlayerState state)
|
||||
{
|
||||
Enabled = true;
|
||||
|
|
@ -50,13 +51,15 @@ public partial class IsoActivationProvider: Area3D, IModule<PlayerState, Charact
|
|||
public void Init(IStateMachine<PlayerState, CharacterBody3D> machine)
|
||||
{
|
||||
StateMachine = machine;
|
||||
|
||||
|
||||
_collisionShape = GetNode<CollisionShape3D>("CollisionShape");
|
||||
|
||||
|
||||
_selectorController.Hide();
|
||||
|
||||
SpawnSelector();
|
||||
|
||||
|
||||
_selectorController.ShowSelector += ShowSelector;
|
||||
_selectorController.HideSelector += HideSelector;
|
||||
_selectorController.ChangeParent3D += OnChangeParent3D;
|
||||
|
||||
// if (SelectorScene is not null && _selector is null)
|
||||
// {
|
||||
// _selector = actor.CreateSibling<Selector>(SelectorScene, this.GlobalPosition);
|
||||
|
|
@ -64,16 +67,57 @@ public partial class IsoActivationProvider: Area3D, IModule<PlayerState, Charact
|
|||
// }
|
||||
}
|
||||
|
||||
private void SpawnSelector()
|
||||
private void OnChangeParent3D(Node3D node)
|
||||
{
|
||||
if (IsInstanceValid(_selector) && _selector is not null)
|
||||
{
|
||||
if (node is null)
|
||||
{
|
||||
_selector.Hide();
|
||||
}
|
||||
else
|
||||
{
|
||||
_selector.GlobalPosition = node.GlobalPosition;
|
||||
_selector.Show();
|
||||
//_selector.Reparent(node);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_selector = SelectorScene.Instantiate<Node3D>();
|
||||
//node.AddChild(_selector);
|
||||
StateMachine.MainObject.GetParent().AddChild(_selector);
|
||||
//sel.GlobalPosition = StateMachine.MainObject.GlobalPosition;
|
||||
|
||||
_selector.GlobalPosition = node.GlobalPosition;
|
||||
}
|
||||
//_selector.Position = Vector3.Zero;
|
||||
}
|
||||
|
||||
private void ShowSelector()
|
||||
{
|
||||
if (IsInstanceValid(_selector))
|
||||
{
|
||||
_selector?.Show();
|
||||
}
|
||||
}
|
||||
|
||||
private void HideSelector()
|
||||
{
|
||||
if (IsInstanceValid(_selector))
|
||||
{
|
||||
_selector?.Hide();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void SpawnSelector(Node3D parent)
|
||||
{
|
||||
var sel = SelectorScene.Instantiate<Node3D>();
|
||||
StateMachine.MainObject.GetParent().AddChild(sel);
|
||||
sel.GlobalPosition = StateMachine.MainObject.GlobalPosition;
|
||||
|
||||
_selectorController.ShowSelector += () =>
|
||||
{
|
||||
sel.Show();
|
||||
};
|
||||
_selectorController.ShowSelector += () => { sel.Show(); };
|
||||
|
||||
_selectorController.HideSelector += () =>
|
||||
{
|
||||
|
|
@ -103,13 +147,12 @@ public partial class IsoActivationProvider: Area3D, IModule<PlayerState, Charact
|
|||
|
||||
public void Process(double delta)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void PhysicsProcess(double delta)
|
||||
{
|
||||
_selectorController.PhysicsProcess(delta);
|
||||
|
||||
|
||||
HandleInteraction();
|
||||
}
|
||||
|
||||
|
|
@ -135,29 +178,33 @@ public partial class IsoActivationProvider: Area3D, IModule<PlayerState, Charact
|
|||
|
||||
private bool TrySelect()
|
||||
{
|
||||
var selected = _selectorController.SelectedInteractable;
|
||||
var selected = _selectorController.GetSelectedInteractable();
|
||||
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.RemoveInteractable(selected);
|
||||
_selectorController.DeselectWithCooldown();
|
||||
|
||||
//_selectorController.DeselectWithCooldown();
|
||||
_selectorController.Deselect();
|
||||
|
||||
// Do this at end of frame instead
|
||||
//_selectorController.SelectNext();
|
||||
_selectorController.SelectNextDelayed();
|
||||
|
||||
_selectorController.SelectNext();
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -171,7 +218,6 @@ public partial class IsoActivationProvider: Area3D, IModule<PlayerState, Charact
|
|||
}
|
||||
|
||||
|
||||
|
||||
private List<IInteractable> ScanTargets()
|
||||
{
|
||||
var spaceState = GetWorld3D().DirectSpaceState;
|
||||
|
|
@ -182,7 +228,6 @@ public partial class IsoActivationProvider: Area3D, IModule<PlayerState, Charact
|
|||
CollideWithAreas = true,
|
||||
CollisionMask = this.CollisionMask,
|
||||
Exclude = [GetRid()],
|
||||
|
||||
};
|
||||
|
||||
var targets = spaceState.IntersectShape(query);
|
||||
|
|
@ -204,25 +249,23 @@ public partial class IsoActivationProvider: Area3D, IModule<PlayerState, Charact
|
|||
|
||||
return found;
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue