ok
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
[DisallowMultipleComponent]
|
||||
[RequireComponent(typeof(Collider))]
|
||||
public sealed class NetworkItemInputSlot : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private NetworkItemProcessingWorkstation workstation;
|
||||
|
||||
private readonly Dictionary<NetworkItem, HashSet<Collider>> overlaps = new();
|
||||
|
||||
private void Reset()
|
||||
{
|
||||
workstation = GetComponentInParent<NetworkItemProcessingWorkstation>();
|
||||
GetComponent<Collider>().isTrigger = true;
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (workstation == null)
|
||||
workstation = GetComponentInParent<NetworkItemProcessingWorkstation>();
|
||||
}
|
||||
|
||||
private void OnDisable() => overlaps.Clear();
|
||||
|
||||
private void OnTriggerEnter(Collider other)
|
||||
{
|
||||
RegisterOverlap(other);
|
||||
TryAccept(other);
|
||||
}
|
||||
|
||||
private void OnTriggerStay(Collider other)
|
||||
{
|
||||
RegisterOverlap(other);
|
||||
TryAccept(other);
|
||||
}
|
||||
|
||||
private void OnTriggerExit(Collider other)
|
||||
{
|
||||
if (workstation == null || !workstation.IsServer)
|
||||
return;
|
||||
|
||||
NetworkItem item = other.GetComponentInParent<NetworkItem>();
|
||||
if (item == null || !overlaps.TryGetValue(item, out HashSet<Collider> colliders))
|
||||
return;
|
||||
|
||||
colliders.Remove(other);
|
||||
if (colliders.Count > 0)
|
||||
return;
|
||||
|
||||
overlaps.Remove(item);
|
||||
workstation.NotifyItemExited(item);
|
||||
}
|
||||
|
||||
private void TryAccept(Collider other)
|
||||
{
|
||||
if (workstation == null || !workstation.IsServer || !workstation.IsSpawned)
|
||||
return;
|
||||
|
||||
NetworkItem item = other.GetComponentInParent<NetworkItem>();
|
||||
if (item != null)
|
||||
workstation.TryAcceptItem(item);
|
||||
}
|
||||
|
||||
private void RegisterOverlap(Collider other)
|
||||
{
|
||||
NetworkItem item = other.GetComponentInParent<NetworkItem>();
|
||||
if (item == null)
|
||||
return;
|
||||
|
||||
if (!overlaps.TryGetValue(item, out HashSet<Collider> colliders))
|
||||
{
|
||||
colliders = new HashSet<Collider>();
|
||||
overlaps.Add(item, colliders);
|
||||
}
|
||||
|
||||
colliders.Add(other);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user