EnviroZone.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. namespace Enviro
  6. {
  7. [Serializable]
  8. public class EnviroZoneWeather
  9. {
  10. public bool showEditor;
  11. public EnviroWeatherType weatherType;
  12. public float probability;
  13. }
  14. [AddComponentMenu("Enviro 3/Weather Zone")]
  15. public class EnviroZone : MonoBehaviour
  16. {
  17. public EnviroWeatherType currentWeatherType;
  18. public EnviroWeatherType nextWeatherType;
  19. public bool autoWeatherChanges = true;
  20. public float weatherChangeIntervall = 2f;
  21. public double nextWeatherUpdate;
  22. public List<EnviroZoneWeather> weatherTypeList = new List<EnviroZoneWeather>();
  23. public Vector3 zoneScale = Vector3.one;
  24. public Color zoneGizmoColor;
  25. private BoxCollider zoneCollider;
  26. void Start()
  27. {
  28. zoneCollider = gameObject.AddComponent<BoxCollider>();
  29. zoneCollider.isTrigger = true;
  30. UpdateZoneScale ();
  31. }
  32. public void UpdateZoneScale ()
  33. {
  34. zoneCollider.size = zoneScale;
  35. }
  36. // Adds a new weather type to the zone.
  37. public void AddWeatherType(EnviroWeatherType wType)
  38. {
  39. EnviroZoneWeather weatherTypeEntry = new EnviroZoneWeather();
  40. weatherTypeEntry.weatherType = wType;
  41. weatherTypeList.Add(weatherTypeEntry);
  42. }
  43. // Removes a weather type from the zone.
  44. public void RemoveWeatherZoneType(EnviroZoneWeather wType)
  45. {
  46. weatherTypeList.Remove(wType);
  47. }
  48. // Changes the weather of the zone instantly.
  49. public void ChangeZoneWeatherInstant (EnviroWeatherType type)
  50. {
  51. if(EnviroManager.instance != null && currentWeatherType != type)
  52. {
  53. EnviroManager.instance.NotifyZoneWeatherChanged(type,this);
  54. }
  55. currentWeatherType = type;
  56. }
  57. // Changes the weather of the zone to the type for next weather update.
  58. public void ChangeZoneWeather (EnviroWeatherType type)
  59. {
  60. nextWeatherType = type;
  61. }
  62. private void ChooseNextWeatherRandom ()
  63. {
  64. float rand = UnityEngine.Random.Range(0f,100f);
  65. bool nextWeatherFound = false;
  66. for (int i = 0; i < weatherTypeList.Count; i++)
  67. {
  68. if(rand <= weatherTypeList[i].probability)
  69. {
  70. ChangeZoneWeather(weatherTypeList[i].weatherType);
  71. nextWeatherFound = true;
  72. return;
  73. }
  74. }
  75. if(!nextWeatherFound)
  76. ChangeZoneWeather(currentWeatherType);
  77. }
  78. private void UpdateZoneWeather()
  79. {
  80. if(EnviroManager.instance.Time != null)
  81. {
  82. double currentDate = EnviroManager.instance.Time.GetDateInHours();
  83. if(currentDate >= nextWeatherUpdate)
  84. {
  85. if(nextWeatherType != null)
  86. ChangeZoneWeatherInstant(nextWeatherType);
  87. else
  88. ChangeZoneWeatherInstant(currentWeatherType);
  89. //Get next weather
  90. ChooseNextWeatherRandom ();
  91. nextWeatherUpdate = currentDate + weatherChangeIntervall;
  92. }
  93. }
  94. }
  95. void Update()
  96. {
  97. if (EnviroManager.instance == null || EnviroManager.instance.Weather == null)
  98. return;
  99. if(autoWeatherChanges)
  100. UpdateZoneWeather();
  101. //Forces the weather change in Enviro when this zone is currently the active one.
  102. if(EnviroManager.instance.Weather.currentZone == this && EnviroManager.instance.Weather.targetWeatherType != currentWeatherType)
  103. EnviroManager.instance.Weather.ChangeWeather(currentWeatherType);
  104. }
  105. void OnTriggerEnter (Collider col)
  106. {
  107. if (EnviroManager.instance == null || EnviroManager.instance.Weather == null)
  108. return;
  109. //Change Weather to Zone Weather:
  110. if(col.gameObject.GetComponent<EnviroManager>())
  111. EnviroManager.instance.Weather.currentZone = this;
  112. //EnviroManager.instance.Weather.ChangeWeather(currentWeatherType);
  113. }
  114. void OnTriggerExit (Collider col)
  115. {
  116. if (EnviroManager.instance == null || EnviroManager.instance.Weather == null)
  117. return;
  118. if(col.gameObject.GetComponent<EnviroManager>())
  119. EnviroManager.instance.Weather.currentZone = null;
  120. }
  121. void OnDrawGizmos ()
  122. {
  123. Gizmos.color = zoneGizmoColor;
  124. Matrix4x4 rotationMatrix = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
  125. Gizmos.matrix = rotationMatrix;
  126. Gizmos.DrawCube(Vector3.zero, new Vector3(zoneScale.x, zoneScale.y, zoneScale.z));
  127. }
  128. }
  129. }