Files
aibis-dream/Assets/Scripts/SceneManagement/EnvironmentKit/EnvironmentManager.cs
T
2025-06-09 18:12:56 +08:00

94 lines
2.6 KiB
C#

using System;
using AibisDream.Kit;
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;
public override void OnSingletonInit()
{
if (initOnAwake)
{
InitEnvironment(defaultTime, defaultWeather);
}
}
public void InitEnvironment(TimesOfDay timeOfDay, Weather weather)
{
_absSceneParts = GetComponentsInChildren<AbsScenePart>(true);
foreach (var part in _absSceneParts)
{
part.Init(timeOfDay, weather);
}
}
public void SetTimeOfDay(TimesOfDay timeOfDay)
{
foreach (var part in _absSceneParts)
{
part.OnTimeChange(timeOfDay);
}
}
public void SetWeather(Weather weather)
{
foreach (var part in _absSceneParts)
{
part.OnWeatherChange(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);
}
}
}