Files
back_cantanBuilding/Assets/Scripts/UI/Home/HomeBtnChallenge.cs
2026-05-26 16:15:54 +08:00

291 lines
10 KiB
C#

using asap.core;
using DG.Tweening;
using game;
using GameCore;
using System;
using System.Collections;
using PlayFab.Internal;
using TMPro;
using UniRx;
using UnityEngine;
using UnityEngine.PlayerLoop;
using UnityEngine.UI;
public class FishingChallengeAddEvent { }
public class HomeBtnChallenge : EventButtonResource
{
// 管理器
private FishingChallengeManager _fishingChallengeManager;
// UIElement;
private Transform _bubbleTips;
private TMP_Text _textTips;
private TMP_Text _textTime;
// private Transform _bgText;
// 剩余人数
private TMP_Text _textNum;
private Transform _barBg;
private Image _bar;
//
private Button _btnEnter;
private GameObject _redpoint;
private Image _iconImage;
// 数据
private int _challengeStep;
private int _challengeUIStep;
private int _challengeState;
//
private Timer _timer; // 定时器
private CompositeDisposable _disposables = new();
private readonly float _fillTime = 0.4f;//进度条充满时间
private void Awake()
{
Log("Awake-> ");
_fishingChallengeManager = GContext.container.Resolve<FishingChallengeManager>();
_btnEnter = GetComponent<Button>();
_redpoint = transform.Find("redpoint").gameObject;
_bubbleTips = transform.Find("bubble_tips");
_textTips = transform.Find("bubble_tips/text_tips").GetComponent<TMP_Text>();
_barBg = transform.Find("bar_bg");
// _bgText = transform.Find("bg_text");
_iconImage = transform.Find("icon").GetComponent<Image>();
_textNum = transform.Find("text_num").GetComponent<TMP_Text>();
_textTime = transform.Find("text_time").GetComponent<TMP_Text>();
GContext.OnEvent<HideHomePanelEvent>().Subscribe(OnHideHomePanelEvent).AddTo(_disposables);
GContext.OnEvent<FishingChallengeAddEvent>().Subscribe(ChallengePlayAni).AddTo(_disposables);
_bar = transform.Find("bar").GetComponent<Image>();
}
private void OnHideHomePanelEvent(HideHomePanelEvent e)
{
Destroy(this);
}
private void OnEnable()
{
Log("OnEnable -> ");
_fishingChallengeManager.CheckInit();
if (!_fishingChallengeManager.ShouldOpen())
{
gameObject.SetActive(false);
return;
}
_challengeUIStep = _fishingChallengeManager.fishingChallengeData.uiStep;
_challengeStep = _fishingChallengeManager.GetNewStep(); // 当前界面仅仅计算,不更新到 Manager
_challengeState = _fishingChallengeManager.ChallengeState();
//更新界面
_redpoint.SetActive(_fishingChallengeManager.IsFirstOpen);
InitViewByState();
CheckResource(new System.Collections.Generic.List<string>(){ "FishingChallengeAct", _fishingChallengeManager.eventChallengeConfig.Icon });
}
private void Start()
{
Log("Start -> ");
_btnEnter.onClick.AddListener(OnEnter);
// InitViewByState();
UpdateView();
}
private void UpdateView()
{
switch (_challengeState)
{
// 未匹配
case 0:
break;
// 匹配中
case 1:
{
_textNum.text = $"{_fishingChallengeManager.GetCurResidualNumber()}/{_fishingChallengeManager.eventChallengeMain.RobotNumber + 1}";
var newStep = _challengeStep;
bool hasReward = _fishingChallengeManager.IfHasReward();
_redpoint.SetActive(hasReward); // 如果跨段了会有奖励红点
var eliminateScore = _fishingChallengeManager.GetOriEliminateListScore();
if (_challengeUIStep > eliminateScore.Count - 1)
{
_bar.fillAmount = 1;
_bubbleTips.gameObject.SetActive(true);
_textTips.text = LocalizationMgr.GetText("UI_COMMON_completed");
return;
}
_bar.fillAmount = _fishingChallengeManager.GetFillAmount(_fishingChallengeManager.PreScore,true);
break;
}
case 2:
break;
}
}
private void InitViewByState()
{
if (_challengeState == 3)
{
return;
}
_barBg.gameObject.SetActive(_challengeState != 0);
_bar.gameObject.SetActive(_challengeState != 0);
_bubbleTips.gameObject.SetActive(_challengeState == 0);
var timeSpan = _fishingChallengeManager.GetTimeSpan();
_textTime.text = ConvertTools.ConvertTime2(timeSpan.Days, timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds);
var seconds = timeSpan.TotalSeconds;
_timer = this.AttachTimer((float)seconds, null,
elapsed =>
{
var now = _fishingChallengeManager.GetTimeSpan();
_textTime.text = ConvertTools.ConvertTime2(now.Days, now.Hours, now.Minutes, now.Seconds);
if (now.TotalMilliseconds < 0)
{
_timer?.Cancel();
_timer = null;
_textTime.text = LocalizationMgr.GetText("UI_EventRankPopupPanel_13");
// InChallenge();
}
}, useRealTime: true);
UpdateView();
}
private void ChallengePlayAni(FishingChallengeAddEvent e)
{
var newStep = _challengeStep;
if (newStep != _challengeUIStep)
{
StartCoroutine(PlayPre());
}
else if (_fishingChallengeManager.PreScore != _fishingChallengeManager.fishingChallengeData.Score)
{
StartCoroutine(PlayScorePre());
}
}
private IEnumerator PlayScorePre()
{
Log("PlayScorePre");
var newStep = _challengeStep;
var eliminateScore = _fishingChallengeManager.GetOriEliminateListScore();
// var listEliminateScore = _fishingChallengeManager.GetEliminateListScore();
if (newStep > eliminateScore.Count - 1)
{
_bar.fillAmount = 1;
_bubbleTips.gameObject.SetActive(true);
yield return null;
_textTips.text = LocalizationMgr.GetText("UI_COMMON_completed");
yield break;
}
if ((float)(_fishingChallengeManager.fishingChallengeData.Score - _fishingChallengeManager.PreScore) / eliminateScore[newStep] > 0.05f)
{
var isSuccess = _fishingChallengeManager.IsStepFinished(newStep);
if (!isSuccess)
{
_bar.fillAmount = _fishingChallengeManager.GetFillAmount(_fishingChallengeManager.PreScore,true);
var endFillAmount = _fishingChallengeManager.GetFillAmount(_fishingChallengeManager.fishingChallengeData.Score,false);
_fishingChallengeManager.PreScore = _fishingChallengeManager.fishingChallengeData.Score;
_bar.DOFillAmount(endFillAmount,_fillTime);
yield return new WaitForSeconds(_fillTime);
}
else
{
_bar.fillAmount = 1;
_bubbleTips.gameObject.SetActive(true);
yield return null;
_textTips.text = LocalizationMgr.GetText("UI_COMMON_completed");
}
}
}
private IEnumerator PlayPre()
{
Log("PlayPre");
var newStep = _challengeStep;
var step = newStep - _challengeUIStep;
// 当前先走到最后
var firstDuration = _fillTime * (1 - _bar.fillAmount);
_bar.DOFillAmount(1, firstDuration);
yield return new WaitForSeconds(firstDuration);
// 中间是完整的
if (step > 1)
{
for (var i = 0; i < step -1 ; i++)
{
// text_stage.text = LocalizationMgr.GetFormatTextValue("UI_EventChallangePanel_20", _challengeUIStep + i + 1);
_bar.fillAmount = 0;
_bar.DOFillAmount(1, _fillTime);
yield return new WaitForSeconds(_fillTime);
}
}
_fishingChallengeManager.SetUIStep(newStep);
var isSuccess = _fishingChallengeManager.IsStepFinished(newStep);
if (!isSuccess)
{
// text_stage.text = LocalizationMgr.GetFormatTextValue("UI_EventChallangePanel_20", newStep + 1);
_bar.fillAmount = 0;
var endFillAmount = _fishingChallengeManager.GetFillAmount(_fishingChallengeManager.fishingChallengeData.Score,true);
_fishingChallengeManager.PreScore = _fishingChallengeManager.fishingChallengeData.Score;
_bar.DOFillAmount(endFillAmount, _fillTime);
yield return new WaitForSeconds(_fillTime);
}
else
{
//UpdateView();
_bar.fillAmount = 1;
_bubbleTips.gameObject.SetActive(true);
// await Awaiters.NextFrame;
yield return null;
_textTips.text = LocalizationMgr.GetText("UI_COMMON_completed");
}
// reward.Play("bubble_task_out");
}
private void OnEnter()
{
Log("OnEnter");
// if (true)
// {
// GContext.Publish(new TargetAddData(1002, 0, 100));
// return;
// }
//未开始比赛
// if (_challengeState == 0)
// {
// FishingChallengeAct.CloudType = 1;
// var uiName = _fishingChallengeManager.eventChallengeConfig.MatchPanel;
// var uiType = new UIType(uiName);
// _ = UIManager.Instance.ShowUI(uiType);
// GContext.Publish(new EndTransition());
// }
// else
// {
// InChallenge();
// }
InChallenge();
}
private void InChallenge()
{
_timer?.Cancel();
_timer = null;
_fishingChallengeManager.InChallenge();
}
private void OnDestroy()
{
_btnEnter.onClick.RemoveAllListeners();
_timer?.Cancel();
_timer = null;
_disposables?.Dispose();
_disposables = null;
}
private static void Log(object message)
{
Debug.Log($"<color=orange>HomeBtnChallenge => {message} </color>");
}
protected override void OnLoadEventResource()
{
var iconName = _fishingChallengeManager.eventChallengeConfig.Icon;
GContext.container.Resolve<IUIService>().SetImageSprite(_iconImage, iconName);
gameObject.SetActive(true);
}
}