Advertisement
evelynshilosky

WeatherSystem - Part 40

Apr 26th, 2024
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.51 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using Random = UnityEngine.Random;
  6.  
  7. public class WeatherSystem : MonoBehaviour
  8. {
  9.     [Range(0f,1f)]
  10.     public float chanceToRainSpring = 0.3f; // 30%
  11.     [Range(0f, 1f)]
  12.     public float chanceToRainSummer = 0.001f;
  13.     [Range(0f, 1f)]
  14.     public float chanceToRainAutumn = 0.4f; //40%
  15.     [Range(0f, 1f)]
  16.     public float chanceToRainWinter = 0.7f; //70%
  17.  
  18.     public GameObject rainEffect;
  19.     public Material rainSkyBox;
  20.  
  21.     public bool isSpecialWeather;
  22.  
  23.     public AudioSource rainChannel;
  24.     public AudioClip rainSound;
  25.  
  26.     public enum WeatherCondition
  27.     {
  28.         Sunny,
  29.         Rainy
  30.     }
  31.  
  32.     private WeatherCondition currentWeather = WeatherCondition.Sunny;
  33.  
  34.  
  35.     private void Start()
  36.     {
  37.         TimeManager.Instance.OnDayPass.AddListener(GenerateRandomWeather);
  38.     }
  39.  
  40.     private void GenerateRandomWeather()
  41.     {
  42.         TimeManager.Season currentSeason = TimeManager.Instance.currentSeason;
  43.  
  44.         float chanceToRain = 0f;
  45.  
  46.         switch (currentSeason)
  47.         {
  48.             case TimeManager.Season.Spring:
  49.                 chanceToRain = chanceToRainSpring;
  50.                 break;
  51.             case TimeManager.Season.Summer:
  52.                 chanceToRain = chanceToRainSummer;
  53.                 break;
  54.             case TimeManager.Season.Autumn:
  55.                 chanceToRain = chanceToRainAutumn;
  56.                 break;
  57.             case TimeManager.Season.Winter:
  58.                 chanceToRain = chanceToRainWinter;
  59.                 break;
  60.         }
  61.  
  62.         // Generate random number for the chance of rain
  63.         if (Random.value <= chanceToRain)
  64.         {
  65.             currentWeather = WeatherCondition.Rainy;
  66.             isSpecialWeather = true;
  67.  
  68.            
  69.  
  70.             Invoke("StartRain", 1f);
  71.         }
  72.         else
  73.         {
  74.             currentWeather = WeatherCondition.Sunny;
  75.             isSpecialWeather = false;
  76.  
  77.             StopRain();
  78.         }
  79.  
  80.  
  81.     }
  82.     private void StartRain()
  83.     {
  84.         if (rainChannel.isPlaying == false)
  85.         {
  86.             rainChannel.clip = rainSound;
  87.             rainChannel.loop = true;
  88.             rainChannel.Play();
  89.         }
  90.        
  91.         RenderSettings.skybox = rainSkyBox;
  92.         rainEffect.SetActive(true);
  93.     }
  94.  
  95.  
  96.     private void StopRain()
  97.     {
  98.         if (rainChannel.isPlaying)
  99.         {
  100.             rainChannel.Stop();
  101.         }
  102.  
  103.         rainEffect.SetActive(false);
  104.     }
  105.  
  106.    
  107. }
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement