123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System;
- namespace Enviro
- {
- [Serializable]
- public class EnviroEnvironment
- {
- //Seasons
- public enum Seasons
- {
- Spring,
- Summer,
- Autumn,
- Winter
- }
- public Seasons season;
- public bool changeSeason = true;
- [Tooltip("Start Day of Year for Spring")]
- [Range(0, 366)]
- public int springStart = 60;
- [Tooltip("End Day of Year for Spring")]
- [Range(0, 366)]
- public int springEnd = 92;
- [Tooltip("Start Day of Year for Summer")]
- [Range(0, 366)]
- public int summerStart = 93;
- [Tooltip("End Day of Year for Summer")]
- [Range(0, 366)]
- public int summerEnd = 185;
- [Tooltip("Start Day of Year for Autumn")]
- [Range(0, 366)]
- public int autumnStart = 186;
- [Tooltip("End Day of Year for Autumn")]
- [Range(0, 366)]
- public int autumnEnd = 276;
- [Tooltip("Start Day of Year for Winter")]
- [Range(0, 366)]
- public int winterStart = 277;
- [Tooltip("End Day of Year for Winter")]
- [Range(0, 366)]
- public int winterEnd = 59;
- //Temperature
- [Tooltip("Base Temperature in Spring")]
- public AnimationCurve springBaseTemperature = new AnimationCurve();
- [Tooltip("Base Temperature in Summer")]
- public AnimationCurve summerBaseTemperature = new AnimationCurve();
- [Tooltip("Base Temperature in Autumn")]
- public AnimationCurve autumnBaseTemperature = new AnimationCurve();
- [Tooltip("Base Temperature in Winter")]
- public AnimationCurve winterBaseTemperature = new AnimationCurve();
- [Tooltip("Current temperature.")]
- [Range(-50f, 50f)]
- public float temperature = 0f;
- [Tooltip("Temperature mod used for different weather types.")]
- [Range(-50f, 50f)]
- public float temperatureWeatherMod = 0f;
- [Tooltip("Custom temperature mod for gameplay use.")]
- [Range(-50f, 50f)]
- public float temperatureCustomMod = 0f;
- [Tooltip("Temperature changing speed.")]
- public float temperatureChangingSpeed = 1f;
- //Weather State
- [Tooltip("Current wetness for third party shader or gameplay.")]
- [Range(0f, 1f)]
- public float wetness = 0f;
- [Tooltip("Target wetness for third party shader or gameplay.")]
- [Range(0f, 1f)]
- public float wetnessTarget = 0f;
- [Tooltip("Current snow for third party shader or gameplay.")]
- [Range(0f, 1f)]
- public float snow = 0f;
- [Tooltip("Target snow for third party shader or gameplay.")]
- [Range(0f, 1f)]
- public float snowTarget = 0f;
- [Tooltip("Speed of wetness accumulation.")]
- public float wetnessAccumulationSpeed = 1f;
- [Tooltip("Speed of wetness dries.")]
- public float wetnessDrySpeed = 1f;
- [Tooltip("Speed of snow buildup.")]
- public float snowAccumulationSpeed = 1f;
- [Tooltip("Speed of how fast snow melts.")]
- public float snowMeltSpeed = 1f;
- [Tooltip("Temperature when snow starts to melt.")]
- [Range(-20f, 20f)]
- public float snowMeltingTresholdTemperature = 1f;
- }
- [Serializable]
- public class EnviroEnvironmentModule : EnviroModule
- {
- public Enviro.EnviroEnvironment Settings;
- public EnviroEnvironmentModule preset;
- public bool showSeasonControls,showTemperatureControls,showWeatherStateControls,showWindControls;
- // Update Method
- public override void UpdateModule ()
- {
- if(EnviroManager.instance.Time != null)
- {
- UpdateTemperature(EnviroManager.instance.Time.GetUniversalTimeOfDay() / 24f);
- UpdateSeason();
- }
- else
- {
- UpdateTemperature(1f);
- }
- UpdateWeatherState();
- }
- //Changes season based on day settings.
- public void UpdateSeason()
- {
- if(Settings.changeSeason)
- {
- int currentDay = EnviroManager.instance.Time.Settings.date.DayOfYear;
- if (currentDay >= Settings.springStart && currentDay <= Settings.springEnd)
- {
- ChangeSeason(EnviroEnvironment.Seasons.Spring);
- }
- else if (currentDay >= Settings.summerStart && currentDay <= Settings.summerEnd)
- {
- ChangeSeason(EnviroEnvironment.Seasons.Summer);
- }
- else if (currentDay >= Settings.autumnStart && currentDay <= Settings.autumnEnd)
- {
- ChangeSeason(EnviroEnvironment.Seasons.Autumn);
- }
- else if (currentDay >= Settings.winterStart || currentDay <= Settings.winterEnd)
- {
- ChangeSeason(EnviroEnvironment.Seasons.Winter);
- }
- }
- }
- //Changes Season
- public void ChangeSeason(EnviroEnvironment.Seasons season)
- {
- if(Settings.season != season)
- {
- EnviroManager.instance.NotifySeasonChanged(season);
- Settings.season = season;
- }
- }
- //Sets temperature based on time of day.
- public void UpdateTemperature (float timeOfDay)
- {
- float temperature = 0f;
- switch (Settings.season)
- {
- case EnviroEnvironment.Seasons.Spring:
- temperature = Settings.springBaseTemperature.Evaluate(timeOfDay);
- break;
- case EnviroEnvironment.Seasons.Summer:
- temperature = Settings.summerBaseTemperature.Evaluate(timeOfDay);
- break;
- case EnviroEnvironment.Seasons.Autumn:
- temperature = Settings.autumnBaseTemperature.Evaluate(timeOfDay);
- break;
- case EnviroEnvironment.Seasons.Winter:
- temperature = Settings.winterBaseTemperature.Evaluate(timeOfDay);
- break;
- }
- //Apply temperature mods
- temperature += Settings.temperatureWeatherMod;
- temperature += Settings.temperatureCustomMod;
- //Set temperature
- Settings.temperature = Mathf.Lerp(Settings.temperature, temperature, Time.deltaTime * Settings.temperatureChangingSpeed);
- }
- public void UpdateWeatherState()
- {
- // Wetness
- if (Settings.wetness < Settings.wetnessTarget)
- {
- // Raining
- Settings.wetness = Mathf.Lerp(Settings.wetness, Settings.wetnessTarget, Settings.wetnessAccumulationSpeed * Time.deltaTime);
- }
- else
- { // Drying
- Settings.wetness = Mathf.Lerp(Settings.wetness, Settings.wetnessTarget, Settings.wetnessDrySpeed * Time.deltaTime);
- }
- if(Settings.wetness < 0.0001f)
- Settings.wetness = 0f;
- Settings.wetness = Mathf.Clamp(Settings.wetness, 0f, 1f);
- //Snow
- if (Settings.snow < Settings.snowTarget)
- {
- //Snowing
- Settings.snow = Mathf.Lerp(Settings.snow, Settings.snowTarget, Settings.snowAccumulationSpeed * Time.deltaTime);
- }
- else if (Settings.temperature > Settings.snowMeltingTresholdTemperature)
- {
- //Melting
- Settings.snow = Mathf.Lerp(Settings.snow, Settings.snowTarget, Settings.snowMeltSpeed * Time.deltaTime);
- }
- if(Settings.snow < 0.0001f)
- Settings.snow = 0f;
- Settings.snow = Mathf.Clamp(Settings.snow, 0f, 1f);
- }
- //Save and Load
- public void LoadModuleValues ()
- {
- if(preset != null)
- {
- Settings = JsonUtility.FromJson<Enviro.EnviroEnvironment>(JsonUtility.ToJson(preset.Settings));
- }
- else
- {
- Debug.Log("Please assign a saved module to load from!");
- }
- }
- public void SaveModuleValues ()
- {
- #if UNITY_EDITOR
- EnviroEnvironmentModule t = ScriptableObject.CreateInstance<EnviroEnvironmentModule>();
- t.name = "Environment Preset";
- t.Settings = JsonUtility.FromJson<Enviro.EnviroEnvironment>(JsonUtility.ToJson(Settings));
-
- string assetPathAndName = UnityEditor.AssetDatabase.GenerateUniqueAssetPath("Assets/Enviro 3" + "/New " + t.name + ".asset");
- UnityEditor.AssetDatabase.CreateAsset(t, assetPathAndName);
- UnityEditor.AssetDatabase.SaveAssets();
- UnityEditor.AssetDatabase.Refresh();
- #endif
- }
- public void SaveModuleValues (EnviroEnvironmentModule module)
- {
- module.Settings = JsonUtility.FromJson<Enviro.EnviroEnvironment>(JsonUtility.ToJson(Settings));
- #if UNITY_EDITOR
- UnityEditor.EditorUtility.SetDirty(module);
- UnityEditor.AssetDatabase.SaveAssets();
- #endif
- }
- }
- }
|