55 lines
1.6 KiB
C#
55 lines
1.6 KiB
C#
using Unity.Netcode;
|
|
using UnityEngine;
|
|
|
|
[DisallowMultipleComponent]
|
|
[RequireComponent(typeof(Collider))]
|
|
public sealed class RocketAssemblyInputSlot : MonoBehaviour, IInteractable
|
|
{
|
|
[SerializeField] private Transform snapPoint;
|
|
|
|
private NetworkRocketAssemblyWorkstation workstation;
|
|
private int slotIndex = -1;
|
|
|
|
public Transform SnapPoint => snapPoint != null ? snapPoint : transform;
|
|
public Vector3 InteractionPosition => SnapPoint.position;
|
|
public string InteractionPrompt => workstation != null
|
|
? workstation.GetTakePrompt(slotIndex)
|
|
: "Take part";
|
|
public bool IsInteractionAvailable =>
|
|
workstation != null && workstation.CanTakeSlotItem(slotIndex);
|
|
|
|
private void Reset()
|
|
{
|
|
snapPoint = transform;
|
|
GetComponent<Collider>().isTrigger = true;
|
|
}
|
|
|
|
private void OnValidate() => GetComponent<Collider>().isTrigger = true;
|
|
|
|
public void Configure(NetworkRocketAssemblyWorkstation owner, int index)
|
|
{
|
|
workstation = owner;
|
|
slotIndex = index;
|
|
}
|
|
|
|
public void RequestInteraction()
|
|
{
|
|
if (IsInteractionAvailable)
|
|
workstation.RequestTakeSlotItem(slotIndex);
|
|
}
|
|
|
|
private void OnTriggerEnter(Collider other) => TryInsert(other);
|
|
|
|
private void OnTriggerStay(Collider other) => TryInsert(other);
|
|
|
|
private void TryInsert(Collider other)
|
|
{
|
|
if (workstation == null || !workstation.IsServer || !workstation.IsSpawned)
|
|
return;
|
|
|
|
NetworkItem item = other.GetComponentInParent<NetworkItem>();
|
|
if (item != null)
|
|
workstation.TryInsertItem(slotIndex, item);
|
|
}
|
|
}
|