1
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
using System;
|
||||
using BITKit.Selection;
|
||||
using BITKit.Sensors;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
using UnityEngine.InputSystem.Interactions;
|
||||
namespace BITKit.Entities.Player
|
||||
{
|
||||
public class EntityInteractive : EntityPlayerComponent
|
||||
[CustomType(typeof(ISelector))]
|
||||
public class EntityInteractive : EntityPlayerComponent,ISelector
|
||||
{
|
||||
[Header(Constant.Header.Settings)]
|
||||
[SerializeReference, SubclassSelector] private ISensor sensor;
|
||||
@@ -12,9 +15,23 @@ namespace BITKit.Entities.Player
|
||||
[Header(Constant.Header.InternalVariables)]
|
||||
private ISelectable selected;
|
||||
private IntervalUpdate cd = new(0.08f);
|
||||
[Inject]
|
||||
private IHealth _health;
|
||||
|
||||
public override void OnStart()
|
||||
{
|
||||
_health.OnSetAlive += OnSetAlive;
|
||||
}
|
||||
|
||||
private void OnSetAlive(bool obj)
|
||||
{
|
||||
TryDeSelected();
|
||||
}
|
||||
|
||||
public override void OnUpdate(float deltaTime)
|
||||
{
|
||||
if (sensor.Get().TryGetAny(x=>x.TryGetComponentAny<ISelectable>(out _),out var detected))
|
||||
//if (sensor.Get().TryGetAny(x=>x.TryGetComponentAny<ISelectable>(out _),out var detected))
|
||||
if (sensor.Get().TryGetAny(x=>x.GetComponentInParent<ISelectable>() is not null,out var detected))
|
||||
{
|
||||
if (detected.TryGetComponentAny<ISelectable>(out var _detected))
|
||||
{
|
||||
@@ -44,34 +61,44 @@ namespace BITKit.Entities.Player
|
||||
{
|
||||
if (selected is null) return;
|
||||
selected.SetSelectionState(SelectionState.None);
|
||||
foreach (var x in entity.GetCallbacks<ISelectableCallback>())
|
||||
{
|
||||
x.OnInactive(selected);
|
||||
}
|
||||
OnInactive?.Invoke(selected);
|
||||
selected = null;
|
||||
}
|
||||
private void Detected(ISelectable detected)
|
||||
{
|
||||
selected = detected;
|
||||
detected.SetSelectionState(SelectionState.Hover);
|
||||
foreach (var x in entity.GetCallbacks<ISelectableCallback>())
|
||||
{
|
||||
x.OnHover(selected);
|
||||
}
|
||||
OnSelected?.Invoke(selected);
|
||||
}
|
||||
|
||||
public void Interactive(InputAction.CallbackContext context)
|
||||
{
|
||||
if (context.interaction is not PressInteraction || !context.performed || cd.AllowUpdate is false) return;
|
||||
if (context.interaction is not PressInteraction || !context.performed ) return;
|
||||
if (cd.AllowUpdate is false) return;
|
||||
var _selected = selected;
|
||||
if (_selected is not MonoBehaviour monoBehaviour) return;
|
||||
if (monoBehaviour.TryGetComponentAny<IAction>(out var action))
|
||||
{
|
||||
action.Execute();
|
||||
}
|
||||
foreach (var x in entity.GetCallbacks<ISelectableCallback>())
|
||||
{
|
||||
x.OnActive(_selected);
|
||||
}
|
||||
selected.SetSelectionState(SelectionState.Active);
|
||||
OnActive?.Invoke(selected);
|
||||
}
|
||||
|
||||
public bool TryGetCurrentSelectable(out ISelectable selectable)
|
||||
{
|
||||
selectable = selected;
|
||||
return selectable != null;
|
||||
}
|
||||
|
||||
public event Action<ISelectable> OnNone;
|
||||
public event Action<ISelectable> OnHover;
|
||||
public event Action<ISelectable> OnActive;
|
||||
public event Action<ISelectable> OnInactive;
|
||||
public event Action<ISelectable> OnFocus;
|
||||
public event Action<ISelectable> OnSelected;
|
||||
public event Action<ISelectable> OnEnabled;
|
||||
public event Action<ISelectable> OnChecked;
|
||||
public event Action<ISelectable> OnRoot;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user