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

62 lines
1.9 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using TMPro;
using GameCore;
using asap.core;
using System;
using UniRx;
using DG.Tweening;
public class HomeBtnPiggyBank : MonoBehaviour
{
private Image _bar;
private TMP_Text _textTime;
private Button _btn;
private PiggyBankPackData _pbpd;
private IDisposable _timer;
private void Awake()
{
_bar = transform.Find("bar").GetComponent<Image>();
_textTime = transform.Find("text_time").GetComponent<TMP_Text>();
_btn = GetComponent<Button>();
_pbpd = GContext.container.Resolve<PiggyBankPackData>();
if (!_pbpd.IsPackActivated)
gameObject.SetActive(false);
GContext.OnEvent<PiggyBankProgressEvent>().Subscribe(UpdateBar).AddTo(this);
}
private void Start()
{
_btn.onClick.AddListener(OpenPanel);
}
private async void OpenPanel()
{
await UIManager.Instance.ShowUI(UITypes.GiftPiggyBankPopupPanel);
}
private void OnEnable()
{
_textTime.text = ConvertTools.ConvertTime2(_pbpd.RemainingTime);
_timer = Observable.Interval(TimeSpan.FromSeconds(1.0f))
.Subscribe(_ =>
{
_textTime.text = ConvertTools.ConvertTime2(_pbpd.RemainingTime);
if (_pbpd.RemainingTime.TotalSeconds <= 0 || _pbpd.PurchaseCount >= _pbpd.MaxCount)
gameObject.SetActive(false);
});
_bar.fillAmount = (float)_pbpd.Progress / (float)_pbpd.Target;
}
private void OnDisable()
{
_timer?.Dispose();
}
private void UpdateBar(PiggyBankProgressEvent e)
{
//Debug.Log($"update bar {e.type}");
if (e.type == 0)
_bar.fillAmount = ((float)_pbpd.Progress - (float)e.addProgress) / (float)_pbpd.Target;
else
{
float f = _bar.fillAmount;
_bar.DOFillAmount(f + (float)e.addProgress / (float)_pbpd.Target, 0.5f);
}
}
}