using System; using AibisDream.Framework; using Newtonsoft.Json; using UnityEngine; namespace AibisDream.FixSystem { public class BodyModule : MonoBehaviour, ISocket { #region 组件引用 private const string SocketObjName = "Socket"; private const string ModulePicName = "Module Pic"; public Transform _socketPos; private BodyModuleSystem _bodyModuleSystem; private Material highlightMaterial; private Material originalMaterial; // 原材质 private SpriteRenderer targetSpriteRenderer; // 目标的 SpriteRenderer #endregion // 模块基本数据 [SerializeField] public BodyModuleData data; private bool available = true; private void Awake() { // 获取组件索引 InitReference(); } private void InitReference() { _socketPos = transform.Find(SocketObjName); _bodyModuleSystem = transform.parent.parent.GetComponent(); targetSpriteRenderer=transform.Find("Module Pic").GetComponent(); originalMaterial = GetComponent().material; highlightMaterial = Resources.Load("Materials/HighlightMaterial"); if (highlightMaterial != null) { Debug.Log("成功加载高亮材质!"); } else { Debug.LogError("未找到高亮材质!请检查路径和名称是否正确。"); } } #region 插槽功能 public bool IsAvailable() { return available; } public void SetSocketAvailable(bool isAvailable) { available=isAvailable; } public void PlugIn() { // 插入后先播放wave ShowWave(); if(DialogController.Instance!=null) // 然后触发Yarn节点 DialogController.Instance.StartDialogNode(_bodyModuleSystem.inHotErr ? "系统过热" : data.PlugInNodeName); } public void PlugOut() { // 先关闭wave _bodyModuleSystem.oscilloscopeSystem.ShowScreenMessage("未插入任何模块", Color.white); CloseWave(); // TODO 触发Yarn节点(似乎目前没有拔出对话?) } public void DeepIn() { if (!data.waveHasError&&!data.ignoreWaveCheck) return; // 如果有错误并且已经深入了 if(DialogController.Instance!=null) DialogController.Instance.StartDialogNode(data.DeepInNodeName); } public void CloseWave() { _bodyModuleSystem.oscilloscopeSystem.StopWave(); } public void ShowWave() { if (_bodyModuleSystem.inHotErr) { _bodyModuleSystem.oscilloscopeSystem.ShowScreenMessage("发热,无法检查", Color.red); _bodyModuleSystem.oscilloscopeSystem.PlayWave(WaveformType.HotNoise, false); } else if(data.cantStopRunning) { _bodyModuleSystem.oscilloscopeSystem.ShowScreenMessage("持续运行中,无法进入", Color.red); //_bodyModuleSystem.oscilloscopeSystem.ChangeDeepInButtonState(true); } else if(data.ignoreWaveCheck) { _bodyModuleSystem.oscilloscopeSystem.ShowScreenMessage("无需检查,允许进入", Color.yellow); _bodyModuleSystem.oscilloscopeSystem.ChangeDeepInButtonState(true); } else if(data.waveHasError) { _bodyModuleSystem.oscilloscopeSystem.ShowScreenMessage(data.moduleName+": 发现错误,拖动滑动条寻找错误波形以深入检查", Color.red); _bodyModuleSystem.oscilloscopeSystem.PlayWave(data.waveformType, data.waveHasError); } else { _bodyModuleSystem.oscilloscopeSystem.ShowScreenMessage(data.moduleName+": 未发现错误,无寻深入", Color.green); } } public Vector3 GetSocketPos() { return _socketPos.position; } #endregion #region 模块数据保存与卸载 public void Load(BodyModuleData newData) { var modulePic = transform.Find(ModulePicName).GetComponent(); var socketPos = transform.Find(SocketObjName); gameObject.name = newData.moduleName; // 初始化位置与外形 transform.localPosition = newData.ModulePos; modulePic.sprite = newData.ModulePic; socketPos.localPosition = newData.SocketPos; // 数据同步 data = newData; InitReference(); } public BodyModuleData Save() { var modulePic = transform.Find(ModulePicName).GetComponent(); var socketPos = transform.Find(SocketObjName); return new BodyModuleData { moduleName = data.moduleName, ModulePic = modulePic.sprite, ModulePos = transform.localPosition, SocketPos = socketPos.localPosition, waveformType = data.waveformType, waveHasError = data.waveHasError, ignoreWaveCheck = data.ignoreWaveCheck, cantStopRunning=data.cantStopRunning }; } #endregion #region 高亮提示 public void Highlight(bool enable) { if (targetSpriteRenderer != null && highlightMaterial != null) { targetSpriteRenderer.material = enable ? highlightMaterial : originalMaterial; } } #endregion } [Serializable] 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; // 功能信息 public WaveformType waveformType; public bool waveHasError; public bool ignoreWaveCheck; 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); } } }