using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TimeManager : MonoBehaviour {
public static TOD_Sky sky;
public static TOD_Time time;
public static event Action OnMinute;
public static event Action OnHour;
public static event Action OnHour2;
public static event Action OnDay;
public static event Action OnWeek;
public static event Action OnMonth;
public static event Action OnSeason;
public static event Action OnYear;
public static event Action OnSunrise;
public static event Action OnSunset;
/// <summary>
/// 当前时辰
/// </summary>
public static int curHour2;
static string[] Hour2Names = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };
/// <summary>
/// 当前的季度
/// </summary>
public static int curSeason;
static string[] SeasonNames = { "春", "夏", "秋", "冬" };
/// <summary>
/// 当前年的第几周
/// </summary>
public static int curWeekOfYear;
// Use this for initialization
void Awake()
{
sky = GetComponentInChildren<TOD_Sky>();
time = GetComponentInChildren<TOD_Time>();
curHour2 = ConvertToHour2(sky.Cycle.DateTime.Hour);
curSeason = ConvertToSeason(sky.Cycle.DateTime.Month);
curWeekOfYear = ConvertToWeekOfYear(sky.Cycle.DateTime.DayOfYear);
Register();
}
void Register()
{
time.OnDay += () =>
{
InvokeAction(OnDay);
var newWeekOfYear = ConvertToWeekOfYear(sky.Cycle.DateTime.DayOfYear);
InvokeWhenNotEquals(OnWeek, ref newWeekOfYear, ref curWeekOfYear);
};
time.OnHour += () =>
{
InvokeAction(OnHour);
var newh2 = ConvertToHour2(sky.Cycle.DateTime.Hour);
InvokeWhenNotEquals(OnHour2, ref newh2, ref curHour2);
};
time.OnMinute += () => InvokeAction(OnMinute);
time.OnMonth += () =>
{
InvokeAction(OnMonth);
var newSeason = ConvertToSeason(sky.Cycle.Month);
InvokeWhenNotEquals(OnSeason, ref newSeason, ref curSeason);
};
time.OnYear += () => InvokeAction(OnYear);
time.OnSunrise += () => InvokeAction(OnSunrise);
time.OnSunset += () => InvokeAction(OnSunset);
}
void InvokeAction(Action act)
{
if (act != null)
act();
}
void InvokeWhenNotEquals<T>(Action act,ref T newValue,ref T curValue)
{
if(!newValue.Equals(curValue))
{
curValue = newValue;
InvokeAction(act);
}
}
/// <summary>
/// 小时转时辰
/// </summary>
/// <param name="hour"></param>
/// <returns></returns>
int ConvertToHour2(int hour)
{
var h2 = Mathf.CeilToInt(hour / 2f);
return h2 % 12;
}
/// <summary>
/// 月份转季度
/// </summary>
/// <param name="m"></param>
/// <returns></returns>
int ConvertToSeason(int m)
{
m -= 3;
if (m < 0) m += 12;
return (m / 3) % 4;
}
int ConvertToWeekOfYear(int dayOfYear)
{
return Mathf.CeilToInt(sky.Cycle.DateTime.DayOfYear / 7.0f);
}
/// <summary>
/// 获取当前时辰
/// </summary>
/// <returns></returns>
public static string GetHour2Name()
{
return Hour2Names[curHour2];
}
/// <summary>
/// 获取当前的季节
/// </summary>
/// <returns></returns>
public static string GetSeasonName()
{
return SeasonNames[curSeason];
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TimeManager : MonoBehaviour {
public static TOD_Sky sky;
public static TOD_Time time;
public static event Action OnMinute;
public static event Action OnHour;
public static event Action OnHour2;
public static event Action OnDay;
public static event Action OnWeek;
public static event Action OnMonth;
public static event Action OnSeason;
public static event Action OnYear;
public static event Action OnSunrise;
public static event Action OnSunset;
/// <summary>
/// 当前时辰
/// </summary>
public static int curHour2;
static string[] Hour2Names = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };
/// <summary>
/// 当前的季度
/// </summary>
public static int curSeason;
static string[] SeasonNames = { "春", "夏", "秋", "冬" };
/// <summary>
/// 当前年的第几周
/// </summary>
public static int curWeekOfYear;
// Use this for initialization
void Awake()
{
sky = GetComponentInChildren<TOD_Sky>();
time = GetComponentInChildren<TOD_Time>();
curHour2 = ConvertToHour2(sky.Cycle.DateTime.Hour);
curSeason = ConvertToSeason(sky.Cycle.DateTime.Month);
curWeekOfYear = ConvertToWeekOfYear(sky.Cycle.DateTime.DayOfYear);
Register();
}
void Register()
{
time.OnDay += () =>
{
InvokeAction(OnDay);
var newWeekOfYear = ConvertToWeekOfYear(sky.Cycle.DateTime.DayOfYear);
InvokeWhenNotEquals(OnWeek, ref newWeekOfYear, ref curWeekOfYear);
};
time.OnHour += () =>
{
InvokeAction(OnHour);
var newh2 = ConvertToHour2(sky.Cycle.DateTime.Hour);
InvokeWhenNotEquals(OnHour2, ref newh2, ref curHour2);
};
time.OnMinute += () => InvokeAction(OnMinute);
time.OnMonth += () =>
{
InvokeAction(OnMonth);
var newSeason = ConvertToSeason(sky.Cycle.Month);
InvokeWhenNotEquals(OnSeason, ref newSeason, ref curSeason);
};
time.OnYear += () => InvokeAction(OnYear);
time.OnSunrise += () => InvokeAction(OnSunrise);
time.OnSunset += () => InvokeAction(OnSunset);
}
void InvokeAction(Action act)
{
if (act != null)
act();
}
void InvokeWhenNotEquals<T>(Action act,ref T newValue,ref T curValue)
{
if(!newValue.Equals(curValue))
{
curValue = newValue;
InvokeAction(act);
}
}
/// <summary>
/// 小时转时辰
/// </summary>
/// <param name="hour"></param>
/// <returns></returns>
int ConvertToHour2(int hour)
{
var h2 = Mathf.CeilToInt(hour / 2f);
return h2 % 12;
}
/// <summary>
/// 月份转季度
/// </summary>
/// <param name="m"></param>
/// <returns></returns>
int ConvertToSeason(int m)
{
m -= 3;
if (m < 0) m += 12;
return (m / 3) % 4;
}
int ConvertToWeekOfYear(int dayOfYear)
{
return Mathf.CeilToInt(sky.Cycle.DateTime.DayOfYear / 7.0f);
}
/// <summary>
/// 获取当前时辰
/// </summary>
/// <returns></returns>
public static string GetHour2Name()
{
return Hour2Names[curHour2];
}
/// <summary>
/// 获取当前的季节
/// </summary>
/// <returns></returns>
public static string GetSeasonName()
{
return SeasonNames[curSeason];
}
}