EnviroEnvironmentModule.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. namespace Enviro
  6. {
  7. [Serializable]
  8. public class EnviroEnvironment
  9. {
  10. //Seasons
  11. public enum Seasons
  12. {
  13. Spring,
  14. Summer,
  15. Autumn,
  16. Winter
  17. }
  18. public Seasons season;
  19. public bool changeSeason = true;
  20. [Tooltip("Start Day of Year for Spring")]
  21. [Range(0, 366)]
  22. public int springStart = 60;
  23. [Tooltip("End Day of Year for Spring")]
  24. [Range(0, 366)]
  25. public int springEnd = 92;
  26. [Tooltip("Start Day of Year for Summer")]
  27. [Range(0, 366)]
  28. public int summerStart = 93;
  29. [Tooltip("End Day of Year for Summer")]
  30. [Range(0, 366)]
  31. public int summerEnd = 185;
  32. [Tooltip("Start Day of Year for Autumn")]
  33. [Range(0, 366)]
  34. public int autumnStart = 186;
  35. [Tooltip("End Day of Year for Autumn")]
  36. [Range(0, 366)]
  37. public int autumnEnd = 276;
  38. [Tooltip("Start Day of Year for Winter")]
  39. [Range(0, 366)]
  40. public int winterStart = 277;
  41. [Tooltip("End Day of Year for Winter")]
  42. [Range(0, 366)]
  43. public int winterEnd = 59;
  44. //Temperature
  45. [Tooltip("Base Temperature in Spring")]
  46. public AnimationCurve springBaseTemperature = new AnimationCurve();
  47. [Tooltip("Base Temperature in Summer")]
  48. public AnimationCurve summerBaseTemperature = new AnimationCurve();
  49. [Tooltip("Base Temperature in Autumn")]
  50. public AnimationCurve autumnBaseTemperature = new AnimationCurve();
  51. [Tooltip("Base Temperature in Winter")]
  52. public AnimationCurve winterBaseTemperature = new AnimationCurve();
  53. [Tooltip("Current temperature.")]
  54. [Range(-50f, 50f)]
  55. public float temperature = 0f;
  56. [Tooltip("Temperature mod used for different weather types.")]
  57. [Range(-50f, 50f)]
  58. public float temperatureWeatherMod = 0f;
  59. [Tooltip("Custom temperature mod for gameplay use.")]
  60. [Range(-50f, 50f)]
  61. public float temperatureCustomMod = 0f;
  62. [Tooltip("Temperature changing speed.")]
  63. public float temperatureChangingSpeed = 1f;
  64. //Weather State
  65. [Tooltip("Current wetness for third party shader or gameplay.")]
  66. [Range(0f, 1f)]
  67. public float wetness = 0f;
  68. [Tooltip("Target wetness for third party shader or gameplay.")]
  69. [Range(0f, 1f)]
  70. public float wetnessTarget = 0f;
  71. [Tooltip("Current snow for third party shader or gameplay.")]
  72. [Range(0f, 1f)]
  73. public float snow = 0f;
  74. [Tooltip("Target snow for third party shader or gameplay.")]
  75. [Range(0f, 1f)]
  76. public float snowTarget = 0f;
  77. [Tooltip("Speed of wetness accumulation.")]
  78. public float wetnessAccumulationSpeed = 1f;
  79. [Tooltip("Speed of wetness dries.")]
  80. public float wetnessDrySpeed = 1f;
  81. [Tooltip("Speed of snow buildup.")]
  82. public float snowAccumulationSpeed = 1f;
  83. [Tooltip("Speed of how fast snow melts.")]
  84. public float snowMeltSpeed = 1f;
  85. [Tooltip("Temperature when snow starts to melt.")]
  86. [Range(-20f, 20f)]
  87. public float snowMeltingTresholdTemperature = 1f;
  88. }
  89. [Serializable]
  90. public class EnviroEnvironmentModule : EnviroModule
  91. {
  92. public Enviro.EnviroEnvironment Settings;
  93. public EnviroEnvironmentModule preset;
  94. public bool showSeasonControls,showTemperatureControls,showWeatherStateControls,showWindControls;
  95. // Update Method
  96. public override void UpdateModule ()
  97. {
  98. if(EnviroManager.instance.Time != null)
  99. {
  100. UpdateTemperature(EnviroManager.instance.Time.GetUniversalTimeOfDay() / 24f);
  101. UpdateSeason();
  102. }
  103. else
  104. {
  105. UpdateTemperature(1f);
  106. }
  107. UpdateWeatherState();
  108. }
  109. //Changes season based on day settings.
  110. public void UpdateSeason()
  111. {
  112. if(Settings.changeSeason)
  113. {
  114. int currentDay = EnviroManager.instance.Time.Settings.date.DayOfYear;
  115. if (currentDay >= Settings.springStart && currentDay <= Settings.springEnd)
  116. {
  117. ChangeSeason(EnviroEnvironment.Seasons.Spring);
  118. }
  119. else if (currentDay >= Settings.summerStart && currentDay <= Settings.summerEnd)
  120. {
  121. ChangeSeason(EnviroEnvironment.Seasons.Summer);
  122. }
  123. else if (currentDay >= Settings.autumnStart && currentDay <= Settings.autumnEnd)
  124. {
  125. ChangeSeason(EnviroEnvironment.Seasons.Autumn);
  126. }
  127. else if (currentDay >= Settings.winterStart || currentDay <= Settings.winterEnd)
  128. {
  129. ChangeSeason(EnviroEnvironment.Seasons.Winter);
  130. }
  131. }
  132. }
  133. //Changes Season
  134. public void ChangeSeason(EnviroEnvironment.Seasons season)
  135. {
  136. if(Settings.season != season)
  137. {
  138. EnviroManager.instance.NotifySeasonChanged(season);
  139. Settings.season = season;
  140. }
  141. }
  142. //Sets temperature based on time of day.
  143. public void UpdateTemperature (float timeOfDay)
  144. {
  145. float temperature = 0f;
  146. switch (Settings.season)
  147. {
  148. case EnviroEnvironment.Seasons.Spring:
  149. temperature = Settings.springBaseTemperature.Evaluate(timeOfDay);
  150. break;
  151. case EnviroEnvironment.Seasons.Summer:
  152. temperature = Settings.summerBaseTemperature.Evaluate(timeOfDay);
  153. break;
  154. case EnviroEnvironment.Seasons.Autumn:
  155. temperature = Settings.autumnBaseTemperature.Evaluate(timeOfDay);
  156. break;
  157. case EnviroEnvironment.Seasons.Winter:
  158. temperature = Settings.winterBaseTemperature.Evaluate(timeOfDay);
  159. break;
  160. }
  161. //Apply temperature mods
  162. temperature += Settings.temperatureWeatherMod;
  163. temperature += Settings.temperatureCustomMod;
  164. //Set temperature
  165. Settings.temperature = Mathf.Lerp(Settings.temperature, temperature, Time.deltaTime * Settings.temperatureChangingSpeed);
  166. }
  167. public void UpdateWeatherState()
  168. {
  169. // Wetness
  170. if (Settings.wetness < Settings.wetnessTarget)
  171. {
  172. // Raining
  173. Settings.wetness = Mathf.Lerp(Settings.wetness, Settings.wetnessTarget, Settings.wetnessAccumulationSpeed * Time.deltaTime);
  174. }
  175. else
  176. { // Drying
  177. Settings.wetness = Mathf.Lerp(Settings.wetness, Settings.wetnessTarget, Settings.wetnessDrySpeed * Time.deltaTime);
  178. }
  179. if(Settings.wetness < 0.0001f)
  180. Settings.wetness = 0f;
  181. Settings.wetness = Mathf.Clamp(Settings.wetness, 0f, 1f);
  182. //Snow
  183. if (Settings.snow < Settings.snowTarget)
  184. {
  185. //Snowing
  186. Settings.snow = Mathf.Lerp(Settings.snow, Settings.snowTarget, Settings.snowAccumulationSpeed * Time.deltaTime);
  187. }
  188. else if (Settings.temperature > Settings.snowMeltingTresholdTemperature)
  189. {
  190. //Melting
  191. Settings.snow = Mathf.Lerp(Settings.snow, Settings.snowTarget, Settings.snowMeltSpeed * Time.deltaTime);
  192. }
  193. if(Settings.snow < 0.0001f)
  194. Settings.snow = 0f;
  195. Settings.snow = Mathf.Clamp(Settings.snow, 0f, 1f);
  196. }
  197. //Save and Load
  198. public void LoadModuleValues ()
  199. {
  200. if(preset != null)
  201. {
  202. Settings = JsonUtility.FromJson<Enviro.EnviroEnvironment>(JsonUtility.ToJson(preset.Settings));
  203. }
  204. else
  205. {
  206. Debug.Log("Please assign a saved module to load from!");
  207. }
  208. }
  209. public void SaveModuleValues ()
  210. {
  211. #if UNITY_EDITOR
  212. EnviroEnvironmentModule t = ScriptableObject.CreateInstance<EnviroEnvironmentModule>();
  213. t.name = "Environment Preset";
  214. t.Settings = JsonUtility.FromJson<Enviro.EnviroEnvironment>(JsonUtility.ToJson(Settings));
  215. string assetPathAndName = UnityEditor.AssetDatabase.GenerateUniqueAssetPath("Assets/Enviro 3" + "/New " + t.name + ".asset");
  216. UnityEditor.AssetDatabase.CreateAsset(t, assetPathAndName);
  217. UnityEditor.AssetDatabase.SaveAssets();
  218. UnityEditor.AssetDatabase.Refresh();
  219. #endif
  220. }
  221. public void SaveModuleValues (EnviroEnvironmentModule module)
  222. {
  223. module.Settings = JsonUtility.FromJson<Enviro.EnviroEnvironment>(JsonUtility.ToJson(Settings));
  224. #if UNITY_EDITOR
  225. UnityEditor.EditorUtility.SetDirty(module);
  226. UnityEditor.AssetDatabase.SaveAssets();
  227. #endif
  228. }
  229. }
  230. }