using System; using System.Collections; using System.Collections.Generic; using AibisDream.Kit; using AibisDream.SaveSystem; using UnityEngine; using Yarn.Unity; namespace AibisDream { public class EnvironmentManager : Singleton { public bool initOnAwake = true; public TimesOfDay defaultTime; public Weather defaultWeather; private AbsScenePart[] _absSceneParts; private Dictionary _animatorDict; private bool _isInitialized; private TimesOfDay _curTime; private Weather _curWeather; public override void OnSingletonInit() { if (initOnAwake) { InitEnvironment(defaultTime, defaultWeather); } } public EnvironmentSnapshotDto CaptureSnapshot() { return new EnvironmentSnapshotDto { curTime = _curTime.ToString(), curWeather = _curWeather.ToString(), isInitialized = _isInitialized }; } public void RestoreSnapshot(EnvironmentSnapshotDto dto) { if (dto == null) return; if (!Enum.TryParse(dto.curTime, out var time)) { time = defaultTime; } if (!Enum.TryParse(dto.curWeather, out var weather)) { weather = defaultWeather; } if (!_isInitialized || !dto.isInitialized) { InitEnvironment(time, weather); } else { SetTimeOfDay(time); SetWeather(weather); } } public void InitEnvironment(TimesOfDay timeOfDay, Weather weather) { if (_isInitialized) { Debug.Log("已经初始化过了"); return; } _animatorDict = new Dictionary(); foreach (var animator in GetComponentsInChildren(true)) { _animatorDict[animator.gameObject.name] = animator; } _absSceneParts = GetComponentsInChildren(true); foreach (var part in _absSceneParts) { part.Init(timeOfDay, weather); } _curTime = timeOfDay; _curWeather = weather; _isInitialized = true; } public void SetTimeOfDay(TimesOfDay timeOfDay) { foreach (var part in _absSceneParts) { part.OnTimeChange(timeOfDay); } _curTime = timeOfDay; } public void SetWeather(Weather weather) { foreach (var part in _absSceneParts) { part.OnWeatherChange(weather); } _curWeather = weather; } public void ControlSinglePart(string partName, string action) { if (_animatorDict.TryGetValue(partName, out var animator)) { animator.Play(action); } else { Debug.Log($"{partName}不存在"); } } } public static class EnvironmentYarnCommand { [YarnCommand("init_environment")] public static void InitEnvironment(string timeOfDay, string weatherType = "Sunny") { if (!Enum.TryParse(timeOfDay, out var time)) { Debug.Log($"{timeOfDay}是不存在的时间"); return; } if (!Enum.TryParse(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(timeOfDay, out var time)) { Debug.Log($"{timeOfDay}是不存在的时间"); return; } EnvironmentManager.Instance.SetTimeOfDay(time); } [YarnCommand("set_weather")] public static void SetWeather(string weatherType) { if (!Enum.TryParse(weatherType, out var weather)) { Debug.Log($"{weatherType}是不存在的天气"); return; } EnvironmentManager.Instance.SetWeather(weather); } [YarnCommand("control_single_part")] public static void ControlSinglePart(string partName, string action) { EnvironmentManager.Instance.ControlSinglePart(partName, action); } } }