增加BodyModule切换功能

This commit is contained in:
2025-04-01 19:01:54 +08:00
parent 82e22d8b38
commit ca2729b009
56 changed files with 33254 additions and 6777 deletions
@@ -1,35 +1,30 @@
using System;
using System.Collections;
using System.Linq;
using AibisDream.Framework;
using AibisDream.Kit;
using Cinemachine;
using UnityEngine;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace AibisDream.FixSystem
{
public class BodyModuleSystem : SystemHasData
public class BodyModuleSystem : MonoBehaviour
{
public static string DataPath = Application.streamingAssetsPath + "/LevelData/BodyModuleSystem/";
#region
public List<BodyModule> BodyModules { get; private set; }
private BodyModule[] BodyModules { get; set; }
private GameObject _bodyModuleGroup;
private GameObject _headModuleGroup;
public CableSystem CableSystem { get; private set; }
public GameObject bodyModulePrefab;
[HideInInspector] public Transform bodyModuleGroup;
[HideInInspector] public HeatMapController heatMapController;
#endregion
#region
public override string CurDataKey { get; set; }
private string[] _keyOptions;
#endregion
#region
private readonly ModuleSystemData _data = new();
public bool inHotErr;
private int _heatValue;
public bool isAvailable;
@@ -50,15 +45,18 @@ namespace AibisDream.FixSystem
private void Awake()
{
InitComponentRefs();
}
private void InitComponentRefs()
{
// 注册数据
StorageSystem.Instance.RegisterData(_data);
// 处理内部索引
BodyModules = transform.GetComponentsInChildren<BodyModule>().ToList();
CableSystem = transform.Find("Cable System").GetComponent<CableSystem>();
bodyModuleGroup = transform.Find("Body Module Group");
_bodyModuleGroup = transform.Find("Body Module Group")?.gameObject;
_headModuleGroup = transform.Find("Head Module Group")?.gameObject;
if (transform.Find("HeatMapController"))
{
heatMapController = transform.Find("HeatMapController").GetComponent<HeatMapController>();
@@ -72,6 +70,7 @@ namespace AibisDream.FixSystem
private void OnDestroy()
{
StorageSystem.Instance.UnregisterData<ModuleSystemData>();
CameraKit.Instance.UnRegisterCamera(CameraEnum.BodyModule);
}
@@ -126,23 +125,6 @@ namespace AibisDream.FixSystem
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>
@@ -184,112 +166,6 @@ namespace AibisDream.FixSystem
CableSystem.ResetPlug();
}
public void HandleDeepIn()
{
if (CableSystem.curBodyModule)
{
CableSystem.curBodyModule.DeepIn();
}
}
#region
public override string GetConfigPath()
{
return DataPath;
}
public override void LoadLevel()
{
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.ToList();
}
public override void SaveLevel()
{
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
/// <summary>
/// 激活交互系统
/// </summary>
@@ -305,11 +181,48 @@ namespace AibisDream.FixSystem
{
isAvailable = false;
}
public void SwitchModuleGroup(ModuleState moduleState)
{
// 先切换Group
switch (moduleState)
{
case ModuleState.Body:
_bodyModuleGroup.SetActive(true);
_headModuleGroup.SetActive(false);
break;
case ModuleState.Head:
_bodyModuleGroup.SetActive(false);
_headModuleGroup.SetActive(true);
break;
default:
throw new ArgumentOutOfRangeException(nameof(moduleState), moduleState, null);
}
// 重新获取ModuleGroup
var root = moduleState == ModuleState.Body ? _bodyModuleGroup : _headModuleGroup;
BodyModules = root.GetComponentsInChildren<BodyModule>();
// 更改data
_data.moduleState = moduleState;
}
}
public struct BodyModuleSystemData
public class ModuleSystemData : IData
{
public bool hasHotErr;
public BodyModuleData[] moduleDataArray;
public ModuleState moduleState;
[JsonIgnore]
public BodyModuleSystem bodyModuleSystem;
public IEnumerator Load()
{
bodyModuleSystem.SwitchModuleGroup(moduleState);
yield break;
}
}
public enum ModuleState
{
Body,
Head
}
}