Files
ft/Client/Assets/Scripts/UI/Home/HomeBtnDisco.cs
2026-06-29 21:18:33 +08:00

161 lines
4.2 KiB
C#

using System;
using System.Collections.Generic;
using asap.core;
using DataCenter;
using GameCore;
using TMPro;
using UI.OfferDiscoParty;
using UniRx;
using UnityEngine;
using UnityEngine.UI;
public class HomeBtnDisco : EventButtonResource
{
private const string EntranceRedPointKey = "DiscoParty.enter";
// 管理器
private OfferDiscoPartyManager _offerDiscoPartyManager;
#region UIElement
private Image _iconImage;
private GameObject _redpoint;
private Button _btnEnter;
private TMP_Text _textTime;
#endregion
private Timer _timer;
private CompositeDisposable _disposables = new();
#region
private void Awake()
{
_offerDiscoPartyManager = GContext.container.Resolve<OfferDiscoPartyManager>();
_iconImage = transform.Find("icon").GetComponent<Image>();
_redpoint = transform.Find("redpoint").gameObject;
_btnEnter = transform.GetComponent<Button>();
_textTime = transform.Find("text_time").GetComponent<TMP_Text>();
GContext.OnEvent<OfferDiscoPartyRefreshEvent>().Subscribe( _ => OnRefreshByEvent()).AddTo(_disposables);
}
private void Start()
{
_btnEnter.onClick.AddListener(OnBtnEnter);
}
private void OnEnable()
{
_offerDiscoPartyManager.CheckInit();
//
if (!_offerDiscoPartyManager.CheckOpen())
{
gameObject.SetActive(false);
return;
}
_redpoint.SetActive(_offerDiscoPartyManager.IsFirstOpen);
var resList = new List<string>
{
_offerDiscoPartyManager.OfferDiscoParty.Icon,
_offerDiscoPartyManager.OfferDiscoParty.UIPanel
};
CheckResource(resList);
StartCountDown();
}
private void OnDisable()
{
}
// ?
protected override void OnDestroy()
{
base.OnDestroy();
_disposables?.Dispose();
_disposables = null;
_btnEnter.onClick.RemoveAllListeners();
StopCountDown();
}
#endregion
private void OnBtnEnter()
{
Log("OnBtnEnter");
// 进入游戏场景
StartEnter();
}
private void StartEnter()
{
_timer?.Cancel();
_timer = null;
_offerDiscoPartyManager.OnEnterGameAct();
_redpoint.SetActive(_offerDiscoPartyManager.IsFirstOpen);
}
private void OnRefreshByEvent()
{
if (_offerDiscoPartyManager.IsFinished())
{
gameObject.SetActive(false);
}
}
private void StopCountDown()
{
_timer?.Cancel();
_timer = null;
}
private void StartCountDown()
{
StopCountDown();
var timeSpan = _offerDiscoPartyManager.GetTimeRemain();
_textTime.text = ConvertTools.ConvertTime2(timeSpan.Days, timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds);
var seconds = timeSpan.TotalSeconds;
_timer = this.AttachTimer((float)seconds, null,
elapsed =>
{
var now = _offerDiscoPartyManager.GetTimeRemain();
_textTime.text = ConvertTools.ConvertTime2(now.Days, now.Hours, now.Minutes, now.Seconds);
if (now.TotalMilliseconds < 0)
{
OnCountDownFinished();
_textTime.text = LocalizationMgr.GetText("UI_EventRankPopupPanel_13");
}
}, useRealTime: true);
}
private void OnCountDownFinished()
{
gameObject.SetActive(false);
StopCountDown();
_offerDiscoPartyManager.Settle();
}
private static void Log(string message)
{
Debug.Log($"<color=orange>{nameof(HomeBtnDisco)} => {message} </color>");
}
#region
protected override void OnLoadEventResource()
{
Log("OnLoadEventResource");
if (_offerDiscoPartyManager.CheckOpen())
{
gameObject.SetActive(true);
var iconName = _offerDiscoPartyManager.OfferDiscoParty.Icon;
GContext.container.Resolve<IUIService>().SetImageSprite(_iconImage, iconName);
}
}
#endregion
// 刷新事件
}