using UnityEngine; using RPGCreationKit; using RPGCreationKit.SaveSystem; namespace RPGCreationKit { [ExecuteAlways] public class SunLightManager : MonoBehaviour { public static SunLightManager instance; private bool Loaded = false; private float TimeTrigger = 0f; [Header("References")] [SerializeField] private Light DirectionalLight; [SerializeField] public SunLightPreset Preset; [SerializeField] private Transform StarsSky; [SerializeField] private Transform Player; [SerializeField] private Material StarsMat; public bool IsInterior = false; [Header("Time")] [SerializeField] public int Year = 0; [SerializeField] public int Month = 1; [SerializeField] public int Day = 1; [SerializeField, Range(0, 24)] public float TimeOfDay; [Header("Fog")] [SerializeField] public float InteriorFog = 0f; [SerializeField] private float MinDensity = 0.0001f; [SerializeField] private float MaxDensity = 0.1f; [SerializeField] private float DensitySpeed = 0.1f; [SerializeField, Range(1000f, 0.1f)] public float Density = 1000f; [Header("Sky")] [SerializeField] private float MinExposure = 1.0f; [SerializeField] private float MaxExposure = 5.04f; [SerializeField] private float ExposureSpeed = 0.01f; [SerializeField, Range(5.05f, 1.0f)] public float Exposure = 1.0f; [Header("Stars")] [SerializeField] private float MinStars = 0.0001f; [SerializeField] private float MaxStars = 0.5f; [SerializeField] private float StarsSpeed = 0.0001f; [SerializeField, Range(0.0001f, 0.5f)] public float Stars = 0.5f; void Start() { StarsMat = StarsSky.GetComponent().GetComponent().sharedMaterial; Year = SaveSystemManager.instance.saveFile.Environment.Year; Month = SaveSystemManager.instance.saveFile.Environment.Month; Day = SaveSystemManager.instance.saveFile.Environment.Day; TimeOfDay = SaveSystemManager.instance.saveFile.Environment.TimeOfDay; Density = SaveSystemManager.instance.saveFile.Environment.Density; Exposure = SaveSystemManager.instance.saveFile.Environment.Exposure; Stars = SaveSystemManager.instance.saveFile.Environment.Stars; Loaded = true; } // Update is called once per frame void Update() { if (TimeTrigger > TimeOfDay) { // Should only trigger once a day. Day += 1; } TimeTrigger = TimeOfDay; if (Day > 28) { Day = 1; Month += 1; } if (Month > 13) { Month = 1; Year += 1; } if (Loaded == false) { return; } if (Preset == null) { return; } if (Application.isPlaying) { TimeOfDay += (Time.deltaTime / 180f); TimeOfDay %= 24; UpdateLighting(TimeOfDay / 24f); if (IsInterior == false) { // Setting the Fog if (((TimeOfDay > 6.5f) && (TimeOfDay < 12f)) && ((Density / 10000f) > MinDensity)) { Density -= (DensitySpeed); RenderSettings.fogDensity = (Density / 10000f); } else if (((TimeOfDay > 3f) && (TimeOfDay < 6.4f)) && ((Density / 10000f) < MaxDensity)) { Density += (DensitySpeed); RenderSettings.fogDensity = (Density / 10000f); } else { // In case we are in a dead zone, catch fog up to where we went it. if (RenderSettings.fogDensity != (Density / 10000f)) { RenderSettings.fogDensity = (Density / 10000f); } } // Setting the Sun if (DirectionalLight.gameObject.activeSelf == true) { if (RenderSettings.sun != DirectionalLight) { RenderSettings.sun = DirectionalLight; } if (((TimeOfDay > 6.5f) && (TimeOfDay < 15f)) && (Exposure < MaxExposure)) { Exposure += (ExposureSpeed); RenderSettings.skybox.SetFloat("_Exposure", Exposure); } else if (((TimeOfDay > 15f) && (TimeOfDay < 20f)) && (Exposure > MinExposure)) { Exposure -= (ExposureSpeed); RenderSettings.skybox.SetFloat("_Exposure", Exposure); } else { // In case we are in a dead zone, lets catch up if (RenderSettings.skybox.GetFloat("_Exposure") != Exposure) { RenderSettings.skybox.SetFloat("_Exposure", Exposure); } } } // Setting the stars Color color = StarsMat.color; StarsSky.position = Player.position; if (((TimeOfDay > 5f) && (TimeOfDay < 9f)) && (Stars > MinStars)) { Stars -= (StarsSpeed); color.a = Stars; StarsMat.color = color; } else if (((TimeOfDay > 18f) && (TimeOfDay < 21f)) && (Stars < MaxStars)) { Stars += (StarsSpeed); color.a = Stars; StarsMat.color = color; } else { // In case we are in a dead zone, lets catch up if (StarsMat.color != color) { color.a = Stars; StarsMat.color = color; } } } else { // Remove the stars instantly if (StarsMat.color.a != 0) { Color color = StarsMat.color; Stars = MinStars; color.a = Stars; StarsMat.color = color; } if (Density != InteriorFog) { Density = InteriorFog; RenderSettings.fogDensity = (Density / 10000f); } } } else { UpdateLighting(TimeOfDay / 24f); } } private void UpdateLighting(float TimePercent) { RenderSettings.ambientLight = Preset.AmbientColor.Evaluate(TimePercent); RenderSettings.fogColor = Preset.FogColor.Evaluate(TimePercent); if (DirectionalLight != null) { DirectionalLight.color = Preset.DirectionalColor.Evaluate(TimePercent); DirectionalLight.transform.localRotation = Quaternion.Euler(new Vector3((TimePercent * 360f) -90f, -100, 40)); StarsSky.localRotation = Quaternion.Euler(new Vector3((TimePercent * 360f) -90f, -100, 40)); } } public string GetMonth(int A) { // Ari, Tau, Gen, Can, Le, Ver, Lib, Sco, Sol, Sag, Cap, Aua, Pis. if (A == 0) { return "Aries"; } else if (A == 1) { return "Taurus"; } else if (A == 2) { return "Gemini"; } else if (A == 3) { return "Cancer"; } else if (A == 4) { return "Leo"; } else if (A == 5) { return "Virgo"; } else if (A == 6) { return "Libra"; } else if (A == 7) { return "Scorpio"; } else if (A == 8) { return "Sol"; } else if (A == 9) { return "Sagittarius"; } else if (A == 10) { return "Capricorn"; } else if (A == 11) { return "Aquarius"; } else { return "Pisces"; } } } }