95 lines
3.5 KiB
C#
95 lines
3.5 KiB
C#
using UnityEngine;
|
||
// using UniTask;
|
||
|
||
namespace BrewMonster.Scripts
|
||
{
|
||
public class CECSunMoon : MonoSingleton<CECSunMoon>
|
||
{
|
||
private const float TIME_SCALE = 6f;
|
||
private const float NIGHT_DAY_START = (3.0f / 24.0f);
|
||
private const float NIGHT_SUN_RISE_MIN = (4.0f / 24.0f);
|
||
private const float NIGHT_DAY_END = (7.0f / 24.0f);
|
||
private const float DAY_NIGHT_START = (18.0f / 24.0f);
|
||
private const float DAY_SUN_SET_MAX = (19.5f / 24.0f);
|
||
private const float DAY_NIGHT_END = (21.0f / 24.0f);
|
||
|
||
public double m_vTimeOfTheDay; // time of the day 0.0f means 00:00, 1.0f means 24:00
|
||
public float m_fDNFactor; // day or night factor
|
||
public float m_fDNFactorDest; // day or night factor dest
|
||
|
||
[SerializeField] GlobalShaderVariableSetting _globalShaderVariables;
|
||
|
||
protected override void Initialize()
|
||
{
|
||
base.Initialize();
|
||
|
||
// keep this alive when scene is loaded
|
||
DontDestroyOnLoad(gameObject);
|
||
}
|
||
|
||
public void Update()
|
||
{
|
||
m_vTimeOfTheDay += Time.deltaTime / 3600.0 / 24.0 * TIME_SCALE;
|
||
while (m_vTimeOfTheDay > 1.0)
|
||
m_vTimeOfTheDay -= 1.0;
|
||
RefreshDayNightFactorsFromPhase();
|
||
}
|
||
|
||
public float GetTimeOfTheDay() => (float)m_vTimeOfTheDay;
|
||
public float GetDNFactor() => m_fDNFactor;
|
||
public float GetDNFactorDest() => m_fDNFactorDest;
|
||
|
||
/// <summary>
|
||
/// 设置一天中的时刻(与 PC 版 CECSunMoon::SetTimeOfTheDay 对齐) // Set time-of-day phase [0,1), aligned with PC CECSunMoon::SetTimeOfTheDay
|
||
/// </summary>
|
||
public bool SetTimeOfTheDay(float vTime)
|
||
{
|
||
while (vTime < 0f)
|
||
vTime += 1f;
|
||
while (vTime > 1f)
|
||
vTime -= 1f;
|
||
m_vTimeOfTheDay = vTime;
|
||
RefreshDayNightFactorsFromPhase();
|
||
return true;
|
||
}
|
||
|
||
/// <summary>
|
||
/// PC EC_SunMoon::UpdateWithTime 中昼夜因子段(供 minimap 与场景一致) // DN factor block from PC EC_SunMoon::UpdateWithTime
|
||
/// </summary>
|
||
void RefreshDayNightFactorsFromPhase()
|
||
{
|
||
float v = (float)m_vTimeOfTheDay;
|
||
|
||
// update day night factor — same branches as PC EC_SunMoon.cpp UpdateWithTime (lines 894–918)
|
||
if (v < NIGHT_DAY_START)
|
||
{
|
||
m_fDNFactor = 1.0f;
|
||
m_fDNFactorDest = 1.0f;
|
||
}
|
||
else if (v < NIGHT_DAY_END)
|
||
{
|
||
m_fDNFactor = 1.0f - (v - NIGHT_DAY_START) / (NIGHT_DAY_END - NIGHT_DAY_START);
|
||
m_fDNFactorDest = 0.0f;
|
||
}
|
||
else if (m_vTimeOfTheDay < DAY_NIGHT_START)
|
||
{
|
||
// PC 使用 m_vTimeOfTheDay 与 DAY_NIGHT_START 比较 // PC compares m_vTimeOfTheDay to DAY_NIGHT_START (see EC_SunMoon.cpp)
|
||
m_fDNFactor = 0.0f;
|
||
m_fDNFactorDest = 0.0f;
|
||
}
|
||
else if (v < DAY_NIGHT_END)
|
||
{
|
||
m_fDNFactor = (v - DAY_NIGHT_START) / (DAY_NIGHT_END - DAY_NIGHT_START);
|
||
m_fDNFactorDest = 1.0f;
|
||
}
|
||
else
|
||
{
|
||
m_fDNFactor = 1.0f;
|
||
m_fDNFactorDest = 1.0f;
|
||
}
|
||
|
||
_globalShaderVariables?.Apply(m_fDNFactor);
|
||
}
|
||
}
|
||
}
|