Health/Assets/Scripts/UI/GuideUI.cs

171 lines
4.9 KiB
C#
Raw Normal View History

2023-11-07 13:55:35 +00:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
2023-11-07 17:51:34 +00:00
using TMPro;
2023-11-07 13:55:35 +00:00
using UnityEngine;
2023-11-07 17:51:34 +00:00
using UnityEngine.UI;
2023-11-07 13:55:35 +00:00
public class GuideUI : UIPanelBase
{
private GameObject _guide;
2023-11-07 15:28:19 +00:00
private Animator _effectAnimator;
2023-11-07 17:51:34 +00:00
private Transform _content;
2023-11-07 13:55:35 +00:00
2023-11-14 10:52:43 +00:00
//<2F><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
private Image _progressBar;
private DateTime _startTime;
private float _totalSeconds;
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>
private TextMeshProUGUI _successText;
private TextMeshProUGUI _totalText;
private TextMeshProUGUI _kcalText;
2023-11-07 13:55:35 +00:00
public GudieAnimationManager GuideMgr => _guide.GetComponent<GudieAnimationManager>();
private void Awake()
{
_guide = GameObject.FindWithTag("Guide");
if (_guide == null)
{
2023-11-24 05:13:10 +00:00
LogPrint.Error("Guide is null");
2023-11-07 13:55:35 +00:00
}
2023-11-07 15:28:19 +00:00
_effectAnimator = transform.Find("Content").GetComponentInChildren<Animator>();
2023-11-07 17:51:34 +00:00
_content = transform.Find("Content");
2023-11-14 10:52:43 +00:00
_progressBar = _content.Find("Guide/ProgressBar/Background/ProgressBar").GetComponent<Image>();
var detailPanel = _content.Find("DetailPanel");
_successText = detailPanel.Find("SuccessCount").GetComponent<TextMeshProUGUI>();
_totalText = detailPanel.Find("TotalCount").GetComponent<TextMeshProUGUI>();
_kcalText = detailPanel.Find("KcalNum").GetComponent<TextMeshProUGUI>();
2023-11-07 13:55:35 +00:00
}
private void OnEnable()
{
2023-11-10 08:12:37 +00:00
EventManager.Instance.AddEventListener(YogaEventType.PlayAnimation, PlayAnimation);
EventManager.Instance.AddEventListener(YogaEventType.ActionSuccess, ShowSuccessEffect);
EventManager.Instance.AddEventListener(YogaEventType.UpdateProgress, UpdateSuccessCount);
EventManager.Instance.AddEventListener(YogaEventType.ActionFailed, ShowFailEffect);
2023-11-07 13:55:35 +00:00
}
private void OnDisable()
{
2023-11-10 08:12:37 +00:00
EventManager.Instance.RemoveEventListener(YogaEventType.PlayAnimation, PlayAnimation);
EventManager.Instance.RemoveEventListener(YogaEventType.ActionSuccess, ShowSuccessEffect);
EventManager.Instance.RemoveEventListener(YogaEventType.UpdateProgress, UpdateSuccessCount);
EventManager.Instance.RemoveEventListener(YogaEventType.ActionFailed, ShowFailEffect);
2023-11-07 13:55:35 +00:00
}
#region Event Func
private void PlayAnimation(params object[] args)
{
var animeType = args.FirstOrDefault();
if (animeType == null)
{
2023-11-24 05:13:10 +00:00
LogPrint.Error("PlayAnimation animeType is null");
2023-11-07 13:55:35 +00:00
return;
}
2023-11-10 07:17:23 +00:00
GuideMgr.Play(Enum.GetName(typeof(AvatarAction), animeType));
2023-11-07 13:55:35 +00:00
}
2023-11-07 17:51:34 +00:00
private void UpdateSuccessCount(params object[] args)
{
//args[0] = successCount args[1] = excutedCount args[2] = totalCount
if (args.Length < 3)
{
2023-11-24 05:13:10 +00:00
LogPrint.Error("UpdateSuccessCount args is not enough");
2023-11-07 17:51:34 +00:00
return;
}
var successCount = (int)args[0];
var excutedCount = (int)args[1];
2023-11-14 10:52:43 +00:00
if (_successText != null)
2023-11-07 17:51:34 +00:00
{
2023-11-14 10:52:43 +00:00
_successText.text = successCount.ToString();
2023-11-07 17:51:34 +00:00
}
2023-11-14 10:52:43 +00:00
if (_kcalText != null)
2023-11-07 17:51:34 +00:00
{
2023-11-14 10:52:43 +00:00
_kcalText.text = (successCount * 0.1f + (excutedCount - successCount) * 0.5f).ToString("F1");
2023-11-07 17:51:34 +00:00
}
}
2023-11-07 15:28:19 +00:00
private void ShowFailEffect()
{
2023-11-07 17:51:34 +00:00
_effectAnimator.Play("WindowEffectError", 0, 0f);//<2F>߿<EFBFBD><DFBF><EFBFBD>˸
2023-11-07 15:28:19 +00:00
}
private void ShowSuccessEffect()
{
2023-11-07 17:51:34 +00:00
_effectAnimator.Play("WindowEffectCorrect", 0, 0f);//<2F>߿<EFBFBD><DFBF><EFBFBD>˸
2023-11-07 15:28:19 +00:00
}
2023-11-07 13:55:35 +00:00
#endregion
public override void Init(object[] pageData)
{
base.Init(pageData);
if (_guide == null)
_guide = GameObject.FindWithTag("Guide");
2023-11-14 10:52:43 +00:00
if (pageData[0] == null)
{
2023-11-24 05:13:10 +00:00
LogPrint.Error("Guide is null");
2023-11-14 10:52:43 +00:00
return;
}
YogaData data = pageData[0] as YogaData;
_totalSeconds = data.TotalSeconds;
if (_totalSeconds == 0)
{
2023-11-24 05:13:10 +00:00
LogPrint.Error("TotalSeconds is 0");
2023-11-14 10:52:43 +00:00
return;
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>
if (_successText != null)
{
_successText.text = "0";
}
if (_totalText != null)
{
_totalText.text = data.MaxActionCount.ToString();
}
if (_kcalText != null)
{
_kcalText.text = "0";
}
}
//<2F><><EFBFBD>½<EFBFBD><C2BD><EFBFBD><EFBFBD><EFBFBD>
private void ProgressUpdate()
{
var passedTime = Math.Abs((_startTime - DateTime.Now).TotalSeconds);
_progressBar.fillAmount = (float)(passedTime / _totalSeconds);
2023-11-07 13:55:35 +00:00
}
public override void OnEnter()
{
base.OnEnter();
_guide.SetActive(true);
2023-11-10 08:12:37 +00:00
EventManager.Instance.Dispatch(YogaEventType.StartMotionCapture);
EventManager.Instance.Dispatch(YogaEventType.PlayAnimation, YogaManager.Instance.Action.Action);
2023-11-14 10:52:43 +00:00
_startTime = DateTime.Now;
InvokeRepeating("ProgressUpdate", 0, 0.05f); //20fps
2023-11-10 07:17:23 +00:00
}
public override void OnExit()
{
base.OnExit();
_guide.SetActive(false);
GuideMgr.Stop();
2023-11-10 08:12:37 +00:00
EventManager.Instance.Dispatch(YogaEventType.StopMotionCapture);
2023-11-07 13:55:35 +00:00
}
}