Files
back_cantanBuilding/Assets/Scripts/EventCan/EventCanProgressTaskView.cs
2026-05-26 16:15:54 +08:00

89 lines
3.8 KiB
C#

using asap.core;
using DG.Tweening;
using DG.Tweening.Core;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using UniRx;
using System;
using System.Threading.Tasks;
public class EventCanProgressTaskView : MonoBehaviour
{
[SerializeField] private Image progressBar;
[SerializeField] private TMP_Text textProgress;
[SerializeField] private RewardItemNew reward;
private EventCanProgressTaskInfo _info;
private IEventAggregator _eventAggregator;
/// <summary>
/// Prevents multiple task updates
/// </summary>
private bool _isUpdatingTask = false;
public void Init(EventCanProgressTaskInfo info, IEventAggregator ea)
{
float p = info.CurrentProgress / (float)info.TotalProgressRequired;
progressBar.fillAmount = p;
textProgress.text = $"{info.CurrentProgress}/{info.TotalProgressRequired}";
reward.SetData(info.Reward);
_info = info.DeepCopy();
_eventAggregator = ea;
var model = GContext.container.Resolve<EventCanModel>();
_eventAggregator.GetEvent<EventCanOpenEvent>()
.Subscribe(_ => UpdateProgressTask(model.ProgressTaskInfo))
.AddTo(this);
}
public async void UpdateProgressTask(EventCanProgressTaskInfo newInfo)
{
try
{
if (_isUpdatingTask)
return;
string[] tokens;
int currentProgress = 0;
int totalProgressRequired = 0;
TweenerCore<int, int, DG.Tweening.Plugins.Options.NoOptions> tweenText;
TweenerCore<float, float, DG.Tweening.Plugins.Options.FloatOptions> tweenBar;
Sequence sequence;
if (_info.TaskIdx != newInfo.TaskIdx)
{
_isUpdatingTask = true;
tokens = textProgress.text.Split('/');
currentProgress = int.Parse(tokens[0]);
totalProgressRequired = int.Parse(tokens[1]);
tweenText = DOTween.To(() => currentProgress, v => currentProgress = v, totalProgressRequired, 0.5f)
.OnUpdate(() => textProgress.text = $"{currentProgress}/{totalProgressRequired}");
tweenBar = progressBar.DOFillAmount(1, 0.5f);
sequence = DOTween.Sequence();
sequence.Join(tweenText).Join(tweenBar);
await sequence.AsyncWaitForCompletion();
reward.SetReceived(true);
// _eventAggregator.Publish(new EventCanTaskRewardClaimEvent(_info.Reward, reward.gameObject.transform.position));
var request = new EventRewardFlyStashRequest(_info.Reward, reward.icon.GetComponent<RectTransform>());
GContext.Publish(request);
await Task.Delay(TimeSpan.FromSeconds(1f));
progressBar.fillAmount = 0;
await Awaiters.NextFrame;
_info = newInfo.DeepCopy();
reward.SetData(newInfo.Reward);
textProgress.text = $"0/{newInfo.TotalProgressRequired}";
_isUpdatingTask = false;
}
tokens = textProgress.text.Split('/');
currentProgress = int.Parse(tokens[0]);
totalProgressRequired = newInfo.TotalProgressRequired;
tweenText = DOTween.To(() => currentProgress, v => currentProgress = v, newInfo.CurrentProgress, 0.5f)
.OnUpdate(() => textProgress.text = $"{currentProgress}/{totalProgressRequired}");
tweenBar = progressBar.DOFillAmount(newInfo.CurrentProgress / (float)newInfo.TotalProgressRequired, 0.5f);
sequence = DOTween.Sequence();
sequence.Join(tweenText).Join(tweenBar);
await sequence.AsyncWaitForCompletion();
}
catch (Exception e)
{
Debug.LogError($"[EventCan] {e.Message}\n{e.StackTrace}");
}
}
}