125 lines
3.5 KiB
C#
125 lines
3.5 KiB
C#
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
|
|
[DisallowMultipleComponent]
|
|
public class ItemProcessingWorkstationView : MonoBehaviour
|
|
{
|
|
[SerializeField] private NetworkItemProcessingWorkstation workstation;
|
|
|
|
[Header("State Events")]
|
|
[SerializeField] private UnityEvent onEmpty;
|
|
[SerializeField] private UnityEvent onProcessingStarted;
|
|
[SerializeField] private UnityEvent onOutputReady;
|
|
|
|
[Header("Continuous Progress")]
|
|
[SerializeField] private UnityEvent<float> onProcessingProgress;
|
|
|
|
private ItemProcessingWorkstationState previousState;
|
|
private bool hasAppliedState;
|
|
|
|
public NetworkItemProcessingWorkstation Workstation => workstation;
|
|
|
|
protected virtual void Reset() =>
|
|
workstation = GetComponentInParent<NetworkItemProcessingWorkstation>();
|
|
|
|
protected virtual void Awake()
|
|
{
|
|
if (workstation == null)
|
|
workstation = GetComponentInParent<NetworkItemProcessingWorkstation>();
|
|
}
|
|
|
|
protected virtual void OnEnable()
|
|
{
|
|
if (workstation != null)
|
|
{
|
|
workstation.Changed += HandleWorkstationChanged;
|
|
ApplyState();
|
|
}
|
|
}
|
|
|
|
protected virtual void Start() => ApplyState();
|
|
|
|
protected virtual void OnDisable()
|
|
{
|
|
if (workstation != null)
|
|
workstation.Changed -= HandleWorkstationChanged;
|
|
|
|
hasAppliedState = false;
|
|
}
|
|
|
|
protected virtual void Update()
|
|
{
|
|
if (workstation != null &&
|
|
workstation.State == ItemProcessingWorkstationState.Processing)
|
|
{
|
|
onProcessingProgress?.Invoke(workstation.ProcessProgress);
|
|
}
|
|
}
|
|
|
|
protected virtual void HandleStateApplied(ItemProcessingWorkstationState state)
|
|
{
|
|
}
|
|
|
|
[ContextMenu("Preview/Empty")]
|
|
private void PreviewEmpty()
|
|
{
|
|
onProcessingProgress?.Invoke(0f);
|
|
onEmpty?.Invoke();
|
|
HandleStateApplied(ItemProcessingWorkstationState.Empty);
|
|
}
|
|
|
|
[ContextMenu("Preview/Processing Started")]
|
|
private void PreviewProcessingStarted()
|
|
{
|
|
onProcessingProgress?.Invoke(0f);
|
|
onProcessingStarted?.Invoke();
|
|
HandleStateApplied(ItemProcessingWorkstationState.Processing);
|
|
}
|
|
|
|
[ContextMenu("Preview/Processing 50 Percent")]
|
|
private void PreviewProcessingHalfway() => onProcessingProgress?.Invoke(0.5f);
|
|
|
|
[ContextMenu("Preview/Output Ready")]
|
|
private void PreviewOutputReady()
|
|
{
|
|
onProcessingProgress?.Invoke(1f);
|
|
onOutputReady?.Invoke();
|
|
HandleStateApplied(ItemProcessingWorkstationState.OutputReady);
|
|
}
|
|
|
|
private void HandleWorkstationChanged() => ApplyState();
|
|
|
|
private void ApplyState()
|
|
{
|
|
if (workstation == null || !workstation.IsSpawned)
|
|
return;
|
|
|
|
ItemProcessingWorkstationState currentState = workstation.State;
|
|
if (hasAppliedState && currentState == previousState)
|
|
return;
|
|
|
|
previousState = currentState;
|
|
hasAppliedState = true;
|
|
|
|
switch (currentState)
|
|
{
|
|
case ItemProcessingWorkstationState.Processing:
|
|
onProcessingStarted?.Invoke();
|
|
onProcessingProgress?.Invoke(workstation.ProcessProgress);
|
|
break;
|
|
|
|
case ItemProcessingWorkstationState.OutputReady:
|
|
onProcessingProgress?.Invoke(1f);
|
|
onOutputReady?.Invoke();
|
|
break;
|
|
|
|
default:
|
|
onProcessingProgress?.Invoke(0f);
|
|
onEmpty?.Invoke();
|
|
break;
|
|
}
|
|
|
|
HandleStateApplied(currentState);
|
|
}
|
|
}
|