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

308 lines
11 KiB
C#

using System;
using asap.core;
using GameCore;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using cfg;
using game;
using UniRx;
using System.Collections.Generic;
public class ThanksGivingChainSlot : MonoBehaviour
{
private RewardItemNew _token;
private RewardItemNew[] _rewards;
private TMP_Text _textPrice;
private Button _btnBuy;
private GameObject _goLock, _goDone;
private Animation _animationPlayButton;
private IChainPackData _data;
private PlayerItemData _playerItemData;
private Pack _packData;
private List<ItemData> _rewardItemDataList;
/// <summary>
/// Position of the Slot, in zigzag snake style.
/// </summary>
private int _slotIndex;
private IAPItemList _iap;
private IEventAggregator _eventAggregator;
private GameObject[] _bgGos;
public IDisposable RewardPanelCallbackSubscription;
private List<int> _progressRewardDropList;
private CanvasGroup _canvasGroupSelected;
private const string ChangeKey = "buy_change", IdleKey = "buy_normal", DoneKey = "buy_done";
private bool IsThisSlotLocked
{
get
{
int finaleChainIdx = _data.ChainListCount - ThanksGivingPackData.SlotCount;
if (_data.ChainProgress >= finaleChainIdx) // Entering the ending stage
{
return _slotIndex > _data.ChainProgress - finaleChainIdx;
}
return _slotIndex == 0;
}
}
public void Init(int slotIdx, IChainPackData packData, IEventAggregator ea)
{
_btnBuy = transform.Find("Item/btn_buy/btn_green").GetComponent<Button>();
_token = transform.Find("Item/extra_reward").GetComponent<RewardItemNew>();
// _rewards = transform.Find("Item/reward").GetComponents<RewardItemNew>();
_rewards = new RewardItemNew[2];
_rewards[0] = transform.Find("Item/reward/reward1").GetComponent<RewardItemNew>();
_rewards[1] = transform.Find("Item/reward/reward2").GetComponent<RewardItemNew>();
_textPrice = transform.Find("Item/btn_buy/btn_green/Ani_Container/p_text").GetComponent<TMP_Text>();
_goLock = transform.Find("Item/btn_buy/btn_green/lock").gameObject;
_goDone = transform.Find("Item/done").gameObject;
_bgGos = new GameObject[2];
_bgGos[0] = transform.Find("Item/bg1").gameObject;
_bgGos[1] = transform.Find("Item/bg2").gameObject;
_animationPlayButton = transform.Find("Item").GetComponent<Animation>();
_canvasGroupSelected = transform.Find("Item/current").GetComponent<CanvasGroup>();
_data = packData;
_slotIndex = slotIdx;
_playerItemData = GContext.container.Resolve<PlayerItemData>();
if (slotIdx == 6 && !_data.IsEndGame) // the extra one slot used in animation
_packData = _data.GetChainPackByChainProgress(_data.GetChainProgressBySlotIdx(slotIdx - 1));
else
_packData = _data.GetChainPackByChainProgress(_data.GetChainProgressBySlotIdx(slotIdx));
_rewardItemDataList = _data.GetItemsByPackDropId(_packData.DropID);
// _rewardItemDataList = _playerItemData.GetItemDataByDropId(_packData.DropID);
// Debug.Log($"normal slot: {_packData.DropID}");
_token.SetData(_rewardItemDataList[0]);
_rewards[0].SetData(_rewardItemDataList[1]);
if (_rewardItemDataList.Count < 3)
_rewards[1].gameObject.SetActive(false);
else
{
_rewards[1].gameObject.SetActive(true);
_rewards[1].SetData(_rewardItemDataList[2]);
}
SetupPrice(out var tmpText, out _iap);
// Debug.Log($"_iap: {_iap}, price: {tmpText}");
_textPrice.text = tmpText;
SetSlotState();
_eventAggregator = ea;
_bgGos[0].SetActive(_packData.ID % 2 == 0);
_bgGos[1].SetActive(_packData.ID % 2 == 1);
}
private void SetSlotState()
{
int finaleChainIdx = _data.ChainListCount - ThanksGivingPackData.SlotCount;
int currentActiveSlotIndex = _data.ChainProgress >= finaleChainIdx ? _data.ChainProgress - finaleChainIdx : 0;
if (_slotIndex < currentActiveSlotIndex)//Done
{
// Debug.Log($"<color=#f18c0a>Index: {_slotIndex}</color>");
_goLock.SetActive(false);
_animationPlayButton.Play(DoneKey);
SetToken(false);
_canvasGroupSelected.gameObject.SetActive(false);
_canvasGroupSelected.alpha = 0;
return;
}
if (_slotIndex == currentActiveSlotIndex)//Active
{
_goLock.SetActive(false);
_btnBuy.onClick.RemoveAllListeners();
_btnBuy.onClick.AddListener(OnClickBuy);
if (_animationPlayButton.GetClip(IdleKey) is not null)
_animationPlayButton.Play(IdleKey);
_canvasGroupSelected.gameObject.SetActive(true);
_canvasGroupSelected.alpha = 1;
SetToken(true);
return;
}
//Locked
_goLock.SetActive(true);
_canvasGroupSelected.gameObject.SetActive(false);
_canvasGroupSelected.alpha = 0;
_btnBuy.onClick.RemoveAllListeners();
_btnBuy.onClick.AddListener(() => ToastPanel.Show(LocalizationMgr.GetText("UI_ToastPanel_30")));
SetToken(true);
}
private void SetupPrice(out string price, out IAPItemList iap)
{
int IAPID = _packData.IAPID;
iap = GContext.container.Resolve<Tables>().TbIAPItemList.GetOrDefault(IAPID);
var sdde = new SKUDetailDataEvent(iap);
GContext.Publish(sdde);
price = sdde.price;
}
private async void OnClickBuy()
{
try
{
_btnBuy.onClick.RemoveAllListeners();
var shopBuyTypeData = new ShopBuyTypeData
{
type = ShopBuyType.EventPack,
ID = _data.EventId,
IsHide = true
};
RewardPanelCallbackSubscription?.Dispose();
RewardPanelCallbackSubscription = GContext.OnEvent<RewardPanelClose>().Subscribe(AlterEventData);
bool res;
if (_iap is null)
{
_playerItemData.AddItem(_rewardItemDataList);
_data.ChainProgress++;
_progressRewardDropList = _data.GetTokenProgressRewardAfterAddingToken(_rewardItemDataList[0].count);
TryPopAllRewards();
if (_progressRewardDropList.Count > 0)
{
_playerItemData.AddItemByDropList(_progressRewardDropList, false);
}
res = true;
}
else
{
var purchaseReward = _data.GetItemsByPackDropId(_packData.DropID);
res = await GContext.container.Resolve<PlayerShopData>()
.OnBuy(_packData.DropID, shopBuyTypeData, _iap, purchaseReward, RewardType.Normal);
_progressRewardDropList = _data.GetTokenProgressRewardAfterAddingToken((int)_rewardItemDataList[0].count);
if (res)
{
TryPopAllRewards();
}
}
//Will run ThanksGivingPackData.OnBuySuccess and show reward popup panel if res is true.
//I hate callback.
if (!res)
{
_btnBuy.onClick.AddListener(OnClickBuy);
RewardPanelCallbackSubscription?.Dispose();
}
_data.UploadData();
}
catch(Exception e)
{
Debug.LogError("[ChainSlot] Click Buy Error:", this);
Debug.LogError(e);
}
}
private void AlterEventData(RewardPanelClose _)
{
_eventAggregator.Publish(new EventThanksGivingSlotClaimed(_slotIndex, _progressRewardDropList));
}
public void PlayParticle()
{
_ = _token.ParticleAttractor();
}
public void PlayButtonAnimation()
{
_animationPlayButton.Play(ChangeKey);
}
public void SetLock(bool state)
{
_goLock.SetActive(state);
}
[SerializeField] private float jiandaCanvasGroupFadeTime;
public void PlayCanvasGroupEffect(bool isFadeIn)
{
_canvasGroupSelected.gameObject.SetActive(true);
if (isFadeIn)
{
_canvasGroupSelected.alpha = 0;
_canvasGroupSelected.DOFadeAlpha(1, jiandaCanvasGroupFadeTime);
}
else
{
_canvasGroupSelected.alpha = 1;
_canvasGroupSelected.DOFadeAlpha(0, jiandaCanvasGroupFadeTime);
}
}
public void SetToken(bool doShow)
{
// _token.btn_click.onClick.RemoveAllListeners();
_token.gameObject.SetActive(doShow);
}
[SerializeField] private float jiandaButtonChangeDelay = 1.1f;
[Obsolete]
public async System.Threading.Tasks.Task PlayAnimation()
{
_ = _token.ParticleAttractor();
await System.Threading.Tasks.Task.Delay(TimeSpan.FromSeconds(1));
_animationPlayButton.Play(ChangeKey);
await System.Threading.Tasks.Task.Delay(TimeSpan.FromSeconds(jiandaButtonChangeDelay));
}
private void PopReward(int idx)
{
if (idx >= _rewardItemDataList.Count)
{
Debug.LogWarning($"[ChainSlot] idx{idx} out of range {_rewardItemDataList.Count}", this);
return;
}
RectTransform iconRt;
if (idx >= 1)
iconRt = _rewards[idx - 1].transform.Find("icon") as RectTransform;
else
iconRt = _token.transform.Find("icon") as RectTransform;
var typeInfo = ShootingChainSlot.GetItemTypeInfo(_rewardItemDataList[idx].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 = _rewardItemDataList[idx].id,
Quantity = _rewardItemDataList[idx].count,
// sourceIconSize = iconRt.rect.width,
// targetIconSize = iconRt.rect.width,
// sourcePos = iconRt.position,
// destPos = targetPos,
StartPoint = new BatchedRewardFlyPoint(iconRt),
EndPoint = new BatchedRewardFlyPoint(targetPos, 100, UIManager.Instance.transform.lossyScale.x),
isDestinationRewardStash = false,
AnimationParamIndex = 0,
sourceTextRt = iconRt.parent.Find("text_num") as RectTransform
};
GContext.Publish(request);
}
private const float PanelReactionDelay = 1.6f;
private async void TryPopAllRewards()
{
for (int i = 0; i < _rewardItemDataList.Count; i++)
{
var typeInfo = ShootingChainSlot.GetItemTypeInfo(_rewardItemDataList[i].id);
if (typeInfo.Type == 5 || (typeInfo.Type == 9 && typeInfo.SubType == 2))
{
//fish card and fish box, do nothing
;
}
else if (typeInfo.Type == 11)
{
//fish buff, do nothing
;
}
else
{
PopReward(i);
}
}
await System.Threading.Tasks.Task.Delay(TimeSpan.FromSeconds(PanelReactionDelay));
GContext.Publish(new ShowData());
AlterEventData(new RewardPanelClose());
}
}