142 lines
4.0 KiB
C#
142 lines
4.0 KiB
C#
using System;
|
|
using System.Collections;
|
|
using AibisDream.Framework;
|
|
using AibisDream.Kit;
|
|
using Newtonsoft.Json;
|
|
using UnityEngine;
|
|
using Yarn.Unity;
|
|
|
|
namespace AibisDream
|
|
{
|
|
public class EnvironmentManager : Singleton<EnvironmentManager>
|
|
{
|
|
public bool initOnAwake = true;
|
|
public TimesOfDay defaultTime;
|
|
public Weather defaultWeather;
|
|
|
|
private AbsScenePart[] _absSceneParts;
|
|
|
|
private readonly EnvironmentData _environmentData = new();
|
|
|
|
public override void OnSingletonInit()
|
|
{
|
|
StorageSystem.Instance.RegisterData(_environmentData);
|
|
_environmentData.OnLoad += OnLoad;
|
|
if (initOnAwake)
|
|
{
|
|
InitEnvironment(defaultTime, defaultWeather);
|
|
}
|
|
}
|
|
|
|
public override void OnSingletonDestroy()
|
|
{
|
|
StorageSystem.Instance.UnregisterData<EnvironmentData>();
|
|
_environmentData.OnLoad -= OnLoad;
|
|
}
|
|
|
|
private void OnLoad()
|
|
{
|
|
InitEnvironment(_environmentData.curTime, _environmentData.curWeather);
|
|
}
|
|
|
|
public void InitEnvironment(TimesOfDay timeOfDay, Weather weather)
|
|
{
|
|
if (_environmentData.isInit)
|
|
{
|
|
Debug.Log("已经初始化过了");
|
|
return;
|
|
}
|
|
|
|
_absSceneParts = GetComponentsInChildren<AbsScenePart>(true);
|
|
foreach (var part in _absSceneParts)
|
|
{
|
|
part.Init(timeOfDay, weather);
|
|
}
|
|
|
|
_environmentData.curTime = timeOfDay;
|
|
_environmentData.curWeather = weather;
|
|
_environmentData.isInit = true;
|
|
}
|
|
|
|
public void SetTimeOfDay(TimesOfDay timeOfDay)
|
|
{
|
|
foreach (var part in _absSceneParts)
|
|
{
|
|
part.OnTimeChange(timeOfDay);
|
|
}
|
|
|
|
_environmentData.curTime = timeOfDay;
|
|
}
|
|
|
|
public void SetWeather(Weather weather)
|
|
{
|
|
foreach (var part in _absSceneParts)
|
|
{
|
|
part.OnWeatherChange(weather);
|
|
}
|
|
|
|
_environmentData.curWeather = weather;
|
|
}
|
|
}
|
|
|
|
public static class EnvironmentYarnCommand
|
|
{
|
|
[YarnCommand("init_environment")]
|
|
public static void InitEnvironment(string timeOfDay, string weatherType = "Sunny")
|
|
{
|
|
if (!Enum.TryParse<TimesOfDay>(timeOfDay, out var time))
|
|
{
|
|
Debug.Log($"{timeOfDay}是不存在的时间");
|
|
return;
|
|
}
|
|
|
|
if (!Enum.TryParse<Weather>(weatherType, out var weather))
|
|
{
|
|
Debug.Log($"{weatherType}是不存在的天气");
|
|
return;
|
|
}
|
|
|
|
EnvironmentManager.Instance.InitEnvironment(time, weather);
|
|
}
|
|
|
|
[YarnCommand("set_time_of_day")]
|
|
public static void SetTimeOfDay(string timeOfDay)
|
|
{
|
|
if (!Enum.TryParse<TimesOfDay>(timeOfDay, out var time))
|
|
{
|
|
Debug.Log($"{timeOfDay}是不存在的时间");
|
|
return;
|
|
}
|
|
|
|
EnvironmentManager.Instance.SetTimeOfDay(time);
|
|
}
|
|
|
|
[YarnCommand("set_weather")]
|
|
public static void SetWeather(string weatherType)
|
|
{
|
|
if (!Enum.TryParse<Weather>(weatherType, out var weather))
|
|
{
|
|
Debug.Log($"{weatherType}是不存在的天气");
|
|
return;
|
|
}
|
|
|
|
EnvironmentManager.Instance.SetWeather(weather);
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public class EnvironmentData : IData
|
|
{
|
|
[JsonIgnore] public bool isInit;
|
|
public TimesOfDay curTime;
|
|
public Weather curWeather;
|
|
|
|
public event Action OnLoad;
|
|
|
|
public IEnumerator Load()
|
|
{
|
|
OnLoad?.Invoke();
|
|
yield break;
|
|
}
|
|
}
|
|
} |