2023-03-28 13:16:30 -04:00
|
|
|
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;
|
|
|
|
|
2023-06-19 13:17:45 -04:00
|
|
|
[Header("\tReferences")]
|
|
|
|
[Tooltip("The Sun.")]
|
|
|
|
[SerializeField] private Light DirectionalLight;
|
|
|
|
[Tooltip("The preset to use.")]
|
2023-03-28 13:16:30 -04:00
|
|
|
[SerializeField] public SunLightPreset Preset;
|
2023-06-19 13:17:45 -04:00
|
|
|
[Tooltip("The Stars.")]
|
2023-03-28 13:16:30 -04:00
|
|
|
[SerializeField] private Transform StarsSky;
|
2023-06-19 13:17:45 -04:00
|
|
|
[Tooltip("The player.")]
|
2023-03-28 13:16:30 -04:00
|
|
|
[SerializeField] private Transform Player;
|
2023-06-19 13:17:45 -04:00
|
|
|
[Tooltip("The settings to use in an Interior cell.")]
|
2023-05-18 21:28:45 -04:00
|
|
|
[SerializeField] private LightingSettings InteriorLS;
|
2023-06-19 13:17:45 -04:00
|
|
|
[Tooltip("The settings to use in an Exterior cell.")]
|
2023-05-18 21:28:45 -04:00
|
|
|
[SerializeField] private LightingSettings ExteriorLS;
|
2023-03-28 13:16:30 -04:00
|
|
|
|
2023-06-19 13:17:45 -04:00
|
|
|
[Header("\tEnable / Disable")]
|
2023-06-21 12:09:52 -04:00
|
|
|
[SerializeField] private bool EnableDirectionalColor = true;
|
|
|
|
[SerializeField] private bool EnableFog = true;
|
|
|
|
[SerializeField] private bool EnableSunIntensity = true;
|
|
|
|
[SerializeField] private bool EnableAmbientLight = true;
|
|
|
|
[SerializeField] private bool EnableStars = true;
|
|
|
|
|
|
|
|
[Header("\tCurrent Time")]
|
|
|
|
[Tooltip("How many frames need to pass before this script runs again. (Just to save resources)")]
|
2023-06-19 13:17:45 -04:00
|
|
|
[SerializeField] private int SlowDown = 2;
|
2023-03-28 13:16:30 -04:00
|
|
|
[SerializeField] public int Year = 0;
|
|
|
|
[SerializeField] public int Month = 1;
|
|
|
|
[SerializeField] public int Day = 1;
|
|
|
|
[SerializeField, Range(0, 24)] public float TimeOfDay;
|
|
|
|
|
2023-06-21 12:09:52 -04:00
|
|
|
[Header("\tTime Settings")]
|
|
|
|
[Tooltip("How many days are in a month.")]
|
|
|
|
[SerializeField] private int MonthLength = 28;
|
|
|
|
[Tooltip("How many months are in a year.")]
|
|
|
|
[SerializeField] private int YearLength = 13;
|
|
|
|
|
2023-05-05 23:02:18 -04:00
|
|
|
private Material StarsMat;
|
|
|
|
public bool IsInterior = false;
|
2023-06-19 13:17:45 -04:00
|
|
|
private int SlowDownCount = 1;
|
2023-05-05 23:02:18 -04:00
|
|
|
|
2023-03-28 13:16:30 -04:00
|
|
|
void Start() {
|
2023-05-05 23:02:18 -04:00
|
|
|
StarsMat = null;
|
|
|
|
StarsMat = StarsSky.GetComponent<ParticleSystem>().GetComponent<Renderer>().sharedMaterial;
|
2023-03-28 13:16:30 -04:00
|
|
|
|
|
|
|
Year = SaveSystemManager.instance.saveFile.Environment.Year;
|
|
|
|
Month = SaveSystemManager.instance.saveFile.Environment.Month;
|
|
|
|
Day = SaveSystemManager.instance.saveFile.Environment.Day;
|
|
|
|
TimeOfDay = SaveSystemManager.instance.saveFile.Environment.TimeOfDay;
|
|
|
|
|
|
|
|
Loaded = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
void Update() {
|
2023-06-19 13:17:45 -04:00
|
|
|
if (SlowDownCount < SlowDown) { // this is just to slow down. makes this run every other frame, using only half as much power.
|
|
|
|
SlowDownCount += 1;
|
|
|
|
} else {
|
|
|
|
SlowDownCount = 0;
|
|
|
|
|
|
|
|
if (TimeTrigger > TimeOfDay) { // Should only trigger once a day.
|
|
|
|
Day += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeTrigger = TimeOfDay;
|
2023-03-28 13:16:30 -04:00
|
|
|
|
2023-06-21 12:09:52 -04:00
|
|
|
if (Day > MonthLength) {
|
2023-06-19 13:17:45 -04:00
|
|
|
Day = 1;
|
|
|
|
Month += 1;
|
|
|
|
}
|
2023-03-28 13:16:30 -04:00
|
|
|
|
2023-06-21 12:09:52 -04:00
|
|
|
if (Month > YearLength) {
|
2023-06-19 13:17:45 -04:00
|
|
|
Month = 1;
|
|
|
|
Year += 1;
|
|
|
|
}
|
2023-03-28 13:16:30 -04:00
|
|
|
|
2023-06-19 13:17:45 -04:00
|
|
|
if (Loaded == false) {
|
|
|
|
return;
|
|
|
|
}
|
2023-03-28 13:16:30 -04:00
|
|
|
|
2023-06-19 13:17:45 -04:00
|
|
|
if (Preset == null) {
|
|
|
|
return;
|
|
|
|
}
|
2023-03-28 13:16:30 -04:00
|
|
|
|
2023-06-19 13:17:45 -04:00
|
|
|
if (Application.isPlaying) {
|
|
|
|
TimeOfDay += (Time.deltaTime / 180f);
|
|
|
|
TimeOfDay %= 24;
|
|
|
|
float TimePercent = TimeOfDay / 24f;
|
2023-05-18 21:28:45 -04:00
|
|
|
|
2023-06-19 13:17:45 -04:00
|
|
|
//Color WorldBrightness = Preset.WorldBrightness.Evaluate(TimePercent);
|
2023-03-28 13:16:30 -04:00
|
|
|
|
2023-06-19 13:17:45 -04:00
|
|
|
if (IsInterior == false) {
|
2023-03-28 13:16:30 -04:00
|
|
|
|
2023-06-19 13:17:45 -04:00
|
|
|
// Setting the Sun
|
|
|
|
if (DirectionalLight.gameObject.activeSelf == true) {
|
|
|
|
if (RenderSettings.sun != DirectionalLight) {
|
|
|
|
RenderSettings.sun = DirectionalLight;
|
|
|
|
|
|
|
|
}
|
2023-03-28 13:16:30 -04:00
|
|
|
}
|
2023-06-19 13:17:45 -04:00
|
|
|
|
|
|
|
// Setting the Sun
|
|
|
|
if (EnableDirectionalColor) {
|
|
|
|
Color DirectionalTempColor = Preset.DirectionalColor.Evaluate(TimePercent);
|
|
|
|
DirectionalLight.color = DirectionalTempColor;
|
|
|
|
RenderSettings.skybox.SetFloat("_Exposure", (DirectionalTempColor.a * 5));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setting the Ambiance Lighting
|
|
|
|
//InteriorLS.indirectScale = WorldBrightness.a * 1.5f;
|
|
|
|
} else {
|
|
|
|
//ExteriorLS.indirectScale = WorldBrightness.a * 1.5f;
|
2023-03-28 13:16:30 -04:00
|
|
|
}
|
2023-06-19 13:17:45 -04:00
|
|
|
UpdateLighting(TimePercent);
|
2023-03-28 13:16:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void UpdateLighting(float TimePercent) {
|
2023-06-19 13:17:45 -04:00
|
|
|
if (EnableAmbientLight) {
|
|
|
|
RenderSettings.ambientLight = Preset.AmbientColor.Evaluate(TimePercent);
|
|
|
|
}
|
2023-03-28 13:16:30 -04:00
|
|
|
|
2023-04-24 18:54:43 -04:00
|
|
|
// Fog
|
2023-06-19 13:17:45 -04:00
|
|
|
if (EnableFog) {
|
|
|
|
Color FogTempColor = Preset.FogColor.Evaluate(TimePercent);
|
|
|
|
RenderSettings.fogColor = FogTempColor;
|
|
|
|
RenderSettings.fogDensity = ((FogTempColor.a / 10) * 2);
|
|
|
|
}
|
2023-04-24 18:54:43 -04:00
|
|
|
|
|
|
|
// Setting the stars
|
2023-06-19 13:17:45 -04:00
|
|
|
if (EnableStars) {
|
|
|
|
StarsSky.position = Player.position;
|
|
|
|
Color StarsColor = Preset.StarsVisability.Evaluate(TimePercent);
|
|
|
|
Color color = StarsMat.color;
|
|
|
|
color.a = StarsColor.a;
|
|
|
|
StarsMat.color = color;
|
|
|
|
}
|
2023-05-18 21:28:45 -04:00
|
|
|
|
|
|
|
// Sun Intensity (Item reflections power)
|
2023-06-19 13:17:45 -04:00
|
|
|
if (EnableSunIntensity) {
|
|
|
|
Color SunTempInt = Preset.SunIntensity.Evaluate(TimePercent);
|
|
|
|
DirectionalLight.intensity = ((SunTempInt.a) * 1.5f);
|
|
|
|
}
|
2023-04-24 18:54:43 -04:00
|
|
|
|
2023-03-28 13:16:30 -04:00
|
|
|
if (DirectionalLight != null) {
|
2023-04-24 18:54:43 -04:00
|
|
|
DirectionalLight.transform.localRotation = Quaternion.Euler(new Vector3((TimePercent * 360f) -90f, 170f, 0f));
|
2023-03-28 13:16:30 -04:00
|
|
|
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";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|