您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

271 行
7.2 KiB

  1. using System;
  2. using System.Collections;
  3. using CIGEnums;
  4. using SUISS.Core;
  5. using SUISS.Scheduling;
  6. using UnityEngine;
  7. public class CIGWeatherManager : MonoBehaviour
  8. {
  9. protected virtual void Start()
  10. {
  11. this.PrecipitationPercentage = 0f;
  12. if (this.lightningOverlay != null)
  13. {
  14. this.lightningOverlay.Brightness = 0f;
  15. }
  16. SingletonMonobehaviour<Scheduler>.Instance.StartRoutine(this._startRoutine = this.WeatherStartRoutine(), base.gameObject);
  17. }
  18. protected virtual void OnDestroy()
  19. {
  20. if (SingletonMonobehaviour<Scheduler>.IsAvailable)
  21. {
  22. this.StopPrecipitationTransition();
  23. if (this._startRoutine != null)
  24. {
  25. SingletonMonobehaviour<Scheduler>.Instance.StopRoutine(this._startRoutine);
  26. this._startRoutine = null;
  27. }
  28. if (this._weatherRoutine != null)
  29. {
  30. SingletonMonobehaviour<Scheduler>.Instance.StopRoutine(this._weatherRoutine);
  31. this._weatherRoutine = null;
  32. }
  33. if (this._waitForLightning != null)
  34. {
  35. SingletonMonobehaviour<Scheduler>.Instance.StopRoutine(this._waitForLightning);
  36. this._waitForLightning = null;
  37. }
  38. if (this._lightningRoutine != null)
  39. {
  40. SingletonMonobehaviour<Scheduler>.Instance.StopRoutine(this._lightningRoutine);
  41. this._lightningRoutine = null;
  42. }
  43. }
  44. }
  45. public bool IsRaining
  46. {
  47. get
  48. {
  49. return this.PrecipitationPercentage > 0f;
  50. }
  51. }
  52. public bool IsLightning
  53. {
  54. get
  55. {
  56. return this.PrecipitationPercentage > this.lightningTreshold;
  57. }
  58. }
  59. public float PrecipitationPercentage
  60. {
  61. get
  62. {
  63. return this._currentPrecipitation;
  64. }
  65. set
  66. {
  67. this._currentPrecipitation = Mathf.Clamp01(value);
  68. if (this.rainParticles != null)
  69. {
  70. this.rainParticles.PrecipitationPercentage = this._currentPrecipitation;
  71. }
  72. if (this.IsLightning)
  73. {
  74. if (this._lightningRoutine == null)
  75. {
  76. SingletonMonobehaviour<Scheduler>.Instance.StartRoutine(this._lightningRoutine = this.LightningRoutine(), base.gameObject);
  77. }
  78. }
  79. else if (this._waitForLightning != null)
  80. {
  81. SingletonMonobehaviour<Scheduler>.Instance.StopRoutine(this._waitForLightning);
  82. this._waitForLightning = null;
  83. }
  84. }
  85. }
  86. public virtual void TweenPrecipitationPercentage(float targetprecipitation)
  87. {
  88. this.StopPrecipitationTransition();
  89. this._transitionRoutine = this.PrecipitationTransitionRoutine(targetprecipitation, this.precipitationTransitionSpeed);
  90. SingletonMonobehaviour<Scheduler>.Instance.StartRoutine(this._transitionRoutine, base.gameObject);
  91. }
  92. public virtual void StopPrecipitationTransition()
  93. {
  94. if (this._transitionRoutine != null)
  95. {
  96. SingletonMonobehaviour<Scheduler>.Instance.StopRoutine(this._transitionRoutine);
  97. this._transitionRoutine = null;
  98. }
  99. }
  100. protected virtual IEnumerator PrecipitationTransitionRoutine(float targetprecipitation, float dpppsec)
  101. {
  102. float currentpp = this.PrecipitationPercentage;
  103. if (currentpp == targetprecipitation)
  104. {
  105. this._transitionRoutine = null;
  106. yield break;
  107. }
  108. if (currentpp > targetprecipitation)
  109. {
  110. while (this.PrecipitationPercentage > targetprecipitation)
  111. {
  112. yield return null;
  113. this.PrecipitationPercentage -= dpppsec * Time.deltaTime;
  114. }
  115. }
  116. else
  117. {
  118. while (this.PrecipitationPercentage < targetprecipitation)
  119. {
  120. yield return null;
  121. this.PrecipitationPercentage += dpppsec * Time.deltaTime;
  122. }
  123. }
  124. this.PrecipitationPercentage = targetprecipitation;
  125. this._transitionRoutine = null;
  126. yield break;
  127. }
  128. protected virtual IEnumerator WeatherStartRoutine()
  129. {
  130. while (SingletonMonobehaviour<WorldMap>.Instance.IsVisible)
  131. {
  132. yield return Timing.time + 0.5;
  133. }
  134. yield return Timing.time + 2.0;
  135. float targetprecipitation;
  136. if (UnityEngine.Random.Range(0f, 1f) <= (float)this.maxRainTime / (float)(this.maxRainTime + this.maxClearTime))
  137. {
  138. targetprecipitation = UnityEngine.Random.Range(this.minPrecipitation, this.maxPrecipitation);
  139. }
  140. else
  141. {
  142. targetprecipitation = 0f;
  143. }
  144. this.StopPrecipitationTransition();
  145. yield return this._transitionRoutine = this.PrecipitationTransitionRoutine(targetprecipitation, this.precipitationTransitionSpeed);
  146. SingletonMonobehaviour<Scheduler>.Instance.StartRoutine(this._weatherRoutine = this.WeatherRoutine(), base.gameObject);
  147. this._startRoutine = null;
  148. yield break;
  149. }
  150. protected virtual int WeatherChangeWaitTime()
  151. {
  152. if (this.IsRaining)
  153. {
  154. return UnityEngine.Random.Range(this.minRainTime, this.maxRainTime);
  155. }
  156. return UnityEngine.Random.Range(this.minClearTime, this.maxClearTime);
  157. }
  158. protected virtual int LightningDelay()
  159. {
  160. return UnityEngine.Random.Range(this.minLightningTime, this.maxLightningTime);
  161. }
  162. protected virtual IEnumerator WeatherRoutine()
  163. {
  164. for (;;)
  165. {
  166. int waittime = this.WeatherChangeWaitTime();
  167. yield return Timing.time + (double)waittime;
  168. this.StopPrecipitationTransition();
  169. float targetprecipitation;
  170. if (this.IsRaining)
  171. {
  172. targetprecipitation = 0f;
  173. }
  174. else
  175. {
  176. targetprecipitation = UnityEngine.Random.Range(this.minPrecipitation, this.maxPrecipitation);
  177. }
  178. yield return this._transitionRoutine = this.PrecipitationTransitionRoutine(targetprecipitation, this.precipitationTransitionSpeed);
  179. }
  180. yield break;
  181. }
  182. protected virtual IEnumerator WaitForLightning()
  183. {
  184. int waittime = this.LightningDelay();
  185. yield return Timing.time + (double)waittime;
  186. this._waitForLightning = null;
  187. yield break;
  188. }
  189. protected virtual IEnumerator LightningRoutine()
  190. {
  191. while (this.IsLightning)
  192. {
  193. if (this.IsLightning && this.lightningOverlay != null)
  194. {
  195. float force = UnityEngine.Random.Range(0f, 1f);
  196. this.lightningOverlay.Brightness = force;
  197. SingletonMonobehaviour<CIGAudioManager>.Instance.PlayClip(Clip.Thunder, force);
  198. float darkness = Mathf.Min(-0.5f, (this.lightningTreshold - this.PrecipitationPercentage) / (1f - this.lightningTreshold));
  199. while (this.lightningOverlay.Brightness > darkness)
  200. {
  201. yield return null;
  202. this.lightningOverlay.Brightness -= 0.1f;
  203. }
  204. this.lightningOverlay.Brightness = darkness;
  205. }
  206. yield return this._waitForLightning = this.WaitForLightning();
  207. }
  208. if (this.lightningOverlay != null)
  209. {
  210. while (this.lightningOverlay.Brightness < 0f)
  211. {
  212. yield return null;
  213. this.lightningOverlay.Brightness += 0.01f;
  214. }
  215. this.lightningOverlay.Brightness = 0f;
  216. }
  217. this._lightningRoutine = null;
  218. yield break;
  219. }
  220. public WeatherParticles rainParticles;
  221. public WeatherOverlay lightningOverlay;
  222. public float minPrecipitation = 0.5f;
  223. public float maxPrecipitation = 1f;
  224. public float precipitationTransitionSpeed = 0.2f;
  225. public float lightningTreshold = 0.8f;
  226. public int minRainTime = 60;
  227. public int maxRainTime = 300;
  228. public int minClearTime = 120;
  229. public int maxClearTime = 650;
  230. public int minLightningTime = 5;
  231. public int maxLightningTime = 30;
  232. private float _currentPrecipitation;
  233. private IEnumerator _startRoutine;
  234. private IEnumerator _weatherRoutine;
  235. private IEnumerator _lightningRoutine;
  236. private IEnumerator _waitForLightning;
  237. private IEnumerator _transitionRoutine;
  238. }