218 lines
6.2 KiB
C#
218 lines
6.2 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
[AddComponentMenu("Enviro/Weather Zone")]
|
|
public class EnviroZone : MonoBehaviour
|
|
{
|
|
[Tooltip("Defines the zone name.")]
|
|
public string zoneName;
|
|
|
|
[HideInInspector]
|
|
public List<EnviroWeatherPrefab> zoneWeather = new List<EnviroWeatherPrefab>();
|
|
|
|
[HideInInspector]
|
|
public List<EnviroWeatherPrefab> curPossibleZoneWeather;
|
|
|
|
[Header("Zone weather settings:")]
|
|
[Tooltip("Add alll weather prefabs for this zone here.")]
|
|
public List<GameObject> zoneWeatherPrefabs = new List<GameObject>();
|
|
|
|
[Tooltip("Defines how often (gametime hours) the system will heck to change the current weather conditions.")]
|
|
public float WeatherUpdateIntervall = 6f;
|
|
|
|
[Tooltip("Defines the zone scale.")]
|
|
[Header("Zone scaling and gizmo:")]
|
|
public Vector3 zoneScale = new Vector3(100f, 100f, 100f);
|
|
|
|
[Tooltip("Defines the color of the zone's gizmo in editor mode.")]
|
|
public Color zoneGizmoColor = Color.gray;
|
|
|
|
[Tooltip("The current active weather conditions.")]
|
|
[Header("Current active weather:")]
|
|
public EnviroWeatherPrefab currentActiveZoneWeatherID;
|
|
|
|
[HideInInspector]
|
|
public EnviroWeatherPrefab lastActiveZoneWeatherID;
|
|
|
|
private BoxCollider zoneCollider;
|
|
|
|
private float nextUpdate;
|
|
|
|
private bool init;
|
|
|
|
private void Start()
|
|
{
|
|
zoneCollider = base.gameObject.AddComponent<BoxCollider>();
|
|
zoneCollider.isTrigger = true;
|
|
UpdateZoneScale();
|
|
if (!GetComponent<EnviroSky>())
|
|
{
|
|
EnviroSky.instance.RegisterZone(this);
|
|
}
|
|
}
|
|
|
|
public void UpdateZoneScale()
|
|
{
|
|
zoneCollider.size = zoneScale;
|
|
}
|
|
|
|
public void CreateZoneWeatherTypeList()
|
|
{
|
|
for (int i = 0; i < zoneWeatherPrefabs.Count; i++)
|
|
{
|
|
bool flag = true;
|
|
for (int j = 0; j < EnviroSky.instance.Weather.WeatherPrefabs.Count; j++)
|
|
{
|
|
if (zoneWeatherPrefabs[i] == EnviroSky.instance.Weather.WeatherPrefabs[j])
|
|
{
|
|
flag = false;
|
|
zoneWeather.Add(EnviroSky.instance.Weather.WeatherTemplates[j]);
|
|
}
|
|
}
|
|
if (flag)
|
|
{
|
|
GameObject gameObject = Object.Instantiate(zoneWeatherPrefabs[i]);
|
|
gameObject.transform.parent = EnviroSky.instance.Weather.VFXHolder.transform;
|
|
gameObject.transform.localPosition = Vector3.zero;
|
|
gameObject.transform.localRotation = Quaternion.identity;
|
|
zoneWeather.Add(gameObject.GetComponent<EnviroWeatherPrefab>());
|
|
EnviroSky.instance.Weather.WeatherPrefabs.Add(zoneWeatherPrefabs[i]);
|
|
EnviroSky.instance.Weather.WeatherTemplates.Add(gameObject.GetComponent<EnviroWeatherPrefab>());
|
|
}
|
|
}
|
|
for (int k = 0; k < zoneWeather.Count; k++)
|
|
{
|
|
for (int l = 0; l < zoneWeather[k].effectParticleSystems.Count; l++)
|
|
{
|
|
zoneWeather[k].effectEmmisionRates.Add(EnviroSky.GetEmissionRate(zoneWeather[k].effectParticleSystems[l]));
|
|
EnviroSky.SetEmissionRate(zoneWeather[k].effectParticleSystems[l], 0f);
|
|
}
|
|
}
|
|
currentActiveZoneWeatherID = zoneWeather[0];
|
|
lastActiveZoneWeatherID = zoneWeather[0];
|
|
}
|
|
|
|
private void BuildNewWeatherList()
|
|
{
|
|
curPossibleZoneWeather = new List<EnviroWeatherPrefab>();
|
|
for (int i = 0; i < zoneWeather.Count; i++)
|
|
{
|
|
switch (EnviroSky.instance.Seasons.currentSeasons)
|
|
{
|
|
case SeasonVariables.Seasons.Spring:
|
|
if (zoneWeather[i].Spring)
|
|
{
|
|
curPossibleZoneWeather.Add(zoneWeather[i]);
|
|
}
|
|
break;
|
|
case SeasonVariables.Seasons.Summer:
|
|
if (zoneWeather[i].Summer)
|
|
{
|
|
curPossibleZoneWeather.Add(zoneWeather[i]);
|
|
}
|
|
break;
|
|
case SeasonVariables.Seasons.Autumn:
|
|
if (zoneWeather[i].Autumn)
|
|
{
|
|
curPossibleZoneWeather.Add(zoneWeather[i]);
|
|
}
|
|
break;
|
|
case SeasonVariables.Seasons.Winter:
|
|
if (zoneWeather[i].winter)
|
|
{
|
|
curPossibleZoneWeather.Add(zoneWeather[i]);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
private EnviroWeatherPrefab PossibiltyCheck()
|
|
{
|
|
List<EnviroWeatherPrefab> list = new List<EnviroWeatherPrefab>();
|
|
for (int i = 0; i < curPossibleZoneWeather.Count; i++)
|
|
{
|
|
int num = Random.Range(0, 100);
|
|
if (EnviroSky.instance.Seasons.currentSeasons == SeasonVariables.Seasons.Spring && (float)num <= curPossibleZoneWeather[i].possibiltyInSpring)
|
|
{
|
|
list.Add(curPossibleZoneWeather[i]);
|
|
}
|
|
if (EnviroSky.instance.Seasons.currentSeasons == SeasonVariables.Seasons.Summer && (float)num <= curPossibleZoneWeather[i].possibiltyInSummer)
|
|
{
|
|
list.Add(curPossibleZoneWeather[i]);
|
|
}
|
|
if (EnviroSky.instance.Seasons.currentSeasons == SeasonVariables.Seasons.Autumn && (float)num <= curPossibleZoneWeather[i].possibiltyInAutumn)
|
|
{
|
|
list.Add(curPossibleZoneWeather[i]);
|
|
}
|
|
if (EnviroSky.instance.Seasons.currentSeasons == SeasonVariables.Seasons.Winter && (float)num <= curPossibleZoneWeather[i].possibiltyInWinter)
|
|
{
|
|
list.Add(curPossibleZoneWeather[i]);
|
|
}
|
|
}
|
|
if (list.Count > 0)
|
|
{
|
|
EnviroSky.instance.NotifyZoneWeatherChanged(list[0], this);
|
|
return list[0];
|
|
}
|
|
return currentActiveZoneWeatherID;
|
|
}
|
|
|
|
private void WeatherUpdate()
|
|
{
|
|
nextUpdate = EnviroSky.instance.currentTimeInHours + WeatherUpdateIntervall;
|
|
BuildNewWeatherList();
|
|
lastActiveZoneWeatherID = currentActiveZoneWeatherID;
|
|
currentActiveZoneWeatherID = PossibiltyCheck();
|
|
}
|
|
|
|
private void LateUpdate()
|
|
{
|
|
if (EnviroSky.instance.started && !init)
|
|
{
|
|
CreateZoneWeatherTypeList();
|
|
init = true;
|
|
}
|
|
if (EnviroSky.instance.currentTimeInHours >= nextUpdate && EnviroSky.instance.Weather.updateWeather && EnviroSky.instance.started)
|
|
{
|
|
WeatherUpdate();
|
|
}
|
|
}
|
|
|
|
private void OnTriggerEnter(Collider col)
|
|
{
|
|
if (EnviroSky.instance.Weather.useTag)
|
|
{
|
|
if (col.gameObject.tag == EnviroSky.instance.gameObject.tag)
|
|
{
|
|
EnviroSky.instance.Weather.currentActiveZone = this;
|
|
}
|
|
}
|
|
else if ((bool)col.gameObject.GetComponent<EnviroSky>())
|
|
{
|
|
EnviroSky.instance.Weather.currentActiveZone = this;
|
|
}
|
|
}
|
|
|
|
private void OnTriggerExit(Collider col)
|
|
{
|
|
if (EnviroSky.instance.Weather.useTag)
|
|
{
|
|
if (col.gameObject.tag == EnviroSky.instance.gameObject.tag)
|
|
{
|
|
EnviroSky.instance.Weather.currentActiveZone = EnviroSky.instance.Weather.zones[0];
|
|
}
|
|
}
|
|
else if ((bool)col.gameObject.GetComponent<EnviroSky>())
|
|
{
|
|
EnviroSky.instance.Weather.currentActiveZone = EnviroSky.instance.Weather.zones[0];
|
|
}
|
|
}
|
|
|
|
private void OnDrawGizmos()
|
|
{
|
|
Gizmos.color = zoneGizmoColor;
|
|
Gizmos.DrawCube(base.transform.position, new Vector3(zoneScale.x, zoneScale.y, zoneScale.z));
|
|
}
|
|
}
|