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

274 lines
9.0 KiB
C#

using System;
using System.Collections.Generic;
using asap.core;
using cfg;
using DG.Tweening;
using game;
using GameCore;
using Script.RuntimeScript;
using Script.RuntimeScript.model.Data;
using UniRx;
public partial class EventSandDigPanelLogic : BasePanel
{
private const int ShowRewardCount = 5;
private List<RewardItemNew> _clampRewards = new List<RewardItemNew>();
private DiggingGameManager _manager;
private SandDigServerData _serverData;
private CompositeDisposable _disposable = new CompositeDisposable();
private Timer _timer;
private RewardItemNew _flyRewardEffect;
protected override void Start()
{
base.Start();
AddEvent();
GContext.container.Resolve<GuideDataCenter>().InspectTriggerGuide("EventSandDigPanel");
_clampRewards.Add(_reward1.GetComponent<RewardItemNew>());
_clampRewards.Add(_reward2.GetComponent<RewardItemNew>());
_clampRewards.Add(_reward3.GetComponent<RewardItemNew>());
_clampRewards.Add(_reward4.GetComponent<RewardItemNew>());
_clampRewards.Add(_reward5.GetComponent<RewardItemNew>());
_flyRewardEffect = _flyReward.GetComponent<RewardItemNew>();
_flyReward.gameObject.SetActive(false);
_finished.gameObject.SetActive(false);
_supply.gameObject.SetActive(false);
_close.onClick.AddListener(OnCloseBtn);
_confirm.onClick.AddListener(OnCloseBtn);
_questionmark.onClick.AddListener(OnHelpBtn);
_manager = GContext.container.Resolve<DiggingGameManager>();
_serverData = _manager.DataModel.GetServerData();
UpdateReward();
UpdateView();
ShowTime();
ShowInfo();
}
async void ShowInfo()
{
bool isShowInfo = _manager.NewInfo();
if (isShowInfo)
{
await UIManager.Instance.ShowUI(UITypes.EventSandDigInfoPopupPanel);
}
}
public void AddEvent()
{
GContext.OnEvent<EventSandDigNewClamp>().Subscribe(OnPassClamp).AddTo(_disposable);
GContext.OnEvent<EventSandDigPropUpdate>().Subscribe(OnUpdateDigCount).AddTo(_disposable);
GContext.OnEvent<EventSandDigAddItem>().Subscribe(OnFlyReward).AddTo(_disposable);
GContext.OnEvent<RewardPanelClose>().Subscribe(OnCloseAwardPanel).AddTo(_disposable);
}
private void OnCloseAwardPanel(RewardPanelClose data)
{
UpdateReward();
}
public void ClearEvent()
{
_disposable.Dispose();
}
private void OnHelpBtn()
{
UIManager.Instance.ShowUI(UITypes.EventSandDigInfoPopupPanel);
}
private void OnCloseBtn()
{
DiggingGameManager diggingGameManager = GContext.container.Resolve<DiggingGameManager>();
if (!diggingGameManager.IsCanClick)
{
return;
}
if (diggingGameManager.GameState != SandDigGameState.Going
&& diggingGameManager.GameState != SandDigGameState.Normal)
{
return;
}
diggingGameManager.GameState = SandDigGameState.Normal;
diggingGameManager.BroadLogic?.ClearEvent();
ClearEvent();
UIManager.Instance.DestroyUI(UITypes.EventSandDigPanel);
GContext.Publish(new UnloadActToNextAct());
}
private void OnPassClamp(EventSandDigNewClamp obj)
{
UpdateRewardBar();
UpdateView();
}
private void UpdateView()
{
UpdateDigCount();
int propId = GContext.container.Resolve<DiggingGameManager>().GetActivityPropId();
if (propId > 0)
{
cfg.Item item = GContext.container.Resolve<Tables>().GetItemData(propId);
GContext.container.Resolve<IUIService>().SetImageSprite(_ticket, item.Icon, PanelName);
}
bool isPassAllClamps = GContext.container.Resolve<DiggingGameManager>().IsPassAllClamps();
icon_ticket.SetActive(!isPassAllClamps);
_finished.gameObject.SetActive(isPassAllClamps);
int index = GContext.container.Resolve<DiggingGameManager>().GetCurClampIndex();
index++;
int all = GContext.container.Resolve<DiggingGameManager>().GetAllClampCount();
string clampName = LocalizationMgr.GetFormatTextValue("UI_EventSandDigPanel_1", index, all);
_titleName.text = clampName;
}
private void OnUpdateDigCount(EventSandDigPropUpdate data)
{
UpdateDigCount();
}
private async void OnFlyReward(EventSandDigAddItem item)
{
ItemData itemDataNew = new ItemData();
itemDataNew.id = item.Id;
itemDataNew.count = item.Count;
_flyReward.transform.position = item.Pos;
_flyReward.gameObject.SetActive(true);
_flyRewardEffect.SetRewardPopupData(itemDataNew, false);
await _flyRewardEffect.PlayClose();
await _flyRewardEffect.ParticleAttractor();
_flyReward.gameObject.SetActive(false);
}
private void UpdateDigCount()
{
int digCount = _manager.DataModel.GetDigCount();
if (digCount <= 0)
{
_num.text = $"<color=red>0</color>";
}
else
{
_num.text = ConvertTools.GetNumberString(_manager.DataModel.GetDigCount());
}
}
void UpdateRewardBar()
{
float fillAmount = _bar.fillAmount + 1f / ShowRewardCount;
_bar.DOFillAmount(fillAmount, 0.5f).OnComplete(() => GContext.Publish(new ShowData()));
}
private void UpdateReward()
{
int curClampId = _serverData.ClampId;
int curIndex;
if (curClampId < 0)
{
curIndex = _manager.DataModel.AllClamps.Count;
}
else
{
curIndex = _manager.DataModel.AllClamps.IndexOf(curClampId);
}
//int[] clampIds = new[] { ShowRewardCount };
//int firstClampId = _manager.DataModel.AllClamps[0];
int clampCount = _manager.DataModel.AllClamps.Count;
DiggingGameManager.LogError("总管卡数量:" + clampCount);
//int endClampId = _manager.DataModel.AllClamps[clampCount - 1];
int maxIndex = clampCount - 1;
int startIndex;
if (curIndex <= 2)
{
startIndex = 0;
}
else if (curIndex > maxIndex - ShowRewardCount + 1)
{
startIndex = maxIndex - ShowRewardCount + 1;
}
else
{
startIndex = curIndex - 2;
if (startIndex > clampCount - ShowRewardCount)
{
startIndex = clampCount - ShowRewardCount > 0 ? clampCount - ShowRewardCount : 0;
}
}
float perCount = 0;
DiggingStageReward clampConfig;
ItemData itemData;
for (int i = 0; i < ShowRewardCount; i++)
{
int index = startIndex + i;
if (index > maxIndex)
{ break; }
int clampId = _manager.DataModel.AllClamps[index];
clampConfig = GContext.container.Resolve<Tables>().TbDiggingStageReward.Get(clampId);
if (clampConfig != null)
{
if (string.IsNullOrEmpty(clampConfig.Icon))
{
itemData = GContext.container.Resolve<PlayerItemData>().GetItemDataOne(clampConfig.DropID, 0);
_clampRewards[i].SetRewardPopupData(itemData);
}
else
{
_clampRewards[i].SetDropIcon(clampConfig.Icon, clampConfig.DropID);
}
_clampRewards[i].SetReceived(index < curIndex);
if (index < curIndex)
{
perCount++;
}
}
else
{
DiggingGameManager.LogError("沙滩寻宝 管卡奖励配置错误");
}
}
_bar.fillAmount = perCount / ShowRewardCount;
}
private void ShowTime()
{
//显示倒计时
if (_timer == null)
{
int activityId = _manager.GetActivityId();
if (activityId <= 0)
{
_time.text = string.Empty; return;
}
DateTime endTime = GContext.container.Resolve<FishingEventData>().GetEventEndTime(activityId);
TimeSpan now = endTime - ZZTimeHelper.UtcNow().UtcNowOffset();
double seconds = now.TotalSeconds;
_timer = this.AttachTimer((float)seconds,
OnTimeEnd,
(elapsed) =>
{
now = endTime - ZZTimeHelper.UtcNow().UtcNowOffset();
_time.text = LocalizationMgr.GetFormatTextValue("UI_COMMON_end",
ConvertTools.ConvertTime2(now.Days, now.Hours, now.Minutes, now.Seconds));
}, useRealTime: true);
}
}
void OnTimeEnd()
{
DiggingGameManager diggingGameManager = GContext.container.Resolve<DiggingGameManager>();
diggingGameManager.GameState = SandDigGameState.Normal;
diggingGameManager.BroadLogic?.ClearEvent();
ClearEvent();
UIManager.Instance.DestroyUI(UITypes.EventSandDigPanel);
GContext.Publish(new UnloadActToNextAct());
}
}