|
- using System;
- using System.Collections;
- using CIGEnums;
- using SUISS.Core;
- using SUISS.Scheduling;
- using UnityEngine;
-
- public class CIGWeatherManager : MonoBehaviour
- {
- protected virtual void Start()
- {
- this.PrecipitationPercentage = 0f;
- if (this.lightningOverlay != null)
- {
- this.lightningOverlay.Brightness = 0f;
- }
- SingletonMonobehaviour<Scheduler>.Instance.StartRoutine(this._startRoutine = this.WeatherStartRoutine(), base.gameObject);
- }
-
- protected virtual void OnDestroy()
- {
- if (SingletonMonobehaviour<Scheduler>.IsAvailable)
- {
- this.StopPrecipitationTransition();
- if (this._startRoutine != null)
- {
- SingletonMonobehaviour<Scheduler>.Instance.StopRoutine(this._startRoutine);
- this._startRoutine = null;
- }
- if (this._weatherRoutine != null)
- {
- SingletonMonobehaviour<Scheduler>.Instance.StopRoutine(this._weatherRoutine);
- this._weatherRoutine = null;
- }
- if (this._waitForLightning != null)
- {
- SingletonMonobehaviour<Scheduler>.Instance.StopRoutine(this._waitForLightning);
- this._waitForLightning = null;
- }
- if (this._lightningRoutine != null)
- {
- SingletonMonobehaviour<Scheduler>.Instance.StopRoutine(this._lightningRoutine);
- this._lightningRoutine = null;
- }
- }
- }
-
- public bool IsRaining
- {
- get
- {
- return this.PrecipitationPercentage > 0f;
- }
- }
-
- public bool IsLightning
- {
- get
- {
- return this.PrecipitationPercentage > this.lightningTreshold;
- }
- }
-
- public float PrecipitationPercentage
- {
- get
- {
- return this._currentPrecipitation;
- }
- set
- {
- this._currentPrecipitation = Mathf.Clamp01(value);
- if (this.rainParticles != null)
- {
- this.rainParticles.PrecipitationPercentage = this._currentPrecipitation;
- }
- if (this.IsLightning)
- {
- if (this._lightningRoutine == null)
- {
- SingletonMonobehaviour<Scheduler>.Instance.StartRoutine(this._lightningRoutine = this.LightningRoutine(), base.gameObject);
- }
- }
- else if (this._waitForLightning != null)
- {
- SingletonMonobehaviour<Scheduler>.Instance.StopRoutine(this._waitForLightning);
- this._waitForLightning = null;
- }
- }
- }
-
- public virtual void TweenPrecipitationPercentage(float targetprecipitation)
- {
- this.StopPrecipitationTransition();
- this._transitionRoutine = this.PrecipitationTransitionRoutine(targetprecipitation, this.precipitationTransitionSpeed);
- SingletonMonobehaviour<Scheduler>.Instance.StartRoutine(this._transitionRoutine, base.gameObject);
- }
-
- public virtual void StopPrecipitationTransition()
- {
- if (this._transitionRoutine != null)
- {
- SingletonMonobehaviour<Scheduler>.Instance.StopRoutine(this._transitionRoutine);
- this._transitionRoutine = null;
- }
- }
-
- protected virtual IEnumerator PrecipitationTransitionRoutine(float targetprecipitation, float dpppsec)
- {
- float currentpp = this.PrecipitationPercentage;
- if (currentpp == targetprecipitation)
- {
- this._transitionRoutine = null;
- yield break;
- }
- if (currentpp > targetprecipitation)
- {
- while (this.PrecipitationPercentage > targetprecipitation)
- {
- yield return null;
- this.PrecipitationPercentage -= dpppsec * Time.deltaTime;
- }
- }
- else
- {
- while (this.PrecipitationPercentage < targetprecipitation)
- {
- yield return null;
- this.PrecipitationPercentage += dpppsec * Time.deltaTime;
- }
- }
- this.PrecipitationPercentage = targetprecipitation;
- this._transitionRoutine = null;
- yield break;
- }
-
- protected virtual IEnumerator WeatherStartRoutine()
- {
- while (SingletonMonobehaviour<WorldMap>.Instance.IsVisible)
- {
- yield return Timing.time + 0.5;
- }
- yield return Timing.time + 2.0;
- float targetprecipitation;
- if (UnityEngine.Random.Range(0f, 1f) <= (float)this.maxRainTime / (float)(this.maxRainTime + this.maxClearTime))
- {
- targetprecipitation = UnityEngine.Random.Range(this.minPrecipitation, this.maxPrecipitation);
- }
- else
- {
- targetprecipitation = 0f;
- }
- this.StopPrecipitationTransition();
- yield return this._transitionRoutine = this.PrecipitationTransitionRoutine(targetprecipitation, this.precipitationTransitionSpeed);
- SingletonMonobehaviour<Scheduler>.Instance.StartRoutine(this._weatherRoutine = this.WeatherRoutine(), base.gameObject);
- this._startRoutine = null;
- yield break;
- }
-
- protected virtual int WeatherChangeWaitTime()
- {
- if (this.IsRaining)
- {
- return UnityEngine.Random.Range(this.minRainTime, this.maxRainTime);
- }
- return UnityEngine.Random.Range(this.minClearTime, this.maxClearTime);
- }
-
- protected virtual int LightningDelay()
- {
- return UnityEngine.Random.Range(this.minLightningTime, this.maxLightningTime);
- }
-
- protected virtual IEnumerator WeatherRoutine()
- {
- for (;;)
- {
- int waittime = this.WeatherChangeWaitTime();
- yield return Timing.time + (double)waittime;
- this.StopPrecipitationTransition();
- float targetprecipitation;
- if (this.IsRaining)
- {
- targetprecipitation = 0f;
- }
- else
- {
- targetprecipitation = UnityEngine.Random.Range(this.minPrecipitation, this.maxPrecipitation);
- }
- yield return this._transitionRoutine = this.PrecipitationTransitionRoutine(targetprecipitation, this.precipitationTransitionSpeed);
- }
- yield break;
- }
-
- protected virtual IEnumerator WaitForLightning()
- {
- int waittime = this.LightningDelay();
- yield return Timing.time + (double)waittime;
- this._waitForLightning = null;
- yield break;
- }
-
- protected virtual IEnumerator LightningRoutine()
- {
- while (this.IsLightning)
- {
- if (this.IsLightning && this.lightningOverlay != null)
- {
- float force = UnityEngine.Random.Range(0f, 1f);
- this.lightningOverlay.Brightness = force;
- SingletonMonobehaviour<CIGAudioManager>.Instance.PlayClip(Clip.Thunder, force);
- float darkness = Mathf.Min(-0.5f, (this.lightningTreshold - this.PrecipitationPercentage) / (1f - this.lightningTreshold));
- while (this.lightningOverlay.Brightness > darkness)
- {
- yield return null;
- this.lightningOverlay.Brightness -= 0.1f;
- }
- this.lightningOverlay.Brightness = darkness;
- }
- yield return this._waitForLightning = this.WaitForLightning();
- }
- if (this.lightningOverlay != null)
- {
- while (this.lightningOverlay.Brightness < 0f)
- {
- yield return null;
- this.lightningOverlay.Brightness += 0.01f;
- }
- this.lightningOverlay.Brightness = 0f;
- }
- this._lightningRoutine = null;
- yield break;
- }
-
- public WeatherParticles rainParticles;
-
- public WeatherOverlay lightningOverlay;
-
- public float minPrecipitation = 0.5f;
-
- public float maxPrecipitation = 1f;
-
- public float precipitationTransitionSpeed = 0.2f;
-
- public float lightningTreshold = 0.8f;
-
- public int minRainTime = 60;
-
- public int maxRainTime = 300;
-
- public int minClearTime = 120;
-
- public int maxClearTime = 650;
-
- public int minLightningTime = 5;
-
- public int maxLightningTime = 30;
-
- private float _currentPrecipitation;
-
- private IEnumerator _startRoutine;
-
- private IEnumerator _weatherRoutine;
-
- private IEnumerator _lightningRoutine;
-
- private IEnumerator _waitForLightning;
-
- private IEnumerator _transitionRoutine;
- }
|