First Commit
This commit is contained in:
commit
eed6b79efc
2836 files changed
+948481
No files matched your search
@@ -0,0 +1,179 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// Смена дня и ночи: 1 реальная минута = 1 игровой час (настраивается). Солнце вращается по времени суток,
|
||||
/// ночью включаются холодный лунный свет, темно-синее небо и уличные фонари. Часы и день — на HUD.
|
||||
/// </summary>
|
||||
public class TimeAndWeatherManager : MonoBehaviour
|
||||
{
|
||||
public static TimeAndWeatherManager Instance { get; private set; }
|
||||
|
||||
[Header("Время")]
|
||||
[Tooltip("Игровых минут за одну реальную секунду (1 = 1 реальная минута на игровой час)")]
|
||||
[SerializeField] private float gameMinutesPerRealSecond = 1f;
|
||||
[SerializeField] private float startHour = 9f;
|
||||
|
||||
[Header("Освещение")]
|
||||
public Light sun;
|
||||
[SerializeField] private Color dayLightColor = new Color(1f, 0.96f, 0.88f);
|
||||
[SerializeField] private Color sunsetColor = new Color(1f, 0.55f, 0.3f);
|
||||
[SerializeField] private float dayIntensity = 1.25f;
|
||||
[SerializeField] private Color moonColor = new Color(0.55f, 0.65f, 1f);
|
||||
[SerializeField] private float moonIntensity = 0.18f;
|
||||
|
||||
[Header("Небо и окружение")]
|
||||
[SerializeField] private Color daySky = new Color(0.55f, 0.66f, 0.82f);
|
||||
[SerializeField] private Color dayEquator = new Color(0.52f, 0.54f, 0.52f);
|
||||
[SerializeField] private Color dayGround = new Color(0.25f, 0.24f, 0.22f);
|
||||
[SerializeField] private Color nightSky = new Color(0.04f, 0.06f, 0.14f);
|
||||
[SerializeField] private Color nightEquator = new Color(0.04f, 0.05f, 0.09f);
|
||||
[SerializeField] private Color nightGround = new Color(0.02f, 0.02f, 0.03f);
|
||||
|
||||
public float TimeOfDayHours { get; private set; }
|
||||
public int Day { get; private set; } = 1;
|
||||
public bool IsNight => TimeOfDayHours >= 20f || TimeOfDayHours < 6f;
|
||||
public string ClockText
|
||||
{
|
||||
get
|
||||
{
|
||||
int h = Mathf.FloorToInt(TimeOfDayHours);
|
||||
int m = Mathf.FloorToInt((TimeOfDayHours - h) * 60f);
|
||||
return $"{h:00}:{m:00}";
|
||||
}
|
||||
}
|
||||
|
||||
public event Action<int> OnNewDay;
|
||||
|
||||
private Light moon;
|
||||
private bool lampsOn;
|
||||
private Material skyMat;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
Instance = this;
|
||||
TimeOfDayHours = startHour;
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (Instance == this) Instance = null;
|
||||
if (skyMat != null) Destroy(skyMat);
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
if (sun == null) sun = RenderSettings.sun;
|
||||
if (sun == null)
|
||||
{
|
||||
foreach (Light l in FindObjectsByType<Light>(FindObjectsSortMode.None))
|
||||
if (l.type == LightType.Directional) { sun = l; break; }
|
||||
}
|
||||
|
||||
GameObject moonObj = new GameObject("Moonlight");
|
||||
moonObj.transform.SetParent(transform, false);
|
||||
moon = moonObj.AddComponent<Light>();
|
||||
moon.type = LightType.Directional;
|
||||
moon.color = moonColor;
|
||||
moon.shadows = LightShadows.Soft;
|
||||
moon.shadowStrength = 0.5f;
|
||||
moon.enabled = false;
|
||||
|
||||
RenderSettings.ambientMode = UnityEngine.Rendering.AmbientMode.Trilight;
|
||||
if (RenderSettings.skybox != null)
|
||||
{
|
||||
skyMat = new Material(RenderSettings.skybox);
|
||||
RenderSettings.skybox = skyMat;
|
||||
}
|
||||
|
||||
lampsOn = !IsNight;
|
||||
ApplyLighting();
|
||||
}
|
||||
|
||||
public void SetTime(float hours)
|
||||
{
|
||||
TimeOfDayHours = Mathf.Repeat(hours, 24f);
|
||||
ApplyLighting();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
MainMenuAndBootManager menu = MainMenuAndBootManager.Instance;
|
||||
if (menu == null || menu.IsPlaying)
|
||||
{
|
||||
TimeOfDayHours += Time.deltaTime * gameMinutesPerRealSecond / 60f;
|
||||
if (TimeOfDayHours >= 24f)
|
||||
{
|
||||
TimeOfDayHours -= 24f;
|
||||
Day++;
|
||||
OnNewDay?.Invoke(Day);
|
||||
RefreshMarketForNewDay();
|
||||
}
|
||||
}
|
||||
ApplyLighting();
|
||||
}
|
||||
|
||||
private void RefreshMarketForNewDay()
|
||||
{
|
||||
CarMarketManager market = FindAnyObjectByType<CarMarketManager>();
|
||||
if (market != null && market.ActiveMeetingListing == null)
|
||||
{
|
||||
market.GenerateListings();
|
||||
GameHUD.Notify($"День {Day}: на авторынке появились новые объявления [M]", GameUI.Blue);
|
||||
}
|
||||
}
|
||||
|
||||
private void ApplyLighting()
|
||||
{
|
||||
float t = TimeOfDayHours / 24f;
|
||||
float sunAngle = t * 360f - 90f; // 06:00 — горизонт, 12:00 — зенит
|
||||
float altitude = Mathf.Sin(sunAngle * Mathf.Deg2Rad);
|
||||
float day = Mathf.SmoothStep(0f, 1f, Mathf.InverseLerp(-0.08f, 0.25f, altitude));
|
||||
|
||||
if (sun != null)
|
||||
{
|
||||
sun.transform.rotation = Quaternion.Euler(sunAngle, 160f, 0f);
|
||||
sun.intensity = dayIntensity * day;
|
||||
sun.color = Color.Lerp(sunsetColor, dayLightColor, Mathf.InverseLerp(0.05f, 0.5f, altitude));
|
||||
sun.enabled = day > 0.01f;
|
||||
sun.shadowStrength = Mathf.Lerp(0.4f, 0.85f, day);
|
||||
}
|
||||
|
||||
if (moon != null)
|
||||
{
|
||||
moon.transform.rotation = Quaternion.Euler(sunAngle + 180f, 200f, 0f);
|
||||
moon.intensity = moonIntensity * (1f - day);
|
||||
moon.enabled = day < 0.95f;
|
||||
}
|
||||
|
||||
Color sky = Color.Lerp(nightSky, daySky, day);
|
||||
Color equator = Color.Lerp(nightEquator, dayEquator, day);
|
||||
Color groundC = Color.Lerp(nightGround, dayGround, day);
|
||||
RenderSettings.ambientSkyColor = sky;
|
||||
RenderSettings.ambientEquatorColor = equator;
|
||||
RenderSettings.ambientGroundColor = groundC;
|
||||
|
||||
// URP берет окружающий свет из ambientProbe — задаем его напрямую, чтобы ночь действительно темнела
|
||||
UnityEngine.Rendering.SphericalHarmonicsL2 sh = new UnityEngine.Rendering.SphericalHarmonicsL2();
|
||||
sh.AddAmbientLight(equator);
|
||||
sh.AddDirectionalLight(Vector3.up, sky - equator * 0.5f, 0.6f);
|
||||
sh.AddDirectionalLight(Vector3.down, groundC, 0.3f);
|
||||
RenderSettings.ambientProbe = sh;
|
||||
RenderSettings.reflectionIntensity = Mathf.Lerp(0.12f, 1f, day);
|
||||
RenderSettings.fogColor = Color.Lerp(new Color(0.03f, 0.04f, 0.08f), new Color(0.62f, 0.68f, 0.76f), day);
|
||||
|
||||
if (skyMat != null)
|
||||
{
|
||||
if (skyMat.HasProperty("_Exposure")) skyMat.SetFloat("_Exposure", Mathf.Lerp(0.12f, 1.25f, day));
|
||||
if (skyMat.HasProperty("_SkyTint")) skyMat.SetColor("_SkyTint", Color.Lerp(new Color(0.12f, 0.15f, 0.35f), new Color(0.5f, 0.5f, 0.5f), day));
|
||||
if (skyMat.HasProperty("_AtmosphereThickness")) skyMat.SetFloat("_AtmosphereThickness", Mathf.Lerp(0.6f, 1f, day));
|
||||
}
|
||||
|
||||
bool night = IsNight || day < 0.2f;
|
||||
if (night != lampsOn)
|
||||
{
|
||||
lampsOn = night;
|
||||
StreetLamp.SetAll(night);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user