增加BodyModule切换功能
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using AibisDream.Framework;
|
||||
using Newtonsoft.Json;
|
||||
using UnityEngine;
|
||||
|
||||
@@ -10,11 +9,9 @@ namespace AibisDream.FixSystem
|
||||
#region 组件引用
|
||||
|
||||
private const string SocketObjName = "Socket";
|
||||
private const string ModulePicName = "Module Pic";
|
||||
|
||||
public Transform socketPos;
|
||||
|
||||
// public Transform _indicatorPos;
|
||||
private BodyModuleSystem _bodyModuleSystem;
|
||||
private Material _highlightMaterial;
|
||||
private Material _alarmMaterial;
|
||||
@@ -67,12 +64,6 @@ namespace AibisDream.FixSystem
|
||||
public void PlugOut()
|
||||
{
|
||||
}
|
||||
|
||||
public void DeepIn()
|
||||
{
|
||||
if(DialogController.Instance)
|
||||
DialogController.Instance.StartDialogNode(data.DeepInNodeName);
|
||||
}
|
||||
|
||||
public Vector3 GetSocketPos()
|
||||
{
|
||||
@@ -81,41 +72,6 @@ namespace AibisDream.FixSystem
|
||||
|
||||
#endregion
|
||||
|
||||
#region 模块数据保存与卸载
|
||||
|
||||
public void Load(BodyModuleData newData)
|
||||
{
|
||||
var modulePic = transform.Find(ModulePicName).GetComponent<SpriteRenderer>();
|
||||
var newSocketPos = transform.Find(SocketObjName);
|
||||
|
||||
gameObject.name = newData.moduleName;
|
||||
// 初始化位置与外形
|
||||
transform.localPosition = newData.ModulePos;
|
||||
modulePic.sprite = newData.ModulePic;
|
||||
newSocketPos.localPosition = newData.SocketPos;
|
||||
// 数据同步
|
||||
data = newData;
|
||||
|
||||
InitReference();
|
||||
}
|
||||
|
||||
public BodyModuleData Save()
|
||||
{
|
||||
var modulePic = transform.Find(ModulePicName).GetComponent<SpriteRenderer>();
|
||||
var newSocketPos = transform.Find(SocketObjName);
|
||||
|
||||
return new BodyModuleData
|
||||
{
|
||||
moduleName = data.moduleName,
|
||||
ModulePic = modulePic.sprite,
|
||||
ModulePos = transform.localPosition,
|
||||
SocketPos = newSocketPos.localPosition,
|
||||
cantStopRunning = data.cantStopRunning,
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 高亮提示和隐藏
|
||||
|
||||
public void Highlight(bool enable)
|
||||
@@ -142,42 +98,11 @@ namespace AibisDream.FixSystem
|
||||
public struct BodyModuleData
|
||||
{
|
||||
public const string PlugInNodeTemplate = "{0}PlugIn";
|
||||
public const string DeepInTemplate = "{0}DeepIn";
|
||||
|
||||
// ID
|
||||
public string moduleName;
|
||||
|
||||
// 外形信息
|
||||
public string modulePicPath;
|
||||
public SerializableVector3 modulePos;
|
||||
public SerializableVector3 socketPos;
|
||||
|
||||
// 功能信息
|
||||
[Header("功能参数")] public bool cantStopRunning;
|
||||
|
||||
// 只读
|
||||
[JsonIgnore] public string PlugInNodeName => string.Format(PlugInNodeTemplate, moduleName);
|
||||
[JsonIgnore] public string DeepInNodeName => string.Format(DeepInTemplate, moduleName);
|
||||
|
||||
[JsonIgnore]
|
||||
public Sprite ModulePic
|
||||
{
|
||||
get => !string.IsNullOrEmpty(modulePicPath) ? ResourceKit.LoadSpriteFromPsd(modulePicPath) : null;
|
||||
set => modulePicPath = ResourceKit.SaveSprite(value);
|
||||
}
|
||||
|
||||
[JsonIgnore]
|
||||
public Vector3 ModulePos
|
||||
{
|
||||
get => modulePos.ToVector3();
|
||||
set => modulePos = new SerializableVector3(value);
|
||||
}
|
||||
|
||||
[JsonIgnore]
|
||||
public Vector3 SocketPos
|
||||
{
|
||||
get => socketPos.ToVector3();
|
||||
set => socketPos = new SerializableVector3(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -9,12 +9,6 @@ namespace AibisDream
|
||||
private static BodyModuleSystem BodyModuleSystem => FixSystemCenter.SystemDic.Get<BodyModuleSystem>();
|
||||
private static PunchTapeSystem PunchTapeSystem => FixSystemCenter.SystemDic.Get<PunchTapeSystem>();
|
||||
|
||||
[YarnCommand("SetModuleState_CantStopRunning")]
|
||||
public static void SetModuleState_CantStopRunning(string moduleName, bool state)
|
||||
{
|
||||
BodyModuleSystem.SetModuleState_CantStopRunning(moduleName, state);
|
||||
}
|
||||
|
||||
[YarnCommand("ShowHeatMap")]
|
||||
public static void ShowHeatMap()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user