Files
MinFt/Client/Assets/Scripts/UI/Shop/GiftBargainPopupPanel.cs
2026-04-27 12:07:32 +08:00

324 lines
13 KiB
C#

using System;
using TMPro;
using UniRx;
using UnityEngine.UI;
using cfg;
using asap.core;
using GameCore;
using UnityEngine;
using game;
using DG.Tweening;
public class GiftBargainPopupPanel : BasePanel
{
private TMP_Text _textTimer,
_textBargainProgress,
_textPrice,
_textPriceDiscount,
_textDiscountTag1,
_textDiscountTag2,
_textPriceOrigin,
_textInfo,
_textLastPriceDiscount,
_textLastPriceOrigin;
private Image _bargainBar;
private Tables _tables;
private Button _btnBuy, _btnDiscount, _btnClose, _btnInfo, _btnLastDiscount;
private GiftRewardAttached _reward4, _reward3;
private BargainPackData _bpd;
private GameObject _discountTag1, _discountTag2;
private Spine.Unity.SkeletonGraphic _npc;
private IAPItemList _iap;
private System.Threading.CancellationTokenSource _cts;
private Animation _rootAni;
private void Awake()
{
#region UI
_textTimer = transform.Find("root/text_time").GetComponent<TMP_Text>();
_bargainBar = transform.Find("root/bg_bar/bar").GetComponent<Image>();
_textBargainProgress = transform.Find("root/bg_bar/text_progress").GetComponent<TMP_Text>();
_btnBuy = transform.Find("root/btn_buy/btn_green").GetComponent<Button>();
_textPrice = transform.Find("root/btn_buy/btn_green/Ani_Container/p_text").GetComponent<TMP_Text>();
_btnDiscount = transform.Find("root/btn_discount/btn_green").GetComponent<Button>();
_textPriceDiscount =
transform.Find("root/btn_discount/btn_green/Ani_Container/p_text").GetComponent<TMP_Text>();
_textPriceOrigin = transform.Find("root/btn_discount/btn_green/Ani_Container/text_old")
.GetComponent<TMP_Text>();
_btnLastDiscount = transform.Find("root/btn_discount2/btn_green").GetComponent<Button>();
_textLastPriceDiscount =
transform.Find("root/btn_discount2/btn_green/Ani_Container/p_text").GetComponent<TMP_Text>();
_textLastPriceOrigin = transform.Find("root/btn_discount2/btn_green/Ani_Container/text_old")
.GetComponent<TMP_Text>();
_reward3 = transform.Find("root/reward_3").GetComponent<GiftRewardAttached>();
_reward4 = transform.Find("root/reward_4").GetComponent<GiftRewardAttached>();
_btnClose = transform.Find("root/btn_close").GetComponent<Button>();
_btnInfo = transform.Find("root/btn_questionmark").GetComponent<Button>();
_discountTag1 = transform.Find("root/tag/tag_1").gameObject;
_discountTag2 = transform.Find("root/tag/tag_2").gameObject;
_textDiscountTag1 = transform.Find("root/tag/tag_1/text_num").GetComponent<TMP_Text>();
_textDiscountTag2 = transform.Find("root/tag/tag_2/text_num").GetComponent<TMP_Text>();
_textInfo = transform.Find("root/bg_info/text_num").GetComponent<TMP_Text>();
_npc = transform.Find("root/npc/mask/spine").GetComponent<Spine.Unity.SkeletonGraphic>();
_rootAni = transform.Find("root/").GetComponent<Animation>();
#endregion
#region config
_tables = GContext.container.Resolve<Tables>();
_bpd = GContext.container.Resolve<BargainPackData>();
#endregion
if (!_bpd.IsPackActivated) gameObject.SetActive(false);
_cts = new System.Threading.CancellationTokenSource();
}
protected override void Start()
{
_btnClose.onClick.AddListener(() => ClosePanel());
_btnInfo.onClick.AddListener(async () => await UIManager.Instance.ShowUI(UITypes.GiftBargainInfoPopupPanel));
_textTimer.text = ConvertTools.ConvertTime2(_bpd.RemainingTime);
Observable.Interval(TimeSpan.FromSeconds(1.0f))
.Subscribe(_ => { _textTimer.text = ConvertTools.ConvertTime2(_bpd.RemainingTime); })
.AddTo(this);
_bargainBar.fillAmount = GetFillAmount(_bpd.VisualProgress, _bpd.NextVisualTarget);
if (_bpd.IsFull)
_textBargainProgress.text = LocalizationMgr.GetText("UI_FishingPanel_101007");
else
_textBargainProgress.text = $"{_bpd.VisualProgress}/{_bpd.NextVisualTarget}";
_textDiscountTag1.text = $"{_bpd.NextVisualDiscount}%";
_textInfo.text = $"{_tables.TbSpecialPack[_bpd.PackID].DiscountList[_bpd.DiscountLvlCount - 1]}%";
_textPrice.text = GetPrice(0);
_textPriceOrigin.text = GetPrice(0);
_textLastPriceOrigin.text = GetPrice(0);
if (_bpd.VisualDiscountLvl == 0)
{
_btnBuy.transform.parent.gameObject.SetActive(true);
_btnDiscount.transform.parent.gameObject.SetActive(false);
_btnLastDiscount.transform.parent.gameObject.SetActive(false);
_textPrice.text = GetPrice(0);
_btnBuy.onClick.AddListener(async () => { await OnClickBuy(); });
}
else
{
_btnBuy.transform.parent.gameObject.SetActive(false);
_btnDiscount.transform.parent.gameObject.SetActive(true);
_btnLastDiscount.transform.parent.gameObject.SetActive(false);
_textPriceDiscount.text = GetPrice(_bpd.VisualDiscountLvl);
_btnDiscount.onClick.AddListener(async () => { await OnClickBuy(); });
}
int dropID = _tables.TbSpecialPack[_bpd.PackID].DropID[0];
_reward4.SetData(GContext.container.Resolve<PlayerItemData>().GetItemDataByDropIdLureInflation(null, dropID));
PlayCutSequence(_cts.Token);
PlayNPCLoopSequence(_cts.Token);
oldPanelName = panelName;
base.Start();
}
private string GetPrice(int discountLvl)
{
int IAPID = _tables
.TbSpecialPack[_tables.TbEventPackManager[_bpd.RedirectID].VIPPackList[_bpd.VIPLvlWhenEventActivated][0]]
.IAPID[discountLvl];
_iap = _tables.TbIAPItemList.GetOrDefault(IAPID);
//Debug.Log($"_iap: {_iap}");
SKUDetailDataEvent sdde = new SKUDetailDataEvent(_iap);
GContext.Publish(sdde);
return sdde.price;
}
[SerializeField] private float _judgeInterval = 4.6f, _switchChance = 0.2f;
private float _countDown = 0;
private bool _doPlayIdle2 = false;
private void Update()
{
if (_doPlayIdle2 || _npc.AnimationState.GetCurrent(0) == null
|| _npc.AnimationState.GetCurrent(0).Animation.Name != "Idle01")
return;
_countDown += Time.deltaTime;
if (_countDown >= _judgeInterval)
{
_countDown -= _judgeInterval;
_doPlayIdle2 = (UnityEngine.Random.value < _switchChance);
}
}
[SerializeField] private float _barAnimationDuration = 0.5f;
private async System.Threading.Tasks.Task PlayCutSequence(System.Threading.CancellationToken token)
{
int p, dl, progressStart;
if (_bpd.VisualDiscountLvl >= _bpd.DiscountLvlCount - 1 && _bpd.VisualProgress >= _bpd.NextVisualTarget)
{
_state = LumberJackState.Rest;
return;
}
if (_bpd.VisualDiscountLvl >= _bpd.DiscountLvl) //discount lvl did not change
{
_bargainBar.DOFillAmount(GetFillAmount(_bpd.Progress, _bpd.NextTarget), _barAnimationDuration);
p = _bpd.VisualProgress;
DOTween.To(() => p, value => p = value, _bpd.Progress, _barAnimationDuration)
.OnUpdate(() => _textBargainProgress.text = $"{p}/{_bpd.NextTarget}");
await System.Threading.Tasks.Task.Delay((int)(_barAnimationDuration * 1000));
}
else //discount lvl change
{
dl = _bpd.VisualDiscountLvl;
progressStart = _bpd.VisualProgress;
_state = LumberJackState.Cut;
p = progressStart;
_bargainBar.fillAmount = GetFillAmount(_bpd.VisualProgress, _bpd.NextVisualTarget);
_btnBuy.onClick.RemoveAllListeners();
_btnDiscount.onClick.RemoveAllListeners();
_btnLastDiscount.onClick.RemoveAllListeners();
while (dl < _bpd.DiscountLvl)
{
_textDiscountTag1.text = $"{_bpd.GetNextDiscount(dl)}%";
_bargainBar.DOFillAmount(1, _barAnimationDuration);
DOTween.To(() => p, value => p = value, _bpd.GetNextDiscountTarget(dl), _barAnimationDuration)
.OnUpdate(() => _textBargainProgress.text = $"{p}/{_bpd.GetNextDiscountTarget(dl)}");
await System.Threading.Tasks.Task.Delay((int)(_barAnimationDuration * 1000));
//Debug.Log($"DL: {dl}");
//Debug.Log($"max lvl: {_bpd.DiscountLvlCount - 1}");
if (dl == _bpd.DiscountLvlCount - 2 && _bpd.Progress >= _bpd.NextVisualTarget)
{
_textBargainProgress.text = LocalizationMgr.GetText("UI_FishingPanel_101007");
}
_rootAni.Play("bargain_fillup");
await System.Threading.Tasks.Task.Delay((int)(40.0f / 60.0f * 1000f));
if (dl == 0)
{
_textPriceDiscount.text = GetPrice(dl + 1);
_rootAni.Play("bargain_change1");
//TODO:
}
else
{
_textLastPriceDiscount.text = GetPrice(dl);
_textPriceDiscount.text = GetPrice(dl + 1);
_rootAni.Play("bargain_change2");
//TODO:
}
PlayCutAudio();
await PlaySpine("Cut01", false);
PlaySpine("Idle01", true);
p = 0;
_bargainBar.fillAmount = 0;
dl++;
}
_textDiscountTag1.text = $"{_bpd.GetNextDiscount(dl)}%";
_bpd.RefreshVisualData();
if (_bpd.VisualDiscountLvl >= _bpd.DiscountLvlCount - 1 && _bpd.VisualProgress >= _bpd.NextVisualTarget)
{
_bargainBar.fillAmount = 1;
//_rootAni.Play("bargain_fillup");
//await System.Threading.Tasks.Task.Delay((int) (40.0f / 60.0f * 1000f));
_textPriceDiscount.text = GetPrice(dl);
//_rootAni.Play("bargain_change2");
//await PlaySpine("Cut01", false);
_btnDiscount.onClick.AddListener(async () => await OnClickBuy());
await PlaySpine("Idle04", false);
_state = LumberJackState.Rest;
}
else
{
_bargainBar.DOFillAmount(GetFillAmount(_bpd.Progress, _bpd.NextTarget), _barAnimationDuration);
DOTween.To(() => p, value => p = value, _bpd.Progress, _barAnimationDuration)
.OnUpdate(() => _textBargainProgress.text = $"{p}/{_bpd.NextTarget}");
await System.Threading.Tasks.Task.Delay((int)(_barAnimationDuration * 1000));
_btnDiscount.onClick.AddListener(async () => await OnClickBuy());
_state = LumberJackState.Idle;
}
}
//_rootAni.Play("");
}
private enum LumberJackState
{
Idle,
Cut,
Rest
}
private LumberJackState _state = LumberJackState.Idle;
private async System.Threading.Tasks.Task PlayNPCLoopSequence(System.Threading.CancellationToken token)
{
while (true)
{
if (token.IsCancellationRequested) return;
if (_state == LumberJackState.Cut)
{
PlaySpine("Idle01", true);
await new WaitUntil(() => _state != LumberJackState.Cut);
}
if (_state == LumberJackState.Rest)
{
await PlaySpine("Idle03", false);
continue;
}
await PlaySpine("Idle01", false);
if (_doPlayIdle2)
{
_doPlayIdle2 = false;
await PlaySpine("Idle02", false);
}
}
}
private async System.Threading.Tasks.Task PlaySpine(string name, bool isLoop = false)
{
_npc.AnimationState.SetAnimation(0, name, isLoop);
var animationTime = _npc.AnimationState.GetCurrent(0).Animation.Duration;
await System.Threading.Tasks.Task.Delay(System.TimeSpan.FromSeconds(animationTime));
}
private async System.Threading.Tasks.Task OnClickBuy()
{
ShopBuyTypeData shopBuyTypeData = new ShopBuyTypeData();
shopBuyTypeData.type = ShopBuyType.EventPack;
shopBuyTypeData.ID = _bpd.CurrentEventID;
bool res = await GContext.container.Resolve<PlayerShopData>().OnBuy(_bpd.RewardDropID, shopBuyTypeData, _iap,
GContext.container.Resolve<PlayerItemData>().GetItemDataByDropIdLureInflation(null, _bpd.RewardDropID));
if (res)
{
//_bpd.AddPurchase();
ClosePanel();
}
}
private void ClosePanel()
{
_cts.Cancel();
_bpd.RefreshVisualData();
UIManager.Instance.DestroyUI(UITypes.GiftBargainPopupPanel);
}
private float GetFillAmount(int progress, int target)
{
return (float)progress / (float)target;
}
private void OnDisable()
{
_cts?.Dispose();
}
private async void PlayCutAudio()
{
await System.Threading.Tasks.Task.Delay(System.TimeSpan.FromSeconds(0.55f));
GContext.Publish(new Game.EventUISound("audio_ui_eventchoptheprice_cut"));
}
}