85 lines
2.8 KiB
C#
85 lines
2.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
|
|
namespace AibisDream
|
|
{
|
|
[RequireComponent(typeof(Spawner))]
|
|
public class SpawnScenePart : AbsScenePart
|
|
{
|
|
private Spawner _spawner;
|
|
[SerializeField] private SpawnerPartAsset[] spawnerPartAssets;
|
|
|
|
public override void Init(TimesOfDay time, Weather weather = Weather.Sunny)
|
|
{
|
|
_spawner = GetComponent<Spawner>();
|
|
// 处理数据切换
|
|
curWeather = weather;
|
|
OnTimeChange(time);
|
|
// 切换完成后再行初始化
|
|
_spawner.Init();
|
|
}
|
|
|
|
public override void OnTimeChange(TimesOfDay time)
|
|
{
|
|
curTime = time;
|
|
|
|
var timeArray = spawnerPartAssets.Where(item => item.time == time).ToArray();
|
|
if (timeArray.Length == 0) return;
|
|
var res = timeArray.DefaultIfEmpty(timeArray[0]).First(item => item.weather == curWeather);
|
|
UpdateSpawner(res.mobileAddressData);
|
|
}
|
|
|
|
public override void OnWeatherChange(Weather weather)
|
|
{
|
|
curWeather = weather;
|
|
|
|
var weatherArray = spawnerPartAssets.Where(item => item.weather == weather).ToArray();
|
|
if (weatherArray.Length == 0) return;
|
|
var res = weatherArray.DefaultIfEmpty(weatherArray[0]).First(item => item.time == curTime);
|
|
UpdateSpawner(res.mobileAddressData);
|
|
}
|
|
|
|
private void UpdateSpawner(AddressArray[] addressArrays)
|
|
{
|
|
// 先把addressArray转为字典
|
|
var dict = addressArrays.ToDictionary(addressArray => addressArray.itemName);
|
|
// 处理Spawner
|
|
var newData = _spawner.mobileObjectDatas.Select(item => UpdateSpawnerData(item, dict)).ToArray();
|
|
_spawner.mobileObjectDatas = newData;
|
|
// 处理下面的初始化物体
|
|
var initObjects = GetComponentsInChildren<AbsMobileObject>();
|
|
foreach (var initObject in initObjects)
|
|
{
|
|
initObject.mobileObjectData = UpdateSpawnerData(initObject.mobileObjectData, dict);
|
|
}
|
|
}
|
|
|
|
private MobileObjectData UpdateSpawnerData(MobileObjectData mobileObjectData,
|
|
Dictionary<string, AddressArray> addressDict)
|
|
{
|
|
if (addressDict.TryGetValue(mobileObjectData.itemName, out var partAsset))
|
|
{
|
|
mobileObjectData.assetAddressArray = partAsset.assetAddressArray;
|
|
}
|
|
|
|
return mobileObjectData;
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public struct SpawnerPartAsset
|
|
{
|
|
public TimesOfDay time;
|
|
public Weather weather;
|
|
public AddressArray[] mobileAddressData;
|
|
}
|
|
|
|
[Serializable]
|
|
public struct AddressArray
|
|
{
|
|
public string itemName;
|
|
public string[] assetAddressArray;
|
|
}
|
|
} |