using System;
|
using System.Collections;
|
using System.Collections.Generic;
|
using UnityEngine;
|
|
|
public class TimeMgr : SingletonMonobehaviour<TimeMgr>
|
|
{
|
private Dictionary<Component, TimeItem> timeItems = new Dictionary<Component, TimeItem>();
|
private List<TimeItem> timeItemList = new List<TimeItem>();
|
private int hourBuff = -1;
|
private int minuteBuff = -1;
|
public int dayBuff = -1;
|
public event Action OnDayEvent;
|
public event Action OnHourEvent;
|
public event Action OnMinuteEvent;
|
|
private void LateUpdate()
|
{
|
try
|
{
|
if (dayBuff != TimeUtility.ServerNow.Day)
|
{
|
if (OnDayEvent != null)
|
{
|
OnDayEvent();
|
}
|
dayBuff = TimeUtility.ServerNow.Day;
|
}
|
if (hourBuff != TimeUtility.ServerNow.Hour)
|
{
|
if (OnHourEvent != null)
|
{
|
OnHourEvent();
|
}
|
hourBuff = TimeUtility.ServerNow.Hour;
|
}
|
if (minuteBuff != TimeUtility.ServerNow.Minute)
|
{
|
if (OnMinuteEvent != null)
|
{
|
OnMinuteEvent();
|
}
|
minuteBuff = TimeUtility.ServerNow.Minute;
|
}
|
}
|
catch (Exception e)
|
{
|
DebugEx.Log(e.StackTrace);
|
}
|
for (int i = 0; i < syntonyList.Count; i++)
|
{
|
if ((TimeUtility.ServerNow - syntonyList[i].endTime).TotalSeconds > 0)
|
{
|
if (syntonyList[i].callback != null)
|
{
|
syntonyList[i].callback();
|
}
|
var _type = syntonyList[i].type;
|
syntonyList.RemoveAt(i);
|
if (OnSyntonyEvent != null)
|
{
|
OnSyntonyEvent(_type);
|
}
|
i--;
|
}
|
}
|
if (timeItems.Count > 0)
|
{
|
timeItemList.RemoveRange(0, timeItemList.Count);
|
foreach (Component item in timeItems.Keys)
|
{
|
if (item == null) continue;
|
timeItemList.Add(timeItems[item]);
|
}
|
for (int i = 0; i < timeItemList.Count; i++)
|
{
|
timeItemList[i].Update();
|
}
|
}
|
}
|
|
public void Register(Component comp, Action<Component> func, float t, int loopCnt = 1)
|
{
|
if (comp == null) return;
|
if (!timeItems.ContainsKey(comp))
|
{
|
TimeItem item = new TimeItem(t, comp, loopCnt, func);
|
timeItems.Add(comp, item);
|
}
|
}
|
|
public void SetInterval(Component comp, float t)
|
{
|
if (timeItems.ContainsKey(comp))
|
{
|
TimeItem item = timeItems[comp];
|
item.time = t;
|
}
|
}
|
|
public void UnRegister(Component comp)
|
{
|
if (comp == null) return;
|
if (timeItems.ContainsKey(comp))
|
{
|
TimeItem item = timeItems[comp];
|
timeItems.Remove(comp);
|
item = null;
|
}
|
}
|
|
public void Register(SyntonyType type, float _totalTime, Action _func = null)
|
{
|
int index;
|
if (ContainsSyntony(type, out index))
|
{
|
syntonyList[index].SetTime(_totalTime, _func);
|
}
|
else
|
{
|
Syntony syntony = new Syntony();
|
syntony.type = type;
|
syntony.SetTime(_totalTime, _func);
|
syntonyList.Add(syntony);
|
}
|
}
|
|
public void UnRegister(SyntonyType type)
|
{
|
int index;
|
if (ContainsSyntony(type, out index))
|
{
|
syntonyList.RemoveAt(index);
|
}
|
}
|
|
public struct Syntony
|
{
|
public SyntonyType type;
|
public DateTime endTime { get; private set; }
|
public Action callback;
|
public void SetTime(float _totalTime, Action _func = null)
|
{
|
endTime = TimeUtility.ServerNow.AddSeconds(_totalTime);
|
callback = _func;
|
DebugEx.LogFormat("{0}{1}", type, endTime);
|
}
|
}
|
|
private List<Syntony> syntonyList = new List<Syntony>();
|
|
public event Action<SyntonyType> OnSyntonyEvent;
|
|
private bool ContainsSyntony(SyntonyType type, out int index)
|
{
|
index = 0;
|
for (int i = 0; i < syntonyList.Count; i++)
|
{
|
if (syntonyList[i].type == type)
|
{
|
index = i;
|
return true;
|
}
|
}
|
return false;
|
}
|
|
public enum SyntonyType
|
{
|
PrayerRedpoint,
|
BossShow,
|
BossShowName,
|
BossShowDialogue,
|
BossShowSound,
|
FairyLeaugeStage,
|
VipExperirnceOverdue,
|
HeavenBattleState,
|
Realm,
|
RealmSit,
|
GuardHurtEffect,
|
GetFairyInfo,
|
Audio,
|
OSRedEnvelope,
|
AutoPlayVoice,
|
}
|
}
|