ProjectDDD/Assets/_DDD/_Scripts/RestaurantCharacter/Player/RestaurantPlayerInteraction.cs
2025-08-07 17:51:36 +09:00

103 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)
{
OnInteractionHoldProgress(0f);
ResetInteractionState();
}
protected override void OnNearestInteractableChanged(IInteractable newTarget)
{
if (newTarget != null && newTarget.CanInteract())
{
BroadcastShowUi(newTarget, 0f);
}
else
{
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);
}
}
}