IndieGame/client/Assets/Scripts/System/Time/TimeSystem.cs

72 lines
1.8 KiB
C#
Raw Normal View History

2024-10-11 10:12:15 +08:00

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityTimer;
namespace Ether
{
public struct TimeData
{
public int year;
public int month;
public int day;
public int hour;
public int minute;
public int second;
}
public class TimeSystem : SingletonAutoMono<TimeSystem>
{
[Save]
int curTimeSpan = 31104000;
public long CurTimeSpan => curTimeSpan;
Timer timer;
private ActionEvent onTickEvent = new ActionEvent();
public ActionEvent OnTickEvent
{
get { return onTickEvent; }
}
public override void Init()
{
timer?.Cancel();
SaveSystem.Inst.Regist(this);
timer = Timer.Register(1, () =>
{
curTimeSpan++;
OnTickEvent?.Invoke();
}, isLooped: true, autoDestroyOwner: this);
}
public TimeData SpanConvertToData(int span)
{
TimeData data = new TimeData();
data.year = span / 518400;
span = span - data.year * 518400;
data.month = span / 43200;
span = span - data.month * 43200;
data.day = span / 1440;
span = span - data.day * 1440;
data.hour = span / 24;
span = span - data.day * 24;
data.minute = span % 60;
return data;
}
public int DataCovertToSpan(TimeData data)
{
return data.year * 518400 + data.month * 43200 + data.day * 1440 + data.hour * 60 + data.minute;
}
public override void Clear()
{
timer?.Cancel();
SaveSystem.Inst.UnRegist(this);
}
}
}