60 lines
1.4 KiB
C#
60 lines
1.4 KiB
C#
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; }
|
|
} |