Update
This commit is contained in:
75
Assets/Scripts/Player Controller/PlayerInteraction.cs
Normal file
75
Assets/Scripts/Player Controller/PlayerInteraction.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
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];
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user