using System; using System.Collections; using TMPro; using UnityEngine; using UnityEngine.UI; using System.Collections.Generic; using asap.core; using DG.Tweening; using game; using GameCore; using UniRx; using UniRx.Triggers; public class ThanksGivingPopupPanel : MonoBehaviour { [SerializeField] private TMP_Text textTimer, textProgress, textComplete; [SerializeField] private Image barProgress; [SerializeField] private RewardItemNew tokenIcon, rewardProgress; [SerializeField] private ThanksGivingChainSlot[] slots; [SerializeField] private ThanksGivingChainSlot slot6; [SerializeField] private Button btnClose; [SerializeField] private Animation targetAnimation, contentAnimation; [SerializeField] private float jiandaProgressBarIncreaseTime; private readonly IEventAggregator _eventAggregator = new EventAggregator(); private static readonly int[] IndexFromToSlotIndex = { 0, 1, 3, 2, 4, 5 }; private ThanksGivingPackData _data; private int _visualProgress; private const string ContentAnimationShiftKey = "item_change", ContentAnimationIdleKey = "item_normal"; private void Start() { btnClose.onClick.AddListener(OnClickClose); } public void Init(ThanksGivingPackData data) { _data = data; for (int i = 0; i < IndexFromToSlotIndex.Length; i++) // The idx here is a little confusing.... slots[i].Init(IndexFromToSlotIndex[i], _data, _eventAggregator); _eventAggregator.GetEvent().Subscribe(OnClaim).AddTo(this); textTimer.text = ConvertTools.ConvertTime2(_data.RemainingTime); Observable.Interval(TimeSpan.FromSeconds(1.0f)).Subscribe(_ => { textTimer.text = ConvertTools.ConvertTime2(_data.RemainingTime); if (_data.RemainingTime.TotalSeconds <= 0) OnClickClose(); }).AddTo(this); _visualProgress = _data.TokenProgress; InitTargetProgress(); contentAnimation.Play(ContentAnimationIdleKey); } private const string BubbleKey = "bubble_task"; /// /// Triggered when player click the claim button on the reward panel. Important. /// /// Contains the index of the slot that is being called. private void OnClaim(EventThanksGivingSlotClaimed e) { Debug.Log($"[ChainPack]On Claim stuff"); StopAllCoroutines(); // StartCoroutine(PlayParticle(e)); StartCoroutine(PlayButtonChange(e)); StartCoroutine(PlayProgressBar(e)); StartCoroutine(ShiftSlots()); // _ = System.Threading.Tasks.Task.Delay(TimeSpan.FromSeconds(1.0f)) // .ContinueWith(_ => Debug.Log($"End of Animation")); // if (e.SlotIdx >= ThanksGivingPackData.SlotCount - 1) // { // OnClickClose(); // } } [SerializeField] private float jiandaParticleDelay; private IEnumerator PlayParticle(EventThanksGivingSlotClaimed e) { yield return new WaitForSeconds(jiandaParticleDelay); var slot = slots[IndexFromToSlotIndex[e.SlotIdx]]; slot.RewardPanelCallbackSubscription?.Dispose(); slot.PlayParticle(); } [SerializeField] private float jiandaButtonChangeDelay; private IEnumerator PlayButtonChange(EventThanksGivingSlotClaimed e) { yield return new WaitForSeconds(jiandaButtonChangeDelay); var slot = slots[IndexFromToSlotIndex[e.SlotIdx]]; slot.RewardPanelCallbackSubscription?.Dispose(); slot.PlayButtonAnimation(); slot.PlayCanvasGroupEffect(false); slot.SetToken(false); if (e.SlotIdx + 1 < IndexFromToSlotIndex.Length) { slots[IndexFromToSlotIndex[e.SlotIdx + 1]].SetLock(false); slots[IndexFromToSlotIndex[e.SlotIdx + 1]].PlayCanvasGroupEffect(true); } } [SerializeField] private float jiandaProgressBarDelay; private IEnumerator PlayProgressBar(EventThanksGivingSlotClaimed e) { yield return new WaitForSeconds(jiandaProgressBarDelay); _data.GetTokenProgressTargetByProgress(_visualProgress, out var targetDisplay, out var scoreDisplay); var midtermTarget = targetDisplay - scoreDisplay + _visualProgress; int rewardDropId; if (_data.IsChainPackDepleted) { yield return DOTween.To(() => _visualProgress, p => _visualProgress = p, midtermTarget, jiandaProgressBarIncreaseTime).OnUpdate(() => { _data.GetTokenProgressTargetByProgress(_visualProgress, out targetDisplay, out scoreDisplay); // Debug.Log($"{_visualProgress}: {scoreDisplay}/{targetDisplay}"); textProgress.text = $"{scoreDisplay}/{targetDisplay}"; barProgress.fillAmount = (float)scoreDisplay / targetDisplay; }).WaitForCompletion(); targetAnimation.Play(BubbleKey); barProgress.fillAmount = 1; textProgress.gameObject.SetActive(false); textComplete.gameObject.SetActive(true); rewardProgress.SetReceived(true); // rewardDropId = _data.GetRewardDropByChainProgress(_visualProgress); // rewardProgress.SetData(GContext.container.Resolve().GetItemDataByDropId(rewardDropId)[0]); // GContext.Publish(new ShowData(GContext.container.Resolve() // .GetItemDataByDropId(e.ProgressRewardGot[0], afterAdding: true))); // GContext.Publish(new ShowData()); var progressReward = GContext.container.Resolve().GetItemDataByDropId(e.ProgressRewardGot[0], afterAdding: true)[0]; var iconRx = rewardProgress.transform.Find("icon") as RectTransform; var typeInfo = ShootingChainSlot.GetItemTypeInfo(progressReward.id); var particleAttractorData = new ParticleAttractorData { Type = typeInfo.Type, SubType = typeInfo.SubType, Priority = -1, IgnorePack = true }; GContext.Publish(particleAttractorData); var targetPos = particleAttractorData.uIParticleAttractorCenter.transform.position; var request = new BatchedRewardFlyRequest { itemID = progressReward.id, Quantity = progressReward.count, // sourceIconSize = iconRx.rect.width, // targetIconSize = iconRx.rect.width, // sourcePos = iconRx.position, // destPos = targetPos, StartPoint = new BatchedRewardFlyPoint(iconRx), EndPoint = new BatchedRewardFlyPoint(targetPos, 100, 1), isDestinationRewardStash = false, }; GContext.Publish(request); yield break; } if (e.ProgressRewardGot.Count > 0) { yield return DOTween.To(() => _visualProgress, p => _visualProgress = p, midtermTarget, jiandaProgressBarIncreaseTime) .OnUpdate(() => { _data.GetTokenProgressTargetByProgress(_visualProgress, out targetDisplay, out scoreDisplay); // Debug.Log($"{_visualProgress}: {scoreDisplay}/{targetDisplay}"); textProgress.text = $"{scoreDisplay}/{targetDisplay}"; barProgress.fillAmount = (float)scoreDisplay / targetDisplay; }).WaitForCompletion(); targetAnimation.Play(BubbleKey); barProgress.fillAmount = 0; textProgress.text = $"0/{targetDisplay}"; yield return new WaitForSeconds(35f / 60); var progressReward = GContext.container.Resolve().GetItemDataByDropId(e.ProgressRewardGot[0], afterAdding: true)[0]; var iconRx = rewardProgress.transform.Find("icon") as RectTransform; var typeInfo = ShootingChainSlot.GetItemTypeInfo(progressReward.id); var particleAttractorData = new ParticleAttractorData { Type = typeInfo.Type, SubType = typeInfo.SubType, Priority = -1, IgnorePack = true }; GContext.Publish(particleAttractorData); var targetPos = particleAttractorData.uIParticleAttractorCenter.transform.position; var request = new BatchedRewardFlyRequest { itemID = progressReward.id, Quantity = progressReward.count, // sourceIconSize = iconRx.rect.width, // targetIconSize = iconRx.rect.width, // sourcePos = iconRx.position, // destPos = targetPos, StartPoint = new BatchedRewardFlyPoint(iconRx), EndPoint = new BatchedRewardFlyPoint(targetPos, 100, 1), isDestinationRewardStash = false, }; GContext.Publish(request); } rewardDropId = _data.GetRewardDropByChainProgress(_visualProgress); rewardProgress.SetData(GContext.container.Resolve() .GetItemDataByDropId(rewardDropId)[0]); yield return DOTween.To(() => _visualProgress, p => _visualProgress = p, _data.TokenProgress, jiandaProgressBarIncreaseTime) .OnUpdate(() => { _data.GetTokenProgressTargetByProgress(_visualProgress, out targetDisplay, out scoreDisplay); textProgress.text = $"{scoreDisplay}/{targetDisplay}"; barProgress.fillAmount = (float)scoreDisplay / targetDisplay; }).WaitForCompletion(); // if (e.ProgressRewardGot.Count > 0) // { // GContext.Publish( // new ShowData(GContext.container.Resolve() // .GetItemDataByDropId(e.ProgressRewardGot[0], afterAdding: true))); // GContext.Publish(new ShowData()); // } } [SerializeField] private float jiandaSlotShiftDelay; private IEnumerator ShiftSlots() { yield return new WaitForSeconds(jiandaSlotShiftDelay); if (!_data.IsEndGame) { slot6.Init(6, _data, _eventAggregator); contentAnimation.Play(ContentAnimationShiftKey); yield return new WaitForSeconds(35f / 60); contentAnimation.Play(ContentAnimationIdleKey); } for (int i = 0; i < IndexFromToSlotIndex.Length; i++) // The idx here is a little confusing.... { slots[i].Init(IndexFromToSlotIndex[i], _data, _eventAggregator); } } private void OnClickClose() { RedPointManager.Instance.SetRedPointState(_data.RedPointKey, _data.DoNeedPackRedPoint); UIManager.Instance.DestroyUI(gameObject.name); } private void InitTargetProgress() { if (_data.IsChainPackDepleted) { barProgress.fillAmount = 1; textProgress.gameObject.SetActive(false); textComplete.gameObject.SetActive(true); var rewardDropId = _data.GetRewardDropByChainProgress(_visualProgress); rewardProgress.SetData(GContext.container.Resolve() .GetItemDataByDropId(rewardDropId)[0]); rewardProgress.SetReceived(true); } else { _data.GetTokenProgressTargetByProgress(_visualProgress, out var targetDisplay, out var scoreDisplay); // Debug.Log($"Start {_visualProgress}-{targetDisplay} - {scoreDisplay}"); barProgress.fillAmount = scoreDisplay / (float)targetDisplay; textProgress.text = $"{scoreDisplay}/{targetDisplay}"; textComplete.gameObject.SetActive(false); tokenIcon.SetIcon(_data.TokenIconUrl); var rewardDropId = _data.GetRewardDropByChainProgress(_visualProgress); rewardProgress.SetData(GContext.container.Resolve() .GetItemDataByDropId(rewardDropId)[0]); } } private void UpdateTargetProgress(EventThanksGivingSlotClaimed e) { int targetDisplay, scoreDisplay; DOTween.To(() => _visualProgress, p => _visualProgress = p, _data.TokenProgress, jiandaProgressBarIncreaseTime) .OnUpdate(() => { _data.GetTokenProgressTargetByProgress(_visualProgress, out targetDisplay, out scoreDisplay); textProgress.text = $"{scoreDisplay}/{targetDisplay}"; barProgress.fillAmount = (float)scoreDisplay / targetDisplay; }); // if (e.ProgressRewardGot.Count <= 0) // return; } } public class EventThanksGivingSlotClaimed { public readonly int SlotIdx; public List ProgressRewardGot; public EventThanksGivingSlotClaimed(int slotIdx, List progressRewardGot) { SlotIdx = slotIdx; ProgressRewardGot = progressRewardGot; } }