1
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
using UnityEngine.InputSystem.Interactions;
|
||||
using BITKit;
|
||||
namespace BITKit.Entities
|
||||
{
|
||||
public class EntityInteractive : EntityInputComponent
|
||||
{
|
||||
[Header(Constant.Header.Settings)]
|
||||
public float distance;
|
||||
public LayerMask layerMask;
|
||||
[Header(Constant.Header.Input)]
|
||||
public InputActionReference interactiveAction;
|
||||
InputActionGroup inputActionGroup = new();
|
||||
[Header(Constant.Header.InternalVariables)]
|
||||
ISelectable selected;
|
||||
IntervalUpdate cd = new(0.08f);
|
||||
public override void OnStart()
|
||||
{
|
||||
base.OnStart();
|
||||
}
|
||||
public override void OnSpawn()
|
||||
{
|
||||
base.OnSpawn();
|
||||
if (isLocalPlayer)
|
||||
{
|
||||
inputActionGroup.RegisterCallback(interactiveAction, OnInteractive);
|
||||
}
|
||||
}
|
||||
public override void OnDespawn()
|
||||
{
|
||||
base.OnDespawn();
|
||||
if (isLocalPlayer)
|
||||
{
|
||||
inputActionGroup.UnRegisterCallback(interactiveAction, OnInteractive);
|
||||
}
|
||||
}
|
||||
public override void OnUpdate(float deltaTime)
|
||||
{
|
||||
if (isLocalPlayer)
|
||||
{
|
||||
var ray = Camera.main.ScreenPointToRay(Mouse.current.position.ReadValue());
|
||||
if (Physics.Raycast(ray, out var raycastHit, distance, layerMask))
|
||||
{
|
||||
if (raycastHit.transform.TryGetComponentAny<ISelectable>(out var _detected))
|
||||
{
|
||||
if (_detected == selected)
|
||||
{
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
TryDeSelected();
|
||||
|
||||
Detected(_detected);
|
||||
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
TryDeSelected();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
TryDeSelected();
|
||||
}
|
||||
}
|
||||
}
|
||||
void TryDeSelected()
|
||||
{
|
||||
if (selected is not null)
|
||||
{
|
||||
selected.SetSelectionState(SelectionState.None);
|
||||
foreach (var x in entity.GetCallbacks<ISelectableCallback>())
|
||||
{
|
||||
x.OnInactive(selected);
|
||||
}
|
||||
selected = null;
|
||||
}
|
||||
}
|
||||
void Detected(ISelectable detected)
|
||||
{
|
||||
selected = detected;
|
||||
detected.SetSelectionState(SelectionState.Hover);
|
||||
foreach (var x in entity.GetCallbacks<ISelectableCallback>())
|
||||
{
|
||||
x.OnHover(selected);
|
||||
}
|
||||
}
|
||||
public override void OnInteractive(InputAction.CallbackContext context)
|
||||
{
|
||||
if (context.interaction is TapInteraction && context.performed)
|
||||
{
|
||||
var selected = this.selected;
|
||||
if (selected is not null)
|
||||
{
|
||||
|
||||
if (selected is MonoBehaviour monoBehaviour)
|
||||
{
|
||||
if (monoBehaviour.TryGetComponentAny<IAction>(out var action))
|
||||
{
|
||||
action.Excute();
|
||||
}
|
||||
foreach (var x in entity.GetCallbacks<ISelectableCallback>())
|
||||
{
|
||||
x.OnActive(selected);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8de7270cf1d43f64fafdc3413158cea3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user