using System.Collections.Generic; using UnityEngine; [DisallowMultipleComponent] [RequireComponent(typeof(Collider))] public sealed class NetworkItemInputSlot : MonoBehaviour { [SerializeField] private NetworkItemProcessingWorkstation workstation; private readonly Dictionary> overlaps = new(); private void Reset() { workstation = GetComponentInParent(); GetComponent().isTrigger = true; } private void Awake() { if (workstation == null) workstation = GetComponentInParent(); } 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(); if (item == null || !overlaps.TryGetValue(item, out HashSet 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(); if (item != null) workstation.TryAcceptItem(item); } private void RegisterOverlap(Collider other) { NetworkItem item = other.GetComponentInParent(); if (item == null) return; if (!overlaps.TryGetValue(item, out HashSet colliders)) { colliders = new HashSet(); overlaps.Add(item, colliders); } colliders.Add(other); } }