可配置模块

This commit is contained in:
2024-11-30 08:04:03 +08:00
parent d95d117c08
commit 71ec5da49a
45 changed files with 3249 additions and 1262 deletions
+137 -31
View File
@@ -1,30 +1,40 @@
using System;
using System.Linq;
using AibisDream.Framework;
using AibisDream.Kit;
using AibisDream.Utility;
using UnityEngine;
using UnityEngine.Events;
namespace AibisDream.FixSystem
{
public class BodyModuleSystem : MonoBehaviour
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;
public Transform bodyModuleGroup;
#endregion
#region
#region
public UnityEvent<Socket> PlugInEvent => CableSystem.plugInEvent;
public UnityEvent<Socket> PlugOutEvent => CableSystem.plugOutEvent;
public override string CurDataKey { get; set; }
private string[] _keyOptions;
public UnityEvent PlugStartDrag => CableSystem.plugStartDrag;
public UnityEvent PlugEndDrag => CableSystem.plugEndDrag;
#endregion
[SerializeField]
public BodyModuleSystemConfig config = BodyModuleSystemConfig.GeneDefaultConfig();
#region
[HideInInspector] public UnityEvent<ISocket> plugInEvent;
[HideInInspector] public UnityEvent<ISocket> plugOutEvent;
#endregion
[SerializeField] public float plugSnappingRange = 1f;
private void Awake()
{
@@ -36,6 +46,8 @@ namespace AibisDream.FixSystem
// 处理内部索引
BodyModules = transform.GetComponentsInChildren<BodyModule>();
CableSystem = transform.Find("Cable System").GetComponent<CableSystem>();
bodyModuleGroup = transform.Find("Body Module Group");
FixSystemCenter.SystemDic.Register(this);
}
@@ -49,11 +61,11 @@ namespace AibisDream.FixSystem
{
var closestDistance = Mathf.Infinity;
BodyModule closestModule = null;
// 寻找最接近的BodyModule
foreach (var bodyModule in BodyModules)
{
float tempDistance = Vector3.Distance(sourcePos, bodyModule.SocketPos);
float tempDistance = Vector3.Distance(sourcePos, bodyModule.GetSocketPos());
if (tempDistance < closestDistance)
{
@@ -61,9 +73,9 @@ namespace AibisDream.FixSystem
closestModule = bodyModule;
}
}
// 判断距离是否在目标范围内,在的话就返回,不在的话返回个空的
if (closestDistance < config.plugSnappingRange && closestModule)
if (closestDistance < plugSnappingRange && closestModule)
{
targetModule = closestModule;
return true;
@@ -74,7 +86,111 @@ namespace AibisDream.FixSystem
return false;
}
}
/// <summary>
/// 深入检查
/// </summary>
public void HandleDeepIn()
{
if (CableSystem.curBodyModule)
{
CableSystem.curBodyModule.DeepIn();
}
}
#region
public override string[] GetDataKeyOptions()
{
return _keyOptions ??= ConfigUtil.Instance.GetFileNames(DataPath);
}
public override void Load()
{
if (string.IsNullOrEmpty(CurDataKey))
{
Debug.Log("配置项名称为空");
return;
}
string formattedKey = JsonUtil.FormatAsJsonPath(CurDataKey);
if (!ConfigUtil.Instance.TryLoadConfig<BodyModuleSystemData>(DataPath + formattedKey,
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)
{
var oldModules = GetComponentsInChildren<BodyModule>();
// 先销毁旧的Module
foreach (var oldModule in oldModules)
{
#if UNITY_EDITOR
DestroyImmediate(oldModule.gameObject);
#else
Destory(oldModule);
#endif
}
// 再加载新的Module
foreach (var newModuleData in systemData.moduleDataArray)
{
BodyModule newModule = Instantiate(bodyModulePrefab, bodyModuleGroup).GetComponent<BodyModule>();
newModule.Load(newModuleData);
}
}
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
};
string formattedKey = JsonUtil.FormatAsJsonPath(key);
ConfigUtil.Instance.SaveConfig(systemData, DataPath + formattedKey);
}
public void AddBodyModule()
{
Instantiate(bodyModulePrefab, bodyModuleGroup);
}
#endregion
/// <summary>
/// 插线系统是否插在插槽里
/// </summary>
@@ -83,20 +199,10 @@ namespace AibisDream.FixSystem
{
return CableSystem.curSocket != null;
}
[Serializable]
public struct BodyModuleSystemConfig
{
public float plugSnappingRange;
public static BodyModuleSystemConfig GeneDefaultConfig()
{
return new BodyModuleSystemConfig
{
plugSnappingRange = 1f
};
}
}
}
}
public struct BodyModuleSystemData
{
public BodyModuleData[] moduleDataArray;
}
}