ProjectDDD/Assets/_DDD/_Scripts/Restaurant/Ui/OrderUi/CustomerPatienceUiComponent.cs

112 lines
4.7 KiB
C#

using System.Collections.Generic;
using Sirenix.OdinInspector;
using Sirenix.Serialization;
using UnityEngine;
using UnityEngine.UI;
namespace DDD.Restaurant
{
public class CustomerPatienceUiComponent : SerializedMonoBehaviour
{
private IAISharedBlackboard<RestaurantCustomerBlackboardKey> _blackboard;
[OdinSerialize] private HashSet<RestaurantOrderType> _targetOrderType = new();
[SerializeField] private RestaurantOrderType _currentOrderType;
[SerializeField] private RestaurantOrderType _prevOrderType;
private bool _initialized;
private IInteractionSubsystemObject<RestaurantOrderType> _subsystem;
[SerializeField] private Slider _patienceSlider;
private void Start()
{
var canvas = GetComponentInChildren<Canvas>();
canvas.worldCamera = Camera.main;
_patienceSlider = GetComponentInChildren<Slider>();
if (_patienceSlider == null)
{
Debug.LogWarning($"[{GetType().Name}] 슬라이더가 존재하지 않음 오브젝트 해시코드: {gameObject.GetHashCode()}");
return;
}
if (!TryGetComponent(out _blackboard))
{
Debug.LogWarning($"[{GetType().Name}] 블랙보드가 존재하지 않음 오브젝트 해시코드: {gameObject.GetHashCode()}");
return;
}
var targetObject = _blackboard.GetBlackboardValue<GameObject>(RestaurantCustomerBlackboardKey.CurrentTargetGameObject);
if (targetObject == null)
{
Debug.LogWarning($"[{GetType().Name}] 타겟 오브젝트가 존재하지 않음 오브젝트 해시코드: {gameObject.GetHashCode()}");
return;
}
if (!targetObject.TryGetComponent<IInteractionSubsystemOwner>(out var subsystemOwner))
{
Debug.LogWarning($"[{GetType().Name}] 서브시스템 오너가 존재하지 않음 오브젝트 해시코드: {gameObject.GetHashCode()}");
return;
}
subsystemOwner.TryGetSubsystemObject(out _subsystem);
if (_subsystem == null)
{
Debug.LogWarning($"[{GetType().Name}] RestaurantOrderType 서브시스템이 존재하지 않음 오브젝트 해시코드: {gameObject.GetHashCode()}");
return;
}
_currentOrderType = _subsystem.GetInteractionSubsystemType();
_initialized = true;
_patienceSlider.gameObject.SetActive(false);
}
private void Update()
{
if (!_initialized) return;
_currentOrderType = _subsystem.GetInteractionSubsystemType();
if (!_targetOrderType.Contains(_currentOrderType))
{
_patienceSlider.gameObject.SetActive(false);
return;
}
if (_currentOrderType != _prevOrderType)
{
SetPatience();
}
if (_currentOrderType == RestaurantOrderType.Busy)
{
_patienceSlider.maxValue = _blackboard.GetBlackboardValue<float>(RestaurantCustomerBlackboardKey.MaxPatienceTime);
var remainingTime = _blackboard.GetBlackboardValue<float>(RestaurantCustomerBlackboardKey.RemainingPatienceTime);
_patienceSlider.value = _patienceSlider.maxValue - remainingTime;
if (_patienceSlider.value >= _patienceSlider.maxValue)
{
_patienceSlider.gameObject.SetActive(false);
}
}
else
{
_patienceSlider.maxValue = _blackboard.GetBlackboardValue<float>(RestaurantCustomerBlackboardKey.MaxPatienceTime);
_patienceSlider.value = _blackboard.GetBlackboardValue<float>(RestaurantCustomerBlackboardKey.RemainingPatienceTime);
if (_patienceSlider.value <= 0)
{
_patienceSlider.gameObject.SetActive(false);
}
}
}
private void SetPatience()
{
_prevOrderType = _currentOrderType;
_patienceSlider.maxValue = _blackboard.GetBlackboardValue<float>(RestaurantCustomerBlackboardKey.MaxPatienceTime);
_patienceSlider.value = _patienceSlider.maxValue;
ColorBlock colors = _patienceSlider.colors;
colors.normalColor = _currentOrderType == RestaurantOrderType.Busy ? Color.green : Color.red;
_patienceSlider.colors = colors;
_patienceSlider.gameObject.SetActive(true);
}
}
}