202 lines
8.4 KiB
C#
202 lines
8.4 KiB
C#
using asap.core;
|
|
using cfg;
|
|
using game;
|
|
using GameCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using TMPro;
|
|
using UI.Wheel;
|
|
using UniRx;
|
|
using UnityEngine.UI;
|
|
|
|
namespace Assets.Scripts.UI.AdWheel
|
|
{
|
|
public class AdWheelPanel : BasePanel
|
|
{
|
|
#region UI_ELEMENTS
|
|
private Image bar;
|
|
private RewardItemNew bubble_reward;
|
|
private RewardItemNew bubble_final;
|
|
private AdWheelRewardComponent p_wheel;
|
|
private TextMeshProUGUI text_time;
|
|
private TextMeshProUGUI text_remaing;
|
|
private AdvertButton AdvertBtn;
|
|
private Button btn_close;
|
|
private Button btn_cd;
|
|
private TextMeshProUGUI p_text;
|
|
#endregion
|
|
private AdWheelManager _AdWheelManager;
|
|
private AdWheelManager AdWheelManager => _AdWheelManager ??= GContext.container.Resolve<AdWheelManager>();
|
|
private List<IWheelReward> _eventAdWheelRewards;
|
|
private void Awake()
|
|
{
|
|
bar = this.gameObject.FindChildGameObject(nameof(bar)).GetComponent<Image>();
|
|
bubble_reward = this.gameObject.FindChildGameObject(nameof(bubble_reward)).GetComponentInChildren<RewardItemNew>();
|
|
bubble_final = this.gameObject.FindChildGameObject(nameof(bubble_final)).GetComponentInChildren<RewardItemNew>();
|
|
text_time = this.gameObject.FindChildGameObject(nameof(text_time)).GetComponent<TextMeshProUGUI>();
|
|
p_wheel = this.transform.GetComponentInChildren<AdWheelRewardComponent>();
|
|
AdvertBtn = this.gameObject.FindChildGameObject(nameof(AdvertBtn)).GetComponent<AdvertButton>();
|
|
AdvertBtn.SetData(OnBuySuccess, AdvertPopupType.AdWheel);
|
|
text_remaing = AdvertBtn.gameObject.FindChildGameObject(nameof(text_remaing)).GetComponent<TextMeshProUGUI>();
|
|
btn_close = this.gameObject.FindChildGameObject(nameof(btn_close)).GetComponent<Button>();
|
|
btn_cd = this.gameObject.FindChildGameObject(nameof(btn_cd)).GetComponentInChildren<Button>();
|
|
btn_cd.onClick.AddListener(OnClick_btn_cd);
|
|
p_text = btn_cd.gameObject.FindChildGameObject(nameof(p_text)).GetComponentInChildren<TextMeshProUGUI>();
|
|
|
|
Observable.Interval(TimeSpan.FromSeconds(1f)).Subscribe(UpdateTimer).AddTo(disposables);
|
|
}
|
|
protected override void OnDestroy()
|
|
{
|
|
_eventAdWheelRewards.Clear();
|
|
_eventAdWheelRewards = null;
|
|
btn_cd.onClick.RemoveListener(OnClick_btn_cd);
|
|
//GContext.container.Resolve<IDeferredRewardStashService>()?.Flush();
|
|
//GContext.Publish(new ShowData());//允许跳过动画直接关闭
|
|
base.OnDestroy();
|
|
}
|
|
private void UpdateTimer(long obj)
|
|
{
|
|
var adWheelData = _AdWheelManager.Data;
|
|
if (adWheelData == null)
|
|
return;
|
|
|
|
text_time.text = ConvertTools.ConvertTime2(adWheelData.RemainingTime);
|
|
if (btn_cd.isActiveAndEnabled)
|
|
ShowBtn();
|
|
}
|
|
protected override void Start()
|
|
{
|
|
base.Start();
|
|
GContext.OnEvent<RewardPanelClose>().Subscribe(OnRewardPanelClose).AddTo(disposables);
|
|
InitUI();
|
|
ShowUI();
|
|
}
|
|
private void InitUI()
|
|
{
|
|
text_time.text = string.Empty;
|
|
bubble_reward.gameObject.SetActive(false);
|
|
bubble_final.gameObject.SetActive(false);
|
|
bar.fillAmount = 0;
|
|
}
|
|
private void ShowUI()
|
|
{
|
|
if (AdWheelManager == null || AdWheelManager.Data == null) return;
|
|
|
|
ShowProgressUI();
|
|
text_time.text = ConvertTools.ConvertTime2(AdWheelManager.Data.RemainingTime);
|
|
|
|
_eventAdWheelRewards = AdWheelManager.GetEventAdWheelRewards();
|
|
if (_eventAdWheelRewards == null || _eventAdWheelRewards.Count == 0) return;
|
|
p_wheel.SetData(_eventAdWheelRewards);
|
|
p_wheel.ShowUI();
|
|
var progressRewards = AdWheelManager.GetProgressRewards(AdWheelManager.Data);
|
|
if (progressRewards != null && progressRewards.Count == 2)
|
|
{
|
|
bubble_reward.SetData(progressRewards[0]);
|
|
bubble_final.SetData(progressRewards[1]);
|
|
}
|
|
}
|
|
private void ShowProgressUI()
|
|
{
|
|
ShowBtn();
|
|
|
|
bar.fillAmount = AdWheelManager.Data.Progerss;
|
|
var MaxCount = AdWheelManager.Data.AdWheelMain?.ProgressRewardADTimeList.LastOrDefault() ?? 1;
|
|
var remainCount = MaxCount - AdWheelManager.Data.Count;
|
|
text_remaing.text = LocalizationMgr.GetFormatTextValue("UI_EventBonusWheelPopupPanel_3", $"{remainCount}");
|
|
}
|
|
|
|
private void ShowBtn()
|
|
{
|
|
var nowTime = new DateTimeOffset(ZZTimeHelper.UtcNow().UtcNowOffset()).ToUnixTimeSeconds();
|
|
var cdTime = nowTime - AdWheelManager.Data.lastViewAdTime;
|
|
var isCd = cdTime <= AdWheelManager.Data.AdWheelMain.ADInterval;
|
|
|
|
AdvertBtn.gameObject.SetActive(AdWheelManager.Data.Progerss < 1 && !isCd);
|
|
btn_cd.transform.parent.gameObject.SetActive(isCd && AdWheelManager.Data.Progerss < 1);
|
|
|
|
var remainTime = AdWheelManager.Data.AdWheelMain.ADInterval - (int)cdTime;
|
|
p_text.text = ConvertTools.ConvertTime2(new TimeSpan(0, 0, remainTime));
|
|
}
|
|
private bool isRotating = false;
|
|
private void OnBuySuccess()
|
|
{
|
|
isRotating = true;
|
|
this.AdWheelManager.Data.Count += 1;
|
|
this.AdWheelManager.Data.lastViewAdTime = new DateTimeOffset(ZZTimeHelper.UtcNow().UtcNowOffset()).ToUnixTimeSeconds();
|
|
AdWheelManager.SaveData();
|
|
var RewardIndex = AdWheelManager.GetRandomRewardIndex();
|
|
|
|
if (_eventAdWheelRewards.Count <= RewardIndex) return;
|
|
var reward = _eventAdWheelRewards[RewardIndex];
|
|
GContext.Publish(new DeferredRewardStashService.EventStashDrop() { DropId = reward.DropID });
|
|
|
|
AdvertBtn.gameObject.SetActive(false);
|
|
btn_cd.transform.parent.gameObject.SetActive(false);
|
|
btn_close.enabled = false;
|
|
|
|
_ = p_wheel.PlayEffect(RewardIndex, reward,
|
|
() =>
|
|
{
|
|
isRotating = false;
|
|
GContext.container.Resolve<IDeferredRewardStashService>()?.Flush(RewardType.NormalOne);
|
|
GContext.Publish(new ShowData());
|
|
|
|
if (btn_close)
|
|
btn_close.enabled = true;
|
|
});
|
|
|
|
}
|
|
public void OnRewardPanelClose(RewardPanelClose e)
|
|
{
|
|
ShowProgressUI();
|
|
DropProgressReward();
|
|
}
|
|
private void DropProgressReward()
|
|
{
|
|
var adWheelData = _AdWheelManager.Data;
|
|
if (adWheelData == null)
|
|
return;
|
|
|
|
if (adWheelData.ProgressRewardADTime == adWheelData.Count)
|
|
return;
|
|
var res = AdWheelManager.GetProgressRewardByProgress(adWheelData, out var dropID);
|
|
|
|
if (res && dropID > 0)
|
|
{
|
|
GContext.container.Resolve<PlayerItemData>().AddItemByDrop(dropID, null, true, RewardType.NormalOne);
|
|
GContext.Publish(new ShowData());
|
|
adWheelData.ProgressRewardADTime = adWheelData.Count;
|
|
_AdWheelManager.SaveData();
|
|
}
|
|
}
|
|
private bool isSharking = false;
|
|
private void OnClick_btn_cd()
|
|
{
|
|
if (isRotating) return;
|
|
if (isSharking) return;
|
|
isSharking = true;
|
|
_ = p_wheel.PlayCdEffect(() => { isSharking = false; });
|
|
}
|
|
//#if UNITY_EDITOR
|
|
// // 在Inspector中显示调试信息
|
|
// void OnGUI()
|
|
// {
|
|
// if (!Application.isPlaying) return;
|
|
// GUILayout.BeginArea(new Rect(50, 200, 200, 200));
|
|
// GUILayout.Space(10);
|
|
// if (GUILayout.Button("清除购买次数"))
|
|
// {
|
|
// _AdWheelManager.Data.Count = 0;
|
|
// _AdWheelManager.Data.ProgressRewardADTime = 0;
|
|
// _AdWheelManager.Data.lastViewAdTime = 0;
|
|
// _AdWheelManager.SaveData();
|
|
// ShowUI();
|
|
// }
|
|
// GUILayout.EndArea();
|
|
|
|
// }
|
|
//#endif
|
|
}
|
|
} |