105 lines
3.3 KiB
C#
105 lines
3.3 KiB
C#
![]() |
using System.Threading.Tasks;
|
||
|
using UnityEngine;
|
||
|
using UnityEngine.InputSystem;
|
||
|
|
||
|
namespace DDD
|
||
|
{
|
||
|
public class RestaurantPlayerInteraction : RestaurantCharacterInteraction
|
||
|
{
|
||
|
private RestaurantPlayerDataSo _restaurantPlayerDataSo;
|
||
|
|
||
|
protected override void Start()
|
||
|
{
|
||
|
base.Start();
|
||
|
|
||
|
_ = Initialize();
|
||
|
}
|
||
|
|
||
|
private async Task Initialize()
|
||
|
{
|
||
|
_restaurantPlayerDataSo = await AssetManager.LoadAsset<RestaurantPlayerDataSo>(DataConstants.RestaurantPlayerDataSo);
|
||
|
Debug.Assert(_restaurantPlayerDataSo != null, "_restaurantPlayerDataSo is null");
|
||
|
|
||
|
_restaurantPlayerDataSo.InteractAction = InputManager.Instance.GetAction(InputActionMaps.Restaurant, nameof(RestaurantActions.Interact));
|
||
|
_restaurantPlayerDataSo.InteractAction.performed += OnInteractPerformed;
|
||
|
_restaurantPlayerDataSo.InteractAction.canceled += OnInteractCanceled;
|
||
|
|
||
|
_interactionRadius = _restaurantPlayerDataSo.InteractionRadius;
|
||
|
_interactionLayerMask = _restaurantPlayerDataSo.InteractionLayerMask;
|
||
|
|
||
|
EventBus.Register<RestaurantInteractionEvent>(this);
|
||
|
}
|
||
|
|
||
|
protected override void OnDestroy()
|
||
|
{
|
||
|
base.OnDestroy();
|
||
|
|
||
|
if (_restaurantPlayerDataSo != null)
|
||
|
{
|
||
|
_restaurantPlayerDataSo.InteractAction.performed -= OnInteractPerformed;
|
||
|
_restaurantPlayerDataSo.InteractAction.canceled -= OnInteractCanceled;
|
||
|
}
|
||
|
|
||
|
EventBus.Unregister<RestaurantInteractionEvent>(this);
|
||
|
}
|
||
|
|
||
|
private void OnInteractPerformed(InputAction.CallbackContext context)
|
||
|
{
|
||
|
if (_nearestInteractable == null || !_nearestInteractable.CanInteract()) return;
|
||
|
|
||
|
float requiredHoldTime = _nearestInteractable.GetRequiredHoldTime();
|
||
|
|
||
|
if (requiredHoldTime <= 0f)
|
||
|
{
|
||
|
_nearestInteractable.OnInteracted(this);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
_isInteracting = true;
|
||
|
_interactHeldTime = 0f;
|
||
|
_interactingTarget = _nearestInteractable;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void OnInteractCanceled(InputAction.CallbackContext context)
|
||
|
{
|
||
|
ResetInteractionState();
|
||
|
}
|
||
|
|
||
|
protected override void OnNearestInteractableChanged(IInteractable newTarget)
|
||
|
{
|
||
|
if (newTarget != null && newTarget.CanInteract())
|
||
|
{
|
||
|
BroadcastShowUi(newTarget, 0f);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
if (_isInteracting == false)
|
||
|
{
|
||
|
EventBus.Broadcast(GameEvents.HideInteractionUiEvent);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
protected override void OnInteractionHoldProgress(float ratio)
|
||
|
{
|
||
|
if (_interactingTarget != null)
|
||
|
{
|
||
|
BroadcastShowUi(_interactingTarget, ratio);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
protected override void OnInteractionCompleted()
|
||
|
{
|
||
|
|
||
|
}
|
||
|
|
||
|
private void BroadcastShowUi(IInteractable interactable, float ratio)
|
||
|
{
|
||
|
var evt = GameEvents.ShowInteractionUiEvent;
|
||
|
evt.TextKey = interactable.GetInteractionMessageKey();
|
||
|
evt.HoldProgress = ratio;
|
||
|
EventBus.Broadcast(evt);
|
||
|
}
|
||
|
}
|
||
|
}
|