BITFALL/Assets/BITKit/Unity/Scripts/Entity/Components/Interactive/EntityInteractive.cs

141 lines
4.5 KiB
C#
Raw Normal View History

2023-10-20 19:31:12 +08:00
using System;
2023-11-30 00:23:23 +08:00
using BITKit.PlayerCamera;
2023-10-20 19:31:12 +08:00
using BITKit.Selection;
2023-10-02 23:24:56 +08:00
using BITKit.Sensors;
2023-08-23 01:59:40 +08:00
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Interactions;
2023-08-27 02:58:19 +08:00
namespace BITKit.Entities.Player
2023-08-23 01:59:40 +08:00
{
2023-10-20 19:31:12 +08:00
[CustomType(typeof(ISelector))]
2023-10-30 01:25:53 +08:00
public class EntityInteractive : EntityPlayerBehavior,ISelector
2023-08-23 01:59:40 +08:00
{
[Header(Constant.Header.Settings)]
2023-10-02 23:24:56 +08:00
[SerializeReference, SubclassSelector] private ISensor sensor;
2023-08-23 01:59:40 +08:00
2023-11-30 00:23:23 +08:00
[Header(Constant.Header.Gameobjects)]
[SerializeField] private Transform sensorTransform;
2023-08-23 01:59:40 +08:00
[Header(Constant.Header.InternalVariables)]
private ISelectable selected;
private IntervalUpdate cd = new(0.08f);
2023-10-20 19:31:12 +08:00
[Inject]
private IHealth _health;
2023-11-30 00:23:23 +08:00
[Inject(true)] private IEntityMovement _movement;
[Inject(true)] IPlayerCameraService _cameraService;
2023-10-20 19:31:12 +08:00
public override void OnStart()
{
_health.OnSetAlive += OnSetAlive;
}
private void OnSetAlive(bool obj)
{
TryDeSelected();
}
2023-08-23 01:59:40 +08:00
public override void OnUpdate(float deltaTime)
{
2023-11-30 00:23:23 +08:00
if (_cameraService is not null && _movement is not null)
{
if (_cameraService.IsCameraActivated)
{
sensorTransform.SetPositionAndRotation(_cameraService.CameraPosition, _cameraService.CameraRotation);
}
else
{
sensorTransform.position = _movement.Position + _movement.Rotation * _movement.ViewCenter;
sensorTransform.rotation = _movement.ViewRotation;
}
}
2023-10-20 19:31:12 +08:00
//if (sensor.Get().TryGetAny(x=>x.TryGetComponentAny<ISelectable>(out _),out var detected))
2023-11-15 23:54:54 +08:00
try
2023-08-23 01:59:40 +08:00
{
2023-11-15 23:54:54 +08:00
if (sensor.Get().TryGetAny(x=>x.GetComponentInParent<ISelectable>() is not null,out var detected))
2023-08-23 01:59:40 +08:00
{
2023-11-15 23:54:54 +08:00
if (detected.TryGetComponentAny<ISelectable>(out var _detected))
2023-08-23 01:59:40 +08:00
{
2023-11-15 23:54:54 +08:00
if (_detected == selected)
{
2023-08-23 01:59:40 +08:00
2023-11-15 23:54:54 +08:00
}
else
{
TryDeSelected();
Detected(_detected);
}
2023-08-23 01:59:40 +08:00
}
else
{
TryDeSelected();
}
}
else
{
TryDeSelected();
}
}
2023-11-15 23:54:54 +08:00
catch(MissingReferenceException e)
{}
catch (Exception e)
2023-08-23 01:59:40 +08:00
{
2023-11-15 23:54:54 +08:00
Console.WriteLine(e);
throw;
2023-08-23 01:59:40 +08:00
}
2023-11-15 23:54:54 +08:00
2023-08-23 01:59:40 +08:00
}
private void TryDeSelected()
{
if (selected is null) return;
selected.SetSelectionState(SelectionState.None);
2023-10-20 19:31:12 +08:00
OnInactive?.Invoke(selected);
2023-08-23 01:59:40 +08:00
selected = null;
}
private void Detected(ISelectable detected)
{
selected = detected;
detected.SetSelectionState(SelectionState.Hover);
2023-10-20 19:31:12 +08:00
OnSelected?.Invoke(selected);
2023-08-23 01:59:40 +08:00
}
2023-10-20 19:31:12 +08:00
2023-08-23 01:59:40 +08:00
public void Interactive(InputAction.CallbackContext context)
{
2023-10-20 19:31:12 +08:00
if (context.interaction is not PressInteraction || !context.performed ) return;
if (cd.AllowUpdate is false) return;
2023-08-23 01:59:40 +08:00
var _selected = selected;
if (_selected is not MonoBehaviour monoBehaviour) return;
if (monoBehaviour.TryGetComponentAny<IAction>(out var action))
{
action.Execute();
2023-11-15 23:54:54 +08:00
}
else
{
2023-08-23 01:59:40 +08:00
}
2023-10-20 19:31:12 +08:00
selected.SetSelectionState(SelectionState.Active);
OnActive?.Invoke(selected);
2023-08-23 01:59:40 +08:00
}
2023-10-20 19:31:12 +08:00
public bool TryGetCurrentSelectable(out ISelectable selectable)
{
selectable = selected;
2023-11-30 00:23:23 +08:00
return selected?.Transform;
2023-10-20 19:31:12 +08:00
}
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;
2023-08-23 01:59:40 +08:00
}
}