WeatherController.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace Funly.SkyStudio
  5. {
  6. // Manages the interfacing the various weather controller and updating state.
  7. public class WeatherController : MonoBehaviour
  8. {
  9. public RainDownfallController rainDownfallController { get; protected set; }
  10. public RainSplashController rainSplashController { get; protected set; }
  11. public LightningController lightningController { get; protected set; }
  12. public WeatherDepthCamera weatherDepthCamera { get; protected set; }
  13. WeatherEnclosure m_Enclosure;
  14. MeshRenderer m_EnclosureMeshRenderer;
  15. WeatherEnclosureDetector detector;
  16. SkyProfile m_Profile;
  17. float m_TimeOfDay;
  18. private void Awake()
  19. {
  20. DiscoverWeatherControllers();
  21. }
  22. private void Start()
  23. {
  24. DiscoverWeatherControllers();
  25. }
  26. private void OnEnable()
  27. {
  28. DiscoverWeatherControllers();
  29. if (detector == null) {
  30. Debug.LogError("Can't register for enclosure callbacks since there's no WeatherEnclosureDetector on any children");
  31. return;
  32. }
  33. detector.enclosureChangedCallback += OnEnclosureDidChange;
  34. }
  35. void DiscoverWeatherControllers()
  36. {
  37. rainDownfallController = GetComponentInChildren<RainDownfallController>();
  38. rainSplashController = GetComponentInChildren<RainSplashController>();
  39. lightningController = GetComponentInChildren<LightningController>();
  40. weatherDepthCamera = GetComponentInChildren<WeatherDepthCamera>();
  41. detector = GetComponentInChildren<WeatherEnclosureDetector>();
  42. }
  43. private void OnDisable()
  44. {
  45. if (detector == null) {
  46. return;
  47. }
  48. detector.enclosureChangedCallback -= OnEnclosureDidChange;
  49. }
  50. // Update all of the weather systems.
  51. public void UpdateForTimeOfDay(SkyProfile skyProfile, float timeOfDay)
  52. {
  53. if (!skyProfile) {
  54. return;
  55. }
  56. m_Profile = skyProfile;
  57. m_TimeOfDay = timeOfDay;
  58. // Update all the controllers state.
  59. if (weatherDepthCamera != null) {
  60. weatherDepthCamera.enabled = skyProfile.IsFeatureEnabled(ProfileFeatureKeys.RainSplashFeature);
  61. }
  62. if (rainDownfallController != null) {
  63. rainDownfallController.UpdateForTimeOfDay(skyProfile, timeOfDay);
  64. }
  65. if (rainSplashController != null) {
  66. rainSplashController.UpdateForTimeOfDay(skyProfile, timeOfDay);
  67. }
  68. if (lightningController != null) {
  69. lightningController.UpdateForTimeOfDay(skyProfile, timeOfDay);
  70. }
  71. }
  72. private void LateUpdate()
  73. {
  74. if (m_Profile == null) {
  75. return;
  76. }
  77. if (m_EnclosureMeshRenderer && rainDownfallController && m_Profile.IsFeatureEnabled(ProfileFeatureKeys.RainFeature)) {
  78. m_EnclosureMeshRenderer.enabled = true;
  79. } else {
  80. m_EnclosureMeshRenderer.enabled = false;
  81. }
  82. }
  83. private void OnEnclosureDidChange(WeatherEnclosure enclosure)
  84. {
  85. m_Enclosure = enclosure;
  86. if (m_Enclosure != null) {
  87. m_EnclosureMeshRenderer = m_Enclosure.GetComponentInChildren<MeshRenderer>();
  88. }
  89. rainDownfallController.SetWeatherEnclosure(m_Enclosure);
  90. UpdateForTimeOfDay(m_Profile, m_TimeOfDay);
  91. }
  92. }
  93. }