89 lines
2.5 KiB
C#
89 lines
2.5 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 Collider triggerCollider;
|
|
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;
|
|
ResolveTriggerCollider();
|
|
triggerCollider.isTrigger = true;
|
|
}
|
|
|
|
private void Awake() => ResolveTriggerCollider();
|
|
|
|
private void OnValidate()
|
|
{
|
|
ResolveTriggerCollider();
|
|
triggerCollider.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);
|
|
}
|
|
|
|
public bool OverlapsAny(Collider[] itemColliders, float clearance)
|
|
{
|
|
ResolveTriggerCollider();
|
|
if (triggerCollider == null || !triggerCollider.enabled || itemColliders == null)
|
|
return false;
|
|
|
|
Bounds triggerBounds = triggerCollider.bounds;
|
|
triggerBounds.Expand(Mathf.Max(0f, clearance) * 2f);
|
|
foreach (Collider itemCollider in itemColliders)
|
|
{
|
|
if (itemCollider != null && itemCollider.enabled &&
|
|
triggerBounds.Intersects(itemCollider.bounds))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
private void ResolveTriggerCollider()
|
|
{
|
|
if (triggerCollider == null)
|
|
triggerCollider = GetComponent<Collider>();
|
|
}
|
|
}
|