307 lines
10 KiB
C#
307 lines
10 KiB
C#
using asap.core;
|
|
using cfg;
|
|
using GameCore;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace game
|
|
{
|
|
public class EventScratchTicketStampRewardView : MonoBehaviour
|
|
{
|
|
[Header("印花设置")]
|
|
[SerializeField] private int m_EventScratchQuality = 1;
|
|
public int EventScratchQuality { get => m_EventScratchQuality; }
|
|
|
|
[Header("UI设置")]
|
|
[SerializeField] private List<Image> m_GrayStampList = new List<Image>();
|
|
[SerializeField] private List<Image> m_LightStampList = new List<Image>();
|
|
|
|
[Header("奖励设置")]
|
|
[SerializeField] private RewardItemNew m_Reward;
|
|
|
|
[Header("动画设置")]
|
|
[SerializeField] private Animation m_Animation;
|
|
[SerializeField] private float m_AnimWaitingForFullTime = 0.1f;
|
|
[SerializeField] private string m_AnimRewardRefresh = "reward_refresh";
|
|
[SerializeField] private float m_AnimRewardRefreshDurationTime = 0.29f;
|
|
[SerializeField] private string m_AnimRewardOut = "reward_out";
|
|
[SerializeField] private float m_AnimRewardOutDurationTime = 0.37f;
|
|
[SerializeField] private string m_AnimRewardFull = "reward_full";
|
|
[SerializeField] private float m_AnimRewardFullDurationTime = 0.45f;
|
|
|
|
#region 数据相关
|
|
private static object o = new object();
|
|
public Image GetFirstEmptyImage()
|
|
{
|
|
lock (o)
|
|
{
|
|
if (m_IsCounting)
|
|
{
|
|
m_RewardNum++;
|
|
}
|
|
|
|
if (m_EmptyTargetIndex >= m_LightStampList.Count)
|
|
{
|
|
m_EmptyTargetIndex = m_LightStampList.Count - 1;
|
|
}
|
|
}
|
|
|
|
return m_LightStampList[m_EmptyTargetIndex++];
|
|
}
|
|
public void ResetLightStampList()
|
|
{
|
|
foreach (var item in m_LightStampList)
|
|
{
|
|
item.gameObject.SetActive(false);
|
|
}
|
|
}
|
|
private EventScratchCard m_ScratchCard = null;
|
|
public EventScratchCard GetEventScratchCard()
|
|
{
|
|
return m_ScratchCard;
|
|
}
|
|
public async Task<bool> CheckReward()
|
|
{
|
|
if (m_IsFull)
|
|
{
|
|
m_Animation.Play(m_AnimRewardOut);
|
|
|
|
await Awaiters.Seconds(m_AnimRewardOutDurationTime);
|
|
|
|
PlayLightStampFlash();
|
|
|
|
m_Animation.Play(m_AnimRewardRefresh);
|
|
|
|
await Awaiters.Seconds(m_AnimRewardRefreshDurationTime);
|
|
}
|
|
|
|
if (m_RewardNum < m_ScratchCard.Count)
|
|
{
|
|
PlayLightStampFlash();
|
|
|
|
m_IsFull = false;
|
|
|
|
return false;
|
|
}
|
|
|
|
PlayLightStampFlash();
|
|
|
|
m_DataManager.AddStampReward(m_ScratchCard.ID);
|
|
|
|
await Awaiters.Seconds(m_AnimWaitingForFullTime);
|
|
|
|
m_Animation.Play(m_AnimRewardFull);
|
|
|
|
await Awaiters.Seconds(m_AnimRewardFullDurationTime);
|
|
|
|
ItemData itemData = GContext.container.Resolve<PlayerItemData>().GetItemDataOne(m_ScratchCard.DropReward);
|
|
|
|
List<ItemData> rewardList = new List<ItemData>();
|
|
rewardList.Add(itemData);
|
|
|
|
// GContext.container.Resolve<PlayerItemData>().AddItem(rewardList);
|
|
GContext.Publish(new DeferredRewardStashService.EventStashItems { Items = rewardList });
|
|
|
|
ResetRewardNum();
|
|
|
|
m_IsFull = true;
|
|
|
|
return true;
|
|
}
|
|
public void ResetRewardNum()
|
|
{
|
|
m_RewardNum -= m_ScratchCard.Count;
|
|
m_EmptyFromIndex = 0;
|
|
m_EmptyTargetIndex = m_RewardNum;
|
|
}
|
|
public void SyncStampData()
|
|
{
|
|
int num = 0;
|
|
foreach (var item in m_LightStampList)
|
|
{
|
|
if (item.gameObject.activeSelf)
|
|
{
|
|
num++;
|
|
}
|
|
}
|
|
if (m_DataManager.ScratchTicketData.StampRewardDict.ContainsKey(m_EventScratchQuality))
|
|
{
|
|
var data = m_DataManager.ScratchTicketData.StampRewardDict[m_EventScratchQuality];
|
|
data.EventScratchCardID = m_ScratchCard.ID;
|
|
data.Count = num;
|
|
}
|
|
else
|
|
{
|
|
m_DataManager.ScratchTicketData.StampRewardDict[m_EventScratchQuality] = new EventScratchTicketOwnedStampRewardData()
|
|
{
|
|
EventScratchCardID = m_ScratchCard.ID,
|
|
Count = num
|
|
};
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 动画相关
|
|
private void PlayLightStampFlash()
|
|
{
|
|
int count = m_LightStampList.Count;
|
|
|
|
for (int i = 0; i < count; ++i)
|
|
{
|
|
var item = m_LightStampList[i];
|
|
if (i < m_EmptyTargetIndex)
|
|
{
|
|
item.gameObject.SetActive(true);
|
|
}
|
|
else
|
|
{
|
|
item.gameObject.SetActive(false);
|
|
}
|
|
}
|
|
|
|
SyncStampData();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 事件相关
|
|
private void InitEvent()
|
|
{
|
|
FishingScratchTicketAct.Subscribe<EventScratchTicketStampRewardUpdateData>(UpdateStampReward);
|
|
FishingScratchTicketAct.Subscribe<EventScratchTicketStartStampRewardCountUpdateData>(StartStampRewardCount);
|
|
FishingScratchTicketAct.Subscribe<EventScratchTicketStopStampRewardCountUpdateData>(StopStampRewardCount);
|
|
}
|
|
private bool m_IsFull = false;
|
|
private int m_EmptyFromIndex, m_EmptyTargetIndex = 0, m_RewardNum = 0;
|
|
public int RewardNum { get => m_RewardNum; }
|
|
private bool m_IsCounting = false;
|
|
private void StartStampRewardCount(EventScratchTicketStartStampRewardCountUpdateData data)
|
|
{
|
|
int count = m_LightStampList.Count;
|
|
m_EmptyTargetIndex = 0;
|
|
m_EmptyFromIndex = 0;
|
|
for (int i = 0; i < count; ++i)
|
|
{
|
|
if (!m_LightStampList[i].gameObject.activeInHierarchy)
|
|
{
|
|
m_EmptyFromIndex = i;
|
|
m_EmptyTargetIndex = i;
|
|
break;
|
|
}
|
|
}
|
|
|
|
m_RewardNum = m_EmptyTargetIndex;
|
|
m_IsCounting = true;
|
|
m_IsFull = false;
|
|
}
|
|
private void StopStampRewardCount(EventScratchTicketStopStampRewardCountUpdateData data)
|
|
{
|
|
m_IsCounting = false;
|
|
}
|
|
private void UpdateStampReward(EventScratchTicketStampRewardUpdateData data)
|
|
{
|
|
if (m_DataManager == null || m_DataManager.ScratchTicketData == null) return;
|
|
|
|
if (m_DataManager.ScratchTicketData.StampRewardDict != null)
|
|
{
|
|
if (m_DataManager.ScratchTicketData.StampRewardDict.ContainsKey(m_EventScratchQuality))
|
|
{
|
|
var stampReward = m_DataManager.ScratchTicketData.StampRewardDict[m_EventScratchQuality];
|
|
int num = stampReward.Count;
|
|
int count = m_LightStampList.Count;
|
|
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
if (i < num)
|
|
{
|
|
if (!m_LightStampList[i].gameObject.activeInHierarchy)
|
|
{
|
|
m_LightStampList[i].gameObject.SetActive(true);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (m_LightStampList[i].gameObject.activeInHierarchy)
|
|
{
|
|
m_LightStampList[i].gameObject.SetActive(false);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region UI相关
|
|
private void InitUI()
|
|
{
|
|
InitStampReward();
|
|
}
|
|
private void InitStampReward()
|
|
{
|
|
if (m_DataManager == null || m_DataManager.ScratchTicketData == null) return;
|
|
|
|
EventScratchMain eventScratchMain = m_DataManager.GetCurrentEventScratchMain();
|
|
|
|
foreach (var cardID in eventScratchMain.CardList)
|
|
{
|
|
EventScratchCard card = m_DataManager.GetEventScratchCard(cardID);
|
|
if (card != null && card.Quality == m_EventScratchQuality)
|
|
{
|
|
m_ScratchCard = card;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (m_ScratchCard != null)
|
|
{
|
|
foreach (var item in m_GrayStampList)
|
|
{
|
|
item.sprite = m_DataManager.SpriteAtlas.GetSprite(m_ScratchCard.Icon2);
|
|
}
|
|
foreach(var item in m_LightStampList)
|
|
{
|
|
item.sprite = m_DataManager.SpriteAtlas.GetSprite(m_ScratchCard.Icon);
|
|
}
|
|
}
|
|
|
|
PlayerItemData playerItemData = GContext.container.Resolve<PlayerItemData>();
|
|
|
|
var dataItem = playerItemData.GetItemDataOne(m_ScratchCard.DropReward);
|
|
m_Reward.SetData(dataItem, abbr: true);
|
|
|
|
foreach (var item in m_GrayStampList)
|
|
{
|
|
item.gameObject.SetActive(true);
|
|
}
|
|
foreach (var item in m_LightStampList)
|
|
{
|
|
item.gameObject.SetActive(false);
|
|
}
|
|
}
|
|
private void RefreshData()
|
|
{
|
|
if (m_DataManager == null || m_DataManager.ScratchTicketData == null) return;
|
|
|
|
UpdateStampReward(null);
|
|
}
|
|
#endregion
|
|
|
|
#region 生命周期
|
|
private EventScratchTicketDataManager m_DataManager;
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
m_DataManager = GContext.container.Resolve<EventScratchTicketDataManager>();
|
|
|
|
InitUI();
|
|
InitEvent();
|
|
RefreshData();
|
|
}
|
|
#endregion
|
|
}
|
|
}
|