备份CatanBuilding瘦身独立工程
This commit is contained in:
60
Assets/Scripts/EventBingo/BingoTimer.cs
Normal file
60
Assets/Scripts/EventBingo/BingoTimer.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using System;
|
||||
using UniRx;
|
||||
|
||||
public class BingoTimer : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private TMP_Text textTimer;
|
||||
private DateTime _expireTime;
|
||||
private DateTime _startTime;
|
||||
private TimeSpan RemainingTime => _expireTime - ZZTimeHelper.UtcNow();
|
||||
private Action _onExpire;
|
||||
private bool IsActive
|
||||
{
|
||||
get
|
||||
{
|
||||
var res = _expireTime > ZZTimeHelper.UtcNow();
|
||||
if (_startTime != null)
|
||||
res &= _startTime < ZZTimeHelper.UtcNow();
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public void Init(ITimerContext ctx)
|
||||
{
|
||||
_expireTime = ctx.ExpiryTime;
|
||||
_startTime = ctx.StartTime;
|
||||
_onExpire = ctx.OnExpire;
|
||||
UpdateTimer();
|
||||
Observable.Interval(TimeSpan.FromSeconds(1)).Subscribe(UpdateTimer).AddTo(this);
|
||||
}
|
||||
|
||||
private void UpdateTimer(long _ = 0L)
|
||||
{
|
||||
textTimer.text = ConvertTools.ConvertTime2(RemainingTime);
|
||||
if (!IsActive)
|
||||
{
|
||||
_onExpire?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
private void Reset()
|
||||
{
|
||||
textTimer = GetComponent<TMP_Text>();
|
||||
}
|
||||
}
|
||||
|
||||
public class TimerContext : ITimerContext
|
||||
{
|
||||
public DateTime ExpiryTime { get; set; }
|
||||
public DateTime StartTime { get; set; }
|
||||
public Action OnExpire { get; set; }
|
||||
}
|
||||
|
||||
public interface ITimerContext
|
||||
{
|
||||
public DateTime ExpiryTime { get; }
|
||||
public DateTime StartTime { get; }
|
||||
public Action OnExpire { get; }
|
||||
}
|
||||
Reference in New Issue
Block a user