294 lines
8.3 KiB
C#
294 lines
8.3 KiB
C#
using System.Linq;
|
|
using AibisDream.Framework;
|
|
using AibisDream.Kit;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
|
|
namespace AibisDream.FixSystem
|
|
{
|
|
public class BodyModuleSystem : SystemHasData
|
|
{
|
|
public static string DataPath = Application.streamingAssetsPath + "/LevelData/BodyModuleSystem/";
|
|
|
|
#region 内部索引
|
|
|
|
public BodyModule[] BodyModules { get; private set; }
|
|
public CableSystem CableSystem { get; private set; }
|
|
public GameObject bodyModulePrefab;
|
|
[HideInInspector] public Transform bodyModuleGroup;
|
|
[HideInInspector] public OscilloscopeSystem oscilloscopeSystem;
|
|
[HideInInspector] public HeatMapController heatMapController;
|
|
|
|
#endregion
|
|
|
|
#region 数据保存相关
|
|
|
|
public override string CurDataKey { get; set; }
|
|
private string[] _keyOptions;
|
|
|
|
#endregion
|
|
|
|
#region 事件
|
|
|
|
[HideInInspector] public UnityEvent<ISocket> plugInEvent;
|
|
[HideInInspector] public UnityEvent<ISocket> plugOutEvent;
|
|
|
|
#endregion
|
|
|
|
#region 系统状态
|
|
|
|
public bool inHotErr;
|
|
private int _heatValue;
|
|
|
|
private int HeatValue
|
|
{
|
|
get => _heatValue;
|
|
set
|
|
{
|
|
_heatValue = value;
|
|
inHotErr = _heatValue > 0;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
[SerializeField] public float plugSnappingRange = 1f;
|
|
|
|
private void Awake()
|
|
{
|
|
InitComponentRefs();
|
|
}
|
|
|
|
private void InitComponentRefs()
|
|
{
|
|
// 处理内部索引
|
|
BodyModules = transform.GetComponentsInChildren<BodyModule>();
|
|
CableSystem = transform.Find("Cable System").GetComponent<CableSystem>();
|
|
bodyModuleGroup = transform.Find("Body Module Group");
|
|
oscilloscopeSystem = transform.Find("Oscilloscope System").GetComponent<OscilloscopeSystem>();
|
|
if (transform.Find("HeatMapController"))
|
|
{
|
|
heatMapController = transform.Find("HeatMapController").GetComponent<HeatMapController>();
|
|
}
|
|
|
|
Debug.Log("Register");
|
|
FixSystemCenter.SystemDic.Register(this);
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
gameObject.SetActive(false);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 寻找最接近的Module
|
|
/// </summary>
|
|
/// <param name="sourcePos">源位置</param>
|
|
/// <param name="targetModule">最近的Module</param>
|
|
/// <returns>是否能找到目标范围内的Module</returns>
|
|
public bool TryFindClosestModule(Vector3 sourcePos, out BodyModule targetModule)
|
|
{
|
|
var closestDistance = Mathf.Infinity;
|
|
BodyModule closestModule = null;
|
|
|
|
// 寻找最接近的BodyModule
|
|
foreach (var bodyModule in BodyModules)
|
|
{
|
|
if (!bodyModule.IsAvailable()) // 检查模块是否可用
|
|
{
|
|
continue; // 跳过不可用的模块
|
|
}
|
|
|
|
float tempDistance = Vector3.Distance(sourcePos, bodyModule.GetSocketPos());
|
|
|
|
if (tempDistance < closestDistance)
|
|
{
|
|
closestDistance = tempDistance;
|
|
closestModule = bodyModule;
|
|
}
|
|
}
|
|
|
|
// 判断距离是否在目标范围内,在的话就返回,不在的话返回个空的
|
|
if (closestDistance < plugSnappingRange && closestModule)
|
|
{
|
|
targetModule = closestModule;
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
targetModule = null;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 寻找指定名称的BodyModule
|
|
/// </summary>
|
|
/// <param name="modulename">模块名称</param>
|
|
/// <returns>对应名称的BodyModule</returns>
|
|
public BodyModule FindBodyModuleByName(string modulename)
|
|
{
|
|
return BodyModules?.FirstOrDefault(module => module.data.moduleName == modulename);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置死循环状态
|
|
/// </summary>
|
|
public void SetModuleState_CantStopRunning(string moduleName, bool state)
|
|
{
|
|
BodyModule result = FindBodyModuleByName(moduleName);
|
|
if (result != null)
|
|
{
|
|
Debug.Log("Found module: " + moduleName);
|
|
result.data.cantStopRunning = state;
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("Module not found.");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置发热
|
|
/// </summary>
|
|
public void SetHeat(int targetHeatValue)
|
|
{
|
|
HeatValue = targetHeatValue;
|
|
|
|
heatMapController.SetTemptureDirect(HeatValue);
|
|
}
|
|
|
|
public void ShowHeatMap()
|
|
{
|
|
heatMapController.ShowHeatMap();
|
|
ResetPlug();
|
|
}
|
|
|
|
public void HideHeatMap()
|
|
{
|
|
heatMapController.HideHeatMap();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 冷却后处理
|
|
/// </summary>
|
|
public void OnCooling()
|
|
{
|
|
if (!inHotErr) return;
|
|
// 系统冷却
|
|
_heatValue = 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 插头重新插入初始插孔
|
|
/// </summary>
|
|
public void ResetPlug()
|
|
{
|
|
CableSystem.ResetPlug();
|
|
}
|
|
|
|
#region 数据加载与保存
|
|
|
|
public override string[] GetDataKeyOptions()
|
|
{
|
|
return _keyOptions ??= ConfigUtil.Instance.GetFileNames(DataPath);
|
|
}
|
|
|
|
public override void Load()
|
|
{
|
|
if (string.IsNullOrEmpty(CurDataKey))
|
|
{
|
|
Debug.Log("配置项名称为空");
|
|
return;
|
|
}
|
|
|
|
if (!ConfigUtil.Instance.TryLoadConfig<BodyModuleSystemData>(DataPath + CurDataKey,
|
|
out var data))
|
|
{
|
|
Debug.Log("没有找到配置项");
|
|
return;
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
// 现将当前配置保存为temp避免丢失
|
|
if (CurDataKey != "temp.json")
|
|
{
|
|
SaveByKey("temp.json");
|
|
}
|
|
#endif
|
|
|
|
// 加载数据
|
|
LoadData(data);
|
|
}
|
|
|
|
private void LoadData(BodyModuleSystemData systemData)
|
|
{
|
|
inHotErr = systemData.hasHotErr;
|
|
|
|
var oldModules = GetComponentsInChildren<BodyModule>();
|
|
// 先销毁旧的Module
|
|
foreach (var oldModule in oldModules)
|
|
{
|
|
#if UNITY_EDITOR
|
|
DestroyImmediate(oldModule.gameObject);
|
|
#else
|
|
Destroy(oldModule);
|
|
#endif
|
|
}
|
|
|
|
// 再加载新的Module
|
|
var newModules = new BodyModule[systemData.moduleDataArray.Length];
|
|
for (int i = 0; i < systemData.moduleDataArray.Length; i++)
|
|
{
|
|
BodyModule newModule = Instantiate(bodyModulePrefab, bodyModuleGroup).GetComponent<BodyModule>();
|
|
newModule.Load(systemData.moduleDataArray[i]);
|
|
newModules[i] = newModule;
|
|
}
|
|
|
|
BodyModules = newModules;
|
|
}
|
|
|
|
public override void Save()
|
|
{
|
|
if (string.IsNullOrEmpty(CurDataKey)) return;
|
|
|
|
SaveByKey(CurDataKey);
|
|
if (!_keyOptions.Contains(CurDataKey))
|
|
{
|
|
// 重新读取key
|
|
_keyOptions = ConfigUtil.Instance.GetFileNames(DataPath);
|
|
}
|
|
}
|
|
|
|
private void SaveByKey(string key)
|
|
{
|
|
var oldModules = GetComponentsInChildren<BodyModule>();
|
|
var moduleDataArray = new BodyModuleData[oldModules.Length];
|
|
for (int i = 0; i < oldModules.Length; i++)
|
|
{
|
|
moduleDataArray[i] = oldModules[i].Save();
|
|
}
|
|
|
|
var systemData = new BodyModuleSystemData
|
|
{
|
|
moduleDataArray = moduleDataArray,
|
|
hasHotErr = inHotErr
|
|
};
|
|
|
|
ConfigUtil.Instance.SaveConfig(systemData, DataPath + key);
|
|
}
|
|
|
|
public void AddBodyModule()
|
|
{
|
|
Instantiate(bodyModulePrefab, bodyModuleGroup);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
|
|
public struct BodyModuleSystemData
|
|
{
|
|
public bool hasHotErr;
|
|
public BodyModuleData[] moduleDataArray;
|
|
}
|
|
} |