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

103 lines
3.8 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using TMPro;
using GameCore;
using asap.core;
using System;
using UniRx;
using DG.Tweening;
using cfg;
public class HomeBtnBargain : MonoBehaviour
{
private Image _bar;
private TMP_Text _textTime;
private Button _btn;
private BargainPackData _bpd;
private IDisposable _timer;
private IDisposable _updateBar;
private Tables _tables;
private void Awake()
{
_bar = transform.Find("bar").GetComponent<Image>();
_textTime = transform.Find("text_time").GetComponent<TMP_Text>();
_btn = GetComponent<Button>();
_bpd = GContext.container.Resolve<BargainPackData>();
_updateBar = GContext.OnEvent<BargainPackProgressEvent>().Subscribe(UpdateBar);
if (!_bpd.IsPackActivated)
gameObject.SetActive(false);
_tables = GContext.container.Resolve<Tables>();
}
private void Start()
{
_btn.onClick.AddListener(async () => { await UIManager.Instance.ShowUI(UITypes.GiftBargainPopupPanel); });
}
private void OnEnable()
{
_textTime.text = ConvertTools.ConvertTime2(_bpd.RemainingTime);
_timer = Observable.Interval(TimeSpan.FromSeconds(1.0f))
.Subscribe(_ =>
{
_textTime.text = ConvertTools.ConvertTime2(_bpd.RemainingTime);
if (_bpd.RemainingTime.TotalSeconds <= 0 || _bpd.PurchaseCount >= _bpd.MaxCount)
gameObject.SetActive(false);
});
_bar.fillAmount = _bpd.Progress / (float)_bpd.NextTarget;
// Debug.Log($"<color=red>[Bargain] OnEnable: {_bpd.Progress / (float)_bpd.NextTarget}</color>");
}
private void OnDisable()
{
_timer?.Dispose();
_updateBar?.Dispose();
}
private void OnDestroy()
{
_updateBar?.Dispose();
}
private void UpdateBar(BargainPackProgressEvent e)
{
// Debug.Log($"<color=red>[Bargain] CurrentProgress: {_bpd.Progress}, {_bpd.DiscountLvl}</color>");
SpecialPack spp = _tables.TbSpecialPack[_bpd.PackID];
int curDiscountLvl = _bpd.DiscountLvl;
int originProgress = _bpd.Progress - e.addProgress;
PlayerItemData playerItemData = GContext.container.Resolve<PlayerItemData>();
if (e.type == 0)
{
while (originProgress < 0 && curDiscountLvl >= 0)
{
int EventItemRequire = spp.EventItemRequire[curDiscountLvl];
EventItemRequire = playerItemData.IntInflation(EventItemRequire, _bpd.Inflation);
originProgress += EventItemRequire;
curDiscountLvl--;
}
_bar.fillAmount = originProgress / (float)_bpd.NextTarget;
// Debug.Log($"<color=red>[Bargain] UpdateBarType0: {originProgress / (float)_bpd.NextTarget}</color>");
}
else
{
float f = _bar.fillAmount;
if (originProgress >= 0)
{
_bar.DOFillAmount(f + e.addProgress / (float)_bpd.NextTarget, 0.5f);
// Debug.Log($"<color=red>[Bargain] UpdateBarType1: {f + e.addProgress / (float)_bpd.NextTarget}</color>");
}
else
{
PlayBarAnimation();
}
}
}
private async System.Threading.Tasks.Task PlayBarAnimation()
{
_bar.DOFillAmount(1, 0.5f);
// Debug.Log($"<color=red>[Bargain] UpdateBarType1: 1</color>");
await System.Threading.Tasks.Task.Delay(500);
_bar.fillAmount = 0;
// Debug.Log($"<color=red>[Bargain] UpdateBarType1: 0</color>");
await System.Threading.Tasks.Task.Delay(500);
_bar.DOFillAmount(_bpd.Progress / (float)_bpd.NextTarget, 0.5f);
// Debug.Log($"<color=red>[Bargain] UpdateBarType1: To {_bpd.Progress / (float)_bpd.NextTarget}</color>");
await System.Threading.Tasks.Task.Delay(500);
}
}