206 lines
5.5 KiB
C#
206 lines
5.5 KiB
C#
using System;
|
||
using asap.core;
|
||
using DataCenter;
|
||
using GameCore;
|
||
using TMPro;
|
||
using UniRx;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
|
||
public class HomeBtnPartnerGather : MonoBehaviour
|
||
{
|
||
private EventPartnerGatherManager _gatherManager;
|
||
|
||
|
||
// if (!_data.IsEventActivated)
|
||
// {
|
||
// gameObject.SetActive(false);
|
||
// return;
|
||
// }
|
||
// UI组件
|
||
private Image _iconImage;
|
||
private GameObject _redpoint;
|
||
private Button _btnEnter;
|
||
private TMP_Text _textTime;
|
||
|
||
private Timer _timer;
|
||
private CompositeDisposable _disposables = new();
|
||
|
||
private void Awake()
|
||
{
|
||
// 尝试解析管理器
|
||
try
|
||
{
|
||
_gatherManager = GContext.container.Resolve<EventPartnerGatherManager>();
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
Log($"Failed to resolve EventPartnerGatherManager: {e.Message}");
|
||
gameObject.SetActive(false);
|
||
return;
|
||
}
|
||
|
||
// 查找UI组件(使用安全检查)
|
||
var iconTransform = transform.Find("icon_task");
|
||
if (iconTransform != null)
|
||
_iconImage = iconTransform.GetComponent<Image>();
|
||
|
||
var redpointTransform = transform.Find("redpoint");
|
||
if (redpointTransform != null)
|
||
_redpoint = redpointTransform.gameObject;
|
||
|
||
_btnEnter = transform.GetComponent<Button>();
|
||
|
||
var timeTransform = transform.Find("text_time");
|
||
if (timeTransform != null)
|
||
_textTime = timeTransform.GetComponent<TMP_Text>();
|
||
|
||
// 订阅事件
|
||
if (_disposables == null)
|
||
_disposables = new CompositeDisposable();
|
||
|
||
GContext.OnEvent<EventPartnerGatherRefreshEvent>().Subscribe(OnRefreshByEvent).AddTo(_disposables);
|
||
}
|
||
|
||
private void OnEnable()
|
||
{
|
||
// 检查管理器是否存在
|
||
if (_gatherManager == null)
|
||
{
|
||
Log("Manager not available");
|
||
gameObject.SetActive(false);
|
||
return;
|
||
}
|
||
|
||
//
|
||
_gatherManager.CheckForOnEnable();
|
||
if (!_gatherManager.CheckOpen())
|
||
{
|
||
gameObject.SetActive(false);
|
||
return;
|
||
}
|
||
|
||
var iconName = _gatherManager.EventPartnerMain2Data.Icon;
|
||
GContext.container.Resolve<IUIService>().SetImageSprite(_iconImage, iconName);
|
||
_redpoint.SetActive(_gatherManager.IsFirstOpen);
|
||
|
||
|
||
//UpdateTimeDisplay();
|
||
|
||
// 设置图标(后续从配置读取)
|
||
// var iconName = _gatherManager.GetIconName();
|
||
// if (_iconImage != null)
|
||
// GContext.container.Resolve<IUIService>().SetImageSprite(_iconImage, iconName);
|
||
|
||
// 显示红点
|
||
if (_redpoint != null)
|
||
_redpoint.SetActive(_gatherManager.IsFirstOpen);
|
||
}
|
||
|
||
private void Start()
|
||
{
|
||
//BindEvents;
|
||
_btnEnter.onClick.AddListener(OnBtnEnter);
|
||
_gatherManager.CheckInit();
|
||
|
||
if (!_gatherManager.CheckOpen())
|
||
{
|
||
gameObject.SetActive(false);
|
||
return;
|
||
}
|
||
|
||
var iconName = _gatherManager.EventPartnerMain2Data.Icon;
|
||
GContext.container.Resolve<IUIService>().SetImageSprite(_iconImage, iconName);
|
||
_redpoint.SetActive(_gatherManager.IsFirstOpen);
|
||
|
||
|
||
|
||
if (_gatherManager != null)
|
||
StartCountDown();
|
||
}
|
||
|
||
private void StartCountDown()
|
||
{
|
||
StopCountDown();
|
||
|
||
var timeSpan = _gatherManager.GetRemainingTime();
|
||
UpdateTimeDisplay(timeSpan);
|
||
|
||
var seconds = timeSpan.TotalSeconds;
|
||
_timer = this.AttachTimer((float)seconds, null,
|
||
elapsed =>
|
||
{
|
||
var now = _gatherManager.GetRemainingTime();
|
||
UpdateTimeDisplay(now);
|
||
|
||
if (now.TotalMilliseconds < 0)
|
||
{
|
||
OnCountDownFinished();
|
||
}
|
||
}, useRealTime: true);
|
||
}
|
||
|
||
private void UpdateTimeDisplay(TimeSpan timeSpan)
|
||
{
|
||
if (_textTime == null) return;
|
||
|
||
if (timeSpan.TotalMilliseconds > 0)
|
||
{
|
||
_textTime.text = ConvertTools.ConvertTime2(timeSpan.Days, timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds);
|
||
}
|
||
else
|
||
{
|
||
_textTime.text = LocalizationMgr.GetText("UI_EventRankPopupPanel_13");
|
||
}
|
||
}
|
||
|
||
private void StopCountDown()
|
||
{
|
||
_timer?.Cancel();
|
||
_timer = null;
|
||
}
|
||
|
||
private void OnCountDownFinished()
|
||
{
|
||
gameObject.SetActive(false);
|
||
StopCountDown();
|
||
}
|
||
|
||
private void OnBtnEnter()
|
||
{
|
||
Log("OnBtnEnter");
|
||
|
||
// 停止计时器
|
||
StopCountDown();
|
||
|
||
// 进入游戏场景
|
||
_gatherManager.EnterGatherScene();
|
||
|
||
// 更新红点
|
||
if (_redpoint != null)
|
||
_redpoint.SetActive(false);
|
||
}
|
||
|
||
private void OnRefreshByEvent(EventPartnerGatherRefreshEvent evt)
|
||
{
|
||
// 刷新显示
|
||
if (!_gatherManager.CheckOpen())
|
||
{
|
||
gameObject.SetActive(false);
|
||
}
|
||
}
|
||
|
||
private void OnDestroy()
|
||
{
|
||
if (_btnEnter != null)
|
||
_btnEnter.onClick.RemoveAllListeners();
|
||
StopCountDown();
|
||
_disposables?.Dispose();
|
||
_disposables = null;
|
||
}
|
||
|
||
private static void Log(object message)
|
||
{
|
||
Debug.Log($"<color=cyan>HomeBtnPartnerGather => {message}</color>");
|
||
}
|
||
} |