Ver.0.3.0.33
This commit is contained in:
@@ -1,36 +1,31 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using AibisDream.Framework;
|
||||
using AibisDream.Kit;
|
||||
using Cinemachine;
|
||||
using JetBrains.Annotations;
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
|
||||
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 GameObject _bodyModuleGroup;
|
||||
private GameObject _headModuleGroup;
|
||||
|
||||
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 系统状态
|
||||
|
||||
private readonly ModuleSystemData _data = new();
|
||||
public bool inHotErr;
|
||||
private int _heatValue;
|
||||
public bool isAvailable;
|
||||
@@ -49,6 +44,11 @@ namespace AibisDream.FixSystem
|
||||
|
||||
[SerializeField] public float plugSnappingRange = 1f;
|
||||
|
||||
private Vector3 _headModuleOriginalPos;
|
||||
private Coroutine _breathingCoroutine;
|
||||
private Coroutine _painShakeCoroutine;
|
||||
private Coroutine _continuousPainCoroutine;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
InitComponentRefs();
|
||||
@@ -56,24 +56,30 @@ namespace AibisDream.FixSystem
|
||||
|
||||
private void InitComponentRefs()
|
||||
{
|
||||
// 注册数据
|
||||
StorageSystem.Instance.RegisterData(_data);
|
||||
|
||||
// 处理内部索引
|
||||
BodyModules = transform.GetComponentsInChildren<BodyModule>().ToList();
|
||||
_bodyModuleGroup = transform.Find("Body Module Group")?.gameObject;
|
||||
_headModuleGroup = transform.Find("Head Module Group")?.gameObject;
|
||||
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>();
|
||||
}
|
||||
|
||||
FixSystemCenter.SystemDic.Register(this);
|
||||
|
||||
|
||||
// 注册Camera
|
||||
var virtualCam = transform.GetComponentInChildren<ICinemachineCamera>();
|
||||
var virtualCam = transform.Find("Body Module Camera").GetComponent<ICinemachineCamera>();
|
||||
CameraKit.Instance.RegisterCamera(CameraEnum.BodyModule, virtualCam);
|
||||
var virtualCam2 = transform.Find("Body Module Camera Center").GetComponent<ICinemachineCamera>();
|
||||
CameraKit.Instance.RegisterCamera(CameraEnum.BodyModuleCenter, virtualCam2);
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
StorageSystem.Instance.UnregisterData<ModuleSystemData>();
|
||||
CameraKit.Instance.UnRegisterCamera(CameraEnum.BodyModule);
|
||||
}
|
||||
|
||||
@@ -88,6 +94,12 @@ namespace AibisDream.FixSystem
|
||||
var closestDistance = Mathf.Infinity;
|
||||
BodyModule closestModule = null;
|
||||
|
||||
if (BodyModules is not { Count: > 0 })
|
||||
{
|
||||
targetModule = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
// 寻找最接近的BodyModule
|
||||
foreach (var bodyModule in BodyModules)
|
||||
{
|
||||
@@ -95,7 +107,7 @@ namespace AibisDream.FixSystem
|
||||
{
|
||||
continue; // 跳过不可用的模块
|
||||
}
|
||||
|
||||
|
||||
float tempDistance = Vector3.Distance(sourcePos, bodyModule.GetSocketPos());
|
||||
|
||||
if (tempDistance < closestDistance)
|
||||
@@ -105,19 +117,40 @@ namespace AibisDream.FixSystem
|
||||
}
|
||||
}
|
||||
|
||||
bool res;
|
||||
// 判断距离是否在目标范围内,在的话就返回,不在的话返回个空的
|
||||
if (closestDistance < plugSnappingRange && closestModule)
|
||||
{
|
||||
targetModule = closestModule;
|
||||
return true;
|
||||
res = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
targetModule = null;
|
||||
return false;
|
||||
res = false;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public void OpenModuleSlots()
|
||||
{
|
||||
foreach (var module in BodyModules)
|
||||
{
|
||||
module.OpenPlugSlot();
|
||||
}
|
||||
}
|
||||
|
||||
public void CloseModuleSlots([CanBeNull] BodyModule curModule)
|
||||
{
|
||||
foreach (var module in BodyModules)
|
||||
{
|
||||
if (module != curModule)
|
||||
{
|
||||
module.ClosePlugSlot();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 寻找指定名称的BodyModule
|
||||
/// </summary>
|
||||
@@ -128,23 +161,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>
|
||||
@@ -186,112 +202,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>
|
||||
@@ -307,11 +217,183 @@ 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>().ToList();
|
||||
// 更改data
|
||||
_data.moduleState = moduleState;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从模块中清除
|
||||
/// </summary>
|
||||
/// <param name="module">将要被移除的模块</param>
|
||||
public void RemoveModule(BodyModule module)
|
||||
{
|
||||
BodyModules.Remove(module);
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
if (_headModuleGroup != null)
|
||||
{
|
||||
_headModuleOriginalPos = _headModuleGroup.transform.localPosition;
|
||||
StartBreathing();
|
||||
}
|
||||
}
|
||||
|
||||
public void StartBreathing()
|
||||
{
|
||||
if (_breathingCoroutine != null)
|
||||
{
|
||||
StopCoroutine(_breathingCoroutine);
|
||||
}
|
||||
_breathingCoroutine = StartCoroutine(BreathingAnimation());
|
||||
}
|
||||
|
||||
public void StopBreathing()
|
||||
{
|
||||
if (_breathingCoroutine != null)
|
||||
{
|
||||
StopCoroutine(_breathingCoroutine);
|
||||
_breathingCoroutine = null;
|
||||
}
|
||||
if (_headModuleGroup != null)
|
||||
{
|
||||
_headModuleGroup.transform.localPosition = _headModuleOriginalPos;
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator BreathingAnimation()
|
||||
{
|
||||
float time = 0;
|
||||
while (true)
|
||||
{
|
||||
time += Time.deltaTime;
|
||||
float yOffset = Mathf.Sin(time * 2f) * 0.02f; // 调整呼吸幅度和速度
|
||||
_headModuleGroup.transform.localPosition = _headModuleOriginalPos + new Vector3(0, yOffset, 0);
|
||||
yield return null;
|
||||
}
|
||||
// ReSharper disable once IteratorNeverReturns
|
||||
}
|
||||
|
||||
public void TriggerPainShake()
|
||||
{
|
||||
if (_painShakeCoroutine != null)
|
||||
{
|
||||
StopCoroutine(_painShakeCoroutine);
|
||||
}
|
||||
_painShakeCoroutine = StartCoroutine(PainShakeAnimation());
|
||||
}
|
||||
|
||||
private IEnumerator PainShakeAnimation()
|
||||
{
|
||||
StopBreathing();
|
||||
float duration = 0.5f;
|
||||
float elapsed = 0f;
|
||||
Vector3 originalPos = _headModuleGroup.transform.localPosition;
|
||||
|
||||
while (elapsed < duration)
|
||||
{
|
||||
elapsed += Time.deltaTime;
|
||||
float intensity = 1f - (elapsed / duration);
|
||||
_headModuleGroup.transform.localPosition = originalPos + new Vector3(
|
||||
UnityEngine.Random.Range(-0.1f, 0.1f) * intensity,
|
||||
UnityEngine.Random.Range(-0.1f, 0.1f) * intensity,
|
||||
UnityEngine.Random.Range(-0.1f, 0.1f) * intensity
|
||||
);
|
||||
yield return null;
|
||||
}
|
||||
|
||||
_headModuleGroup.transform.localPosition = originalPos;
|
||||
StartBreathing();
|
||||
}
|
||||
|
||||
public void StartContinuousPain()
|
||||
{
|
||||
if (_continuousPainCoroutine != null)
|
||||
{
|
||||
StopCoroutine(_continuousPainCoroutine);
|
||||
}
|
||||
_continuousPainCoroutine = StartCoroutine(ContinuousPainAnimation());
|
||||
}
|
||||
|
||||
public void StopContinuousPain()
|
||||
{
|
||||
if (_continuousPainCoroutine != null)
|
||||
{
|
||||
StopCoroutine(_continuousPainCoroutine);
|
||||
_continuousPainCoroutine = null;
|
||||
}
|
||||
if (_headModuleGroup != null)
|
||||
{
|
||||
_headModuleGroup.transform.localPosition = _headModuleOriginalPos;
|
||||
}
|
||||
StartBreathing();
|
||||
}
|
||||
|
||||
private IEnumerator ContinuousPainAnimation()
|
||||
{
|
||||
StopBreathing();
|
||||
while (true)
|
||||
{
|
||||
// 抖动阶段
|
||||
float shakeDuration = 1f;
|
||||
float shakeElapsed = 0f;
|
||||
Vector3 originalPos = _headModuleGroup.transform.localPosition;
|
||||
|
||||
while (shakeElapsed < shakeDuration)
|
||||
{
|
||||
shakeElapsed += Time.deltaTime;
|
||||
_headModuleGroup.transform.localPosition = originalPos + new Vector3(
|
||||
UnityEngine.Random.Range(-0.05f, 0.05f),
|
||||
UnityEngine.Random.Range(-0.05f, 0.05f),
|
||||
UnityEngine.Random.Range(-0.05f, 0.05f)
|
||||
);
|
||||
yield return null;
|
||||
}
|
||||
|
||||
// 暂停阶段
|
||||
_headModuleGroup.transform.localPosition = originalPos;
|
||||
yield return new WaitForSeconds(0.5f);
|
||||
}
|
||||
// ReSharper disable once IteratorNeverReturns
|
||||
}
|
||||
}
|
||||
|
||||
public struct BodyModuleSystemData
|
||||
[LoadIndex(2)]
|
||||
public class ModuleSystemData : IData
|
||||
{
|
||||
public bool hasHotErr;
|
||||
public BodyModuleData[] moduleDataArray;
|
||||
public ModuleState moduleState;
|
||||
|
||||
public IEnumerator Load()
|
||||
{
|
||||
FixSystemCenter.SystemDic.Get<BodyModuleSystem>().SwitchModuleGroup(moduleState);
|
||||
yield break;
|
||||
}
|
||||
}
|
||||
|
||||
public enum ModuleState
|
||||
{
|
||||
Body,
|
||||
Head
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user