EnviroMirrorServer.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /// <summary>
  2. /// This component can be used to synchronize time and weather.
  3. /// </summary>
  4. using UnityEngine;
  5. #if ENVIRO_MIRROR_SUPPORT
  6. using Mirror;
  7. #endif
  8. using System.Collections;
  9. namespace Enviro
  10. {
  11. #if ENVIRO_MIRROR_SUPPORT
  12. [AddComponentMenu("Enviro 3/Integrations/Mirror Server")]
  13. [RequireComponent(typeof (NetworkIdentity))]
  14. public class EnviroMirrorServer : NetworkBehaviour {
  15. #else
  16. public class EnviroMirrorServer : MonoBehaviour {
  17. #endif
  18. #if ENVIRO_MIRROR_SUPPORT
  19. public float updateSmoothing = 15f;
  20. [SyncVar] private float networkHours;
  21. [SyncVar] private int networkDays;
  22. [SyncVar] private int networkMonths;
  23. [SyncVar] private int networkYears;
  24. public bool isHeadless = true;
  25. public override void OnStartServer()
  26. {
  27. if (isHeadless)
  28. {
  29. //EnviroManager.instance.serverMode = true;
  30. }
  31. EnviroManager.instance.OnSeasonChanged += (EnviroEnvironment.Seasons season) => {
  32. SendSeasonToClient (season);
  33. };
  34. EnviroManager.instance.OnWeatherChanged += (EnviroWeatherType type) => {
  35. SendWeatherToClient (type);
  36. };
  37. }
  38. public void Start ()
  39. {
  40. if (!isServer)
  41. {
  42. if(EnviroManager.instance.Time != null)
  43. EnviroManager.instance.Time.Settings.simulate = false;
  44. }
  45. }
  46. void SendWeatherToClient (EnviroWeatherType w)
  47. {
  48. if(EnviroManager.instance.Weather != null)
  49. {
  50. for (int i = 0; i < EnviroManager.instance.Weather.Settings.weatherTypes.Count; i++)
  51. {
  52. if(EnviroManager.instance.Weather.Settings.weatherTypes[i] == w)
  53. RpcWeatherUpdate (i);
  54. }
  55. }
  56. }
  57. void SendSeasonToClient (EnviroEnvironment.Seasons s)
  58. {
  59. RpcSeasonUpdate((int)s);
  60. }
  61. [ClientRpc]
  62. void RpcSeasonUpdate (int season)
  63. {
  64. if(EnviroManager.instance.Environment != null)
  65. EnviroManager.instance.Environment.ChangeSeason((EnviroEnvironment.Seasons)season);
  66. }
  67. [ClientRpc]
  68. void RpcWeatherUpdate (int weather)
  69. {
  70. if(EnviroManager.instance.Weather != null)
  71. EnviroManager.instance.Weather.ChangeWeather(EnviroManager.instance.Weather.Settings.weatherTypes[weather]);
  72. }
  73. void Update ()
  74. {
  75. if (EnviroManager.instance == null || EnviroManager.instance.Time == null)
  76. return;
  77. if (!isServer)
  78. {
  79. if (networkHours < 1f && EnviroManager.instance.Time.GetTimeOfDay() > 23f)
  80. EnviroManager.instance.Time.SetTimeOfDay(networkHours);
  81. EnviroManager.instance.Time.SetTimeOfDay(Mathf.Lerp(EnviroManager.instance.Time.GetTimeOfDay(), (float)networkHours, Time.deltaTime * updateSmoothing));
  82. EnviroManager.instance.Time.years = networkYears;
  83. EnviroManager.instance.Time.months = networkMonths;
  84. EnviroManager.instance.Time.days = networkDays;
  85. } else {
  86. networkHours = EnviroManager.instance.Time.GetTimeOfDay();
  87. networkDays = EnviroManager.instance.Time.days;
  88. networkMonths = EnviroManager.instance.Time.months;
  89. networkYears = EnviroManager.instance.Time.years;
  90. }
  91. }
  92. #endif
  93. }
  94. }