CapersProject/Assets/02.Scripts/Ui/Tycoon/Bill.cs

250 lines
8.4 KiB
C#
Raw Normal View History

2024-10-08 13:44:20 +00:00
using System;
using System.Collections;
using BlueWater.Npcs.Customers;
using DG.Tweening;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using Random = UnityEngine.Random;
namespace BlueWater
{
public class Bill : MonoBehaviour
{
[SerializeField]
private RectTransform _rect;
[SerializeField]
private AnimationController _animationController;
[SerializeField]
private Slider _slider;
[SerializeField]
private Image _filledImage;
[SerializeField]
private Image _orderImage;
2024-10-14 11:13:08 +00:00
[SerializeField]
private GameObject _makingCocktailPivotObject;
2024-10-20 17:21:39 +00:00
[SerializeField]
private GameObject _checkImageObject;
2024-10-08 13:44:20 +00:00
2024-10-14 11:13:08 +00:00
[SerializeField]
private GameObject _stampImageObject;
2024-10-08 13:44:20 +00:00
[SerializeField]
private GameObject _tableNumberImageObject;
[SerializeField]
private TMP_Text _tableNumberText;
[SerializeField]
private Color _startColor = Color.green;
2024-11-25 12:36:48 +00:00
[SerializeField]
private Color _middleColor = Color.yellow;
2024-10-08 13:44:20 +00:00
[SerializeField]
private Color _endColor = Color.red;
2024-10-10 02:37:44 +00:00
public BillInfo CurrentBillInfo { get; private set; }
2024-10-14 11:13:08 +00:00
private Tween _sliderTween;
2024-10-28 11:42:22 +00:00
private Sequence _moveSequence;
private Sequence _arrivedSequence;
2024-10-10 02:37:44 +00:00
private Vector3 _spawnPosition;
2024-11-25 12:36:48 +00:00
private Color _previousColor;
2024-10-10 02:37:44 +00:00
private float _maxDistance;
2024-11-07 09:13:54 +00:00
private Action _removeEvent;
2024-11-25 12:36:48 +00:00
private void OnDestroy()
{
_slider.onValueChanged.RemoveListener(OnSliderValueChanged);
2024-11-28 23:07:50 +00:00
_sliderTween?.Kill();
_moveSequence?.Kill();
_arrivedSequence?.Kill();
2024-11-25 12:36:48 +00:00
}
2024-10-08 13:44:20 +00:00
2024-11-07 09:13:54 +00:00
public void Initialize(Customer customer, Vector3 spawnPosition, Vector3 billInfoPosition0, Action removeEvent)
2024-10-08 13:44:20 +00:00
{
2024-11-07 09:13:54 +00:00
_removeEvent = removeEvent;
2024-10-08 13:44:20 +00:00
_slider.onValueChanged.AddListener(OnSliderValueChanged);
2024-10-10 02:37:44 +00:00
_spawnPosition = spawnPosition;
_rect.anchoredPosition = _spawnPosition;
_maxDistance = Vector3.Distance(_rect.anchoredPosition, billInfoPosition0);
2024-10-08 13:44:20 +00:00
_slider.value = 1f;
2024-10-14 11:13:08 +00:00
_slider.gameObject.SetActive(true);
2024-10-08 13:44:20 +00:00
_orderImage.sprite = customer.OrderedCocktailData.Sprite;
2024-10-14 11:13:08 +00:00
_orderImage.gameObject.SetActive(true);
2024-10-10 08:42:20 +00:00
_makingCocktailPivotObject.SetActive(false);
2024-10-20 17:21:39 +00:00
_checkImageObject.SetActive(false);
2024-10-14 11:13:08 +00:00
_stampImageObject.SetActive(false);
_tableNumberText.text = customer.CurrentTableSeat.TableNumber.ToString();
_tableNumberImageObject.SetActive(true);
2024-10-15 18:01:16 +00:00
2024-11-28 23:07:50 +00:00
SetTween(customer.CurrentLevelData.WaitTime, customer.HurryTime);
2024-10-15 18:01:16 +00:00
}
public void SetTween(int waitTime, int hurryTime)
{
2024-11-25 12:36:48 +00:00
_sliderTween = _slider.DOValue(0f, hurryTime)
.From(1f)
2024-10-08 13:44:20 +00:00
.SetEase(Ease.Linear)
2024-10-15 18:01:16 +00:00
.SetDelay(waitTime)
2024-10-14 11:13:08 +00:00
.SetAutoKill(true);
2024-10-08 13:44:20 +00:00
}
private void OnSliderValueChanged(float value)
{
2024-11-25 12:36:48 +00:00
if (value > 0.6f)
{
if (_previousColor != _startColor)
{
_filledImage.color = _startColor;
_previousColor = _startColor;
}
}
else if (value > 0.3f)
{
if (_previousColor != _middleColor)
{
_filledImage.color = _middleColor;
_previousColor = _middleColor;
}
}
else
{
if (_previousColor != _endColor)
{
_filledImage.color = _endColor;
_previousColor = _endColor;
}
}
2024-10-08 13:44:20 +00:00
}
public void Move(BillInfo billInfo)
{
2024-11-28 23:07:50 +00:00
if (!this || _rect == null || !gameObject.activeInHierarchy) return;
2024-10-10 02:37:44 +00:00
if (CurrentBillInfo != null)
2024-10-08 13:44:20 +00:00
{
2024-10-10 02:37:44 +00:00
CurrentBillInfo.IsEmpty = true;
2024-10-08 13:44:20 +00:00
}
2024-10-10 02:37:44 +00:00
CurrentBillInfo = billInfo;
CurrentBillInfo.IsEmpty = false;
2024-10-10 05:44:37 +00:00
CurrentBillInfo.IsMoving = true;
2024-10-10 02:37:44 +00:00
var distance = Vector3.Distance(_rect.anchoredPosition, CurrentBillInfo.Position);
var moveTime = Mathf.Lerp(0.2f, 1f, distance / _maxDistance);
var rotationAngle = Mathf.Lerp(8f, 40f, distance / _maxDistance);
var punchStrength = Mathf.Lerp(1f, 5f, distance / _maxDistance);
2024-11-28 23:07:50 +00:00
_moveSequence = DOTween.Sequence()
.Append(_rect?.DOLocalMoveX(CurrentBillInfo.Position.x, moveTime))
.Join(_rect?.DOLocalRotate(new Vector3(0, 0, rotationAngle), moveTime / 5f))
.Join(_rect?.DOPunchRotation(new Vector3(0f, 0f, punchStrength), moveTime * 4f / 5f, 3, 0.2f)
.SetDelay(0.2f)
.SetEase(Ease.InOutBounce))
.AppendCallback(() =>
{
if (!this || _rect == null) return;
OnArrivedTarget();
})
.SetAutoKill(true);
2024-10-08 13:44:20 +00:00
}
private void OnArrivedTarget()
{
2024-11-28 23:07:50 +00:00
if (CurrentBillInfo == null) return;
2024-10-27 09:44:22 +00:00
2024-10-10 05:44:37 +00:00
CurrentBillInfo.IsMoving = false;
2024-10-08 13:44:20 +00:00
var randomZ = Random.Range(-15f, 15f);
2024-11-28 23:07:50 +00:00
_arrivedSequence = DOTween.Sequence()
.Append(_rect?.DOLocalRotate(Vector3.zero, 0.2f).SetEase(Ease.InQuad))
.Append(_rect?.DOPunchRotation(new Vector3(0f, 0f, -20f), 1f, 5, 0.5f).SetEase(Ease.InOutBounce))
.Append(_rect?.DOLocalRotate(new Vector3(0f, 0f, randomZ), 0.2f).SetEase(Ease.InQuad))
.SetAutoKill(true);
2024-10-08 13:44:20 +00:00
}
2024-11-07 09:13:54 +00:00
public void OrderResult(bool isSucceed)
2024-10-08 13:44:20 +00:00
{
2024-11-07 09:13:54 +00:00
StartCoroutine(WaitForAnimation(isSucceed));
2024-10-08 13:44:20 +00:00
}
2024-11-07 09:13:54 +00:00
private IEnumerator WaitForAnimation(bool isSucceed)
2024-10-08 13:44:20 +00:00
{
_animationController.SetAnimationParameter("isOrderedSucceed", isSucceed);
_animationController.SetAnimationParameter("isAnimationTrigger", true);
var animationName = isSucceed ? "OrderedSucceed" : "OrderedFail";
var animationStarted = false;
yield return StartCoroutine(_animationController.WaitForAnimationToRun(animationName,
success => animationStarted = success));
2024-10-10 02:37:44 +00:00
2024-10-08 13:44:20 +00:00
if (!animationStarted)
{
Debug.LogError("애니메이션 오류");
}
2024-10-14 11:13:08 +00:00
_stampImageObject.SetActive(true);
2024-10-28 11:42:22 +00:00
_sliderTween?.Kill();
2024-10-14 11:13:08 +00:00
var isTrigger = false;
2024-10-10 02:37:44 +00:00
while (_animationController.IsComparingCurrentAnimation(animationName) &&
_animationController.GetCurrentAnimationNormalizedTime() < 1f)
{
2024-10-14 11:13:08 +00:00
if (!isTrigger && _animationController.GetCurrentAnimationNormalizedTime() >= 0.5f)
{
_slider.gameObject.SetActive(false);
_makingCocktailPivotObject.SetActive(false);
2024-10-20 17:21:39 +00:00
_checkImageObject.gameObject.SetActive(false);
2024-10-14 11:13:08 +00:00
_orderImage.gameObject.SetActive(false);
_stampImageObject.SetActive(false);
_tableNumberImageObject.SetActive(false);
isTrigger = true;
}
2024-10-10 02:37:44 +00:00
yield return null;
}
2024-11-07 09:13:54 +00:00
Destroy();
2024-10-08 13:44:20 +00:00
}
2024-10-20 17:21:39 +00:00
public void BartenderMakingCocktail()
{
2024-10-28 11:42:22 +00:00
_makingCocktailPivotObject.gameObject.SetActive(true);
2024-10-20 17:21:39 +00:00
}
public void BartenderCompleteMakingCocktail()
{
2024-10-28 11:42:22 +00:00
_makingCocktailPivotObject.gameObject.SetActive(false);
_checkImageObject.gameObject.SetActive(true);
_animationController.SetAnimationParameter("isBartenderChecked", true);
2024-10-20 17:21:39 +00:00
}
2024-10-28 04:52:11 +00:00
public void ResetGauge()
{
if (_sliderTween == null)
{
2024-10-31 05:41:13 +00:00
Debug.LogError("Bill 게이지 리셋 오류");
2024-10-28 04:52:11 +00:00
return;
}
_sliderTween.Restart();
}
2024-11-07 09:13:54 +00:00
public void Destroy()
{
2024-11-28 23:07:50 +00:00
_sliderTween?.Kill();
_moveSequence?.Kill();
_arrivedSequence?.Kill();
CurrentBillInfo.ResetData();
2024-11-07 09:13:54 +00:00
_removeEvent?.Invoke();
Destroy(gameObject);
}
2024-10-08 13:44:20 +00:00
}
2024-10-10 02:37:44 +00:00
}