281 lines
10 KiB
C#
281 lines
10 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 ShootingChainSlot : MonoBehaviour
|
|
{
|
|
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 CanvasGroup _canvasGroupSelected;
|
|
private const string ChangeKey = "buy_change2", IdleKey = "buy_normal2", 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>();
|
|
// _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>();
|
|
_animationPlayButton.Play(IdleKey);
|
|
_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 = _playerItemData.GetItemDataByDropId(_packData.DropID);
|
|
_rewards[0].SetData(_rewardItemDataList[0]);
|
|
if (_rewardItemDataList.Count <= 1)
|
|
_rewards[1].gameObject.SetActive(false);
|
|
else
|
|
{
|
|
_rewards[1].gameObject.SetActive(true);
|
|
_rewards[1].SetData(_rewardItemDataList[1]);
|
|
}
|
|
SetupPrice(out var tmpText, out _iap);
|
|
_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
|
|
{
|
|
_goLock.SetActive(false);
|
|
_animationPlayButton.Play(DoneKey);
|
|
_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;
|
|
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")));
|
|
}
|
|
|
|
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 = false;
|
|
if (_iap is null)
|
|
{
|
|
// GContext.Publish(new ShowData(_rewardItemDataList));
|
|
_playerItemData.AddItem(_rewardItemDataList);
|
|
_data.ChainProgress++;
|
|
|
|
// Debug.Log($"color=#f18c0aOn Click Buy</color>");
|
|
res = true;
|
|
TryPopRewards();
|
|
}
|
|
else
|
|
{
|
|
res = await GContext.container.Resolve<PlayerShopData>().OnBuy(
|
|
_packData.DropID, shopBuyTypeData, _iap, GContext.container.Resolve<PlayerItemData>().GetItemDataByDropId(_packData.DropID), RewardType.Normal);
|
|
if (res)
|
|
TryPopRewards();
|
|
}
|
|
//Will run ThanksGivingPackData.OnBuySuccess and show reward popup panel if res is true.
|
|
//I hate callback.
|
|
if (!res)
|
|
{
|
|
_btnBuy.onClick.AddListener(OnClickBuy);
|
|
RewardPanelCallbackSubscription?.Dispose();
|
|
}
|
|
else
|
|
{
|
|
ShootingRangeAct.EventAggregator.Publish(new ShootingRangeAct.EventAmmoBought());
|
|
}
|
|
// Debug.Log($"[EventLuckMagic] 3rd: {_data.GetHashCode()}");
|
|
_data.UploadData();
|
|
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogError("[ChainSlot] Click Buy Error:", this);
|
|
Debug.LogError(e);
|
|
}
|
|
}
|
|
|
|
private void AlterEventData(RewardPanelClose _)
|
|
{
|
|
_eventAggregator.Publish(new EventChainPackClaimed(_slotIndex));
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
[SerializeField] private float jiandaButtonChangeDelay = 1.1f;
|
|
public static (int Type, int SubType) GetItemTypeInfo(int itemId)
|
|
{
|
|
var itemTable = GContext.container.Resolve<Tables>().TbItem;
|
|
var res = itemTable.DataMap.TryGetValue(itemId, out var itemData);
|
|
if (res)
|
|
return (itemData.Type, itemData.SubType);
|
|
else
|
|
{
|
|
Debug.LogWarning($"[ChainSlot] Item {itemId} is not included in item table");
|
|
return ErrorType;
|
|
}
|
|
}
|
|
|
|
private static (int Type, int SubType) ErrorType = (0, 0);
|
|
|
|
private void PopReward(int idx)
|
|
{
|
|
if (idx >= _rewardItemDataList.Count)
|
|
{
|
|
Debug.LogWarning($"[ChainSlot] idx{idx} out of range {_rewardItemDataList.Count}", this);
|
|
return;
|
|
}
|
|
var iconRt = _rewards[idx].transform.Find("icon") as RectTransform;
|
|
var typeInfo = 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,
|
|
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 async void TryPopRewards()
|
|
{
|
|
for (int i = 0; i < _rewardItemDataList.Count; i++)
|
|
{
|
|
var typeInfo = 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(0.5f));
|
|
GContext.Publish(new ShowData());
|
|
AlterEventData(new RewardPanelClose());
|
|
}
|
|
}
|