177 lines
4.8 KiB
C#
177 lines
4.8 KiB
C#
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<EnvironmentManager>
|
|
{
|
|
public bool initOnAwake = true;
|
|
public TimesOfDay defaultTime;
|
|
public Weather defaultWeather;
|
|
|
|
private AbsScenePart[] _absSceneParts;
|
|
private Dictionary<string, Animator> _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<TimesOfDay>(dto.curTime, out var time))
|
|
{
|
|
time = defaultTime;
|
|
}
|
|
|
|
if (!Enum.TryParse<Weather>(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<string, Animator>();
|
|
foreach (var animator in GetComponentsInChildren<Animator>(true))
|
|
{
|
|
_animatorDict[animator.gameObject.name] = animator;
|
|
}
|
|
|
|
_absSceneParts = GetComponentsInChildren<AbsScenePart>(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<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);
|
|
}
|
|
|
|
[YarnCommand("control_single_part")]
|
|
public static void ControlSinglePart(string partName, string action)
|
|
{
|
|
EnvironmentManager.Instance.ControlSinglePart(partName, action);
|
|
}
|
|
}
|
|
}
|