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

131 lines
4.1 KiB
C#

using asap.core;
using cfg;
using game;
using GameCore;
using Script.RuntimeScript;
using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine.UI;
public class HomeSandigButton : EventButtonResource
{
public Button btn_sandig;
public TMP_Text txt_sandig;
public Image icon_sandig;
private Timer _sanddingTime;
Tables _tables;
int activityId;
//4-1
private void Awake()
{
_tables = GContext.container.Resolve<Tables>();
}
private void Start()
{
btn_sandig.onClick.AddListener(OnClickSandDig);
}
private void OnEnable()
{
activityId = GContext.container.Resolve<DiggingGameManager>().GetActivityId();
if (activityId <= 0)
{
btn_sandig.gameObject.SetActive(false);
txt_sandig.text = string.Empty;
return;
}
var eventData = _tables.TbFishingEvent.GetOrDefault(activityId);
if (eventData != null)
{
var sandInit = _tables.TbDiggingActivityInit.GetOrDefault(eventData.RedirectID);
List<string> strings = new List<string>() { sandInit.Icon };
strings.AddRange(sandInit.DownloadLabel);
CheckResource(strings);
}
else
{
btn_sandig.gameObject.SetActive(false);
}
}
void OnClickSandDig()
{
InSandDigAct();
}
async void InSandDigAct()
{
activityId = GContext.container.Resolve<DiggingGameManager>().GetActivityId();
if (activityId <= 0)
{
return;
}
var eventData = _tables.TbFishingEvent.GetOrDefault(activityId);
if (eventData != null)
{
var sandInit = _tables.TbDiggingActivityInit.GetOrDefault(eventData.RedirectID);
ILoadResourceService loadResourceService = GContext.container.Resolve<ILoadResourceService>();
bool isCanEnter = await loadResourceService.Loads(sandInit.DownloadLabel);
if (isCanEnter)
{
EnterSandDigAct();
}
else
{
var panel = await UIManager.Instance.ShowUI(UITypes.CloudTransitionPanel);
panel.GetComponent<CloudTransitionPanel>().SetBtn(true, EnterSandDigAct);
}
}
}
void EnterSandDigAct()
{
GContext.Publish(new UnloadActToNextAct("SandDigAct"));
}
private void ShowSandDigTime()
{
//显示倒计时
if (_sanddingTime == null)
{
activityId = GContext.container.Resolve<DiggingGameManager>().GetActivityId();
if (activityId <= 0)
{
btn_sandig.gameObject.SetActive(false);
txt_sandig.text = string.Empty;
return;
}
var eventData = _tables.TbFishingEvent.GetOrDefault(activityId);
if (eventData != null)
{
var sandInit = _tables.TbDiggingActivityInit.GetOrDefault(eventData.RedirectID);
GContext.container.Resolve<IUIService>().SetImageSprite(icon_sandig, sandInit.Icon);
}
btn_sandig.gameObject.SetActive(true);
DateTime endTime = GContext.container.Resolve<FishingEventData>().GetEventEndTime(activityId);
TimeSpan now = endTime - ZZTimeHelper.UtcNow().UtcNowOffset();
double seconds = now.TotalSeconds;
_sanddingTime = this.AttachTimer((float)seconds,
RefreshSandDig,
(elapsed) =>
{
now = endTime - ZZTimeHelper.UtcNow().UtcNowOffset();
txt_sandig.text = ConvertTools.ConvertTime2(now.Days, now.Hours, now.Minutes, now.Seconds);
}, useRealTime: true);
}
}
private void RefreshSandDig()
{
_sanddingTime?.Cancel();
_sanddingTime = null;
btn_sandig.gameObject.SetActive(false);
}
private void OnDisable()
{
_sanddingTime?.Cancel();
_sanddingTime = null;
}
protected override void OnLoadEventResource()
{
ShowSandDigTime();
}
}