Files
BABA_YAGA/Assets/Scripts/Player Controller/PlayerInteraction.cs

76 lines
2.5 KiB
C#
Raw Normal View History

2026-04-28 00:07:42 +07:00
using UnityEngine;
using Fusion;
using System.Collections.Generic;
using System;
namespace OnlyScove.Scripts
{
public class PlayerInteraction : NetworkBehaviour
{
[Header("Interaction Settings")]
[SerializeField] public float InteractionRange = 2f;
[SerializeField] public LayerMask InteractionMask;
public event Action<IInteractable> OnInteractableTargetChanged;
private List<IInteractable> interactablesNearby = new List<IInteractable>();
private int currentInteractableIndex = 0;
private EnvironmentScanner scanner;
public void Initialize(EnvironmentScanner scanner)
{
this.scanner = scanner;
}
public void UpdateInteractables()
{
if (scanner == null) return;
interactablesNearby.Clear();
IInteractable target = scanner.ScanForInteractable(InteractionRange, InteractionMask);
if (target != null) interactablesNearby.Add(target);
OnInteractableTargetChanged?.Invoke(target);
if (Object != null && Object.HasInputAuthority)
{
// UI Placeholder: Interaction UI
// Example: UI.UIEventBus.TriggerInteractionPrompt(target?.InteractionPrompt);
// Example: UI.UIEventBus.TriggerInteractionVisibility(target != null);
}
currentInteractableIndex = 0;
}
public void NextInteract()
{
if (interactablesNearby.Count <= 1) return;
currentInteractableIndex = (currentInteractableIndex + 1) % interactablesNearby.Count;
NotifyTargetChanged();
}
public void PreviousInteract()
{
if (interactablesNearby.Count <= 1) return;
currentInteractableIndex = (currentInteractableIndex - 1 + interactablesNearby.Count) % interactablesNearby.Count;
NotifyTargetChanged();
}
private void NotifyTargetChanged()
{
IInteractable target = GetInteractable();
OnInteractableTargetChanged?.Invoke(target);
if (Object != null && Object.HasInputAuthority)
{
// UI Placeholder: Update Prompt
// Example: UI.UIEventBus.TriggerInteractionPrompt(target?.InteractionPrompt);
}
}
public IInteractable GetInteractable()
{
return (interactablesNearby == null || interactablesNearby.Count == 0) ? null : interactablesNearby[currentInteractableIndex];
}
}
}