217 lines
6.5 KiB
C#
217 lines
6.5 KiB
C#
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;
|
|
|
|
// public Transform _indicatorPos;
|
|
private BodyModuleSystem _bodyModuleSystem;
|
|
private Material _highlightMaterial;
|
|
private Material _alarmMaterial;
|
|
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<BodyModuleSystem>();
|
|
|
|
_targetSpriteRenderer = transform.Find("Module Pic").GetComponent<SpriteRenderer>();
|
|
_originalMaterial = _targetSpriteRenderer.GetComponent<SpriteRenderer>().material;
|
|
_highlightMaterial = Resources.Load<Material>("Materials/HighlightMaterial");
|
|
_alarmMaterial = Resources.Load<Material>("Materials/AlarmMaterial");
|
|
}
|
|
|
|
#region 插槽功能
|
|
|
|
public bool IsAvailable()
|
|
{
|
|
return _available;
|
|
}
|
|
|
|
public void SetSocketAvailable(bool isAvailable)
|
|
{
|
|
_available = isAvailable;
|
|
}
|
|
|
|
public void PlugIn()
|
|
{
|
|
// // 插入就在示波器显示图片
|
|
// if (!_bodyModuleSystem.inHotErr)
|
|
// {
|
|
// StartCoroutine(FixSystemCenter.SystemDic.Get<IOscilloscopeSystem>()
|
|
// ?.ShowScanImage(data.ScreenPic, data.checkPoints));
|
|
// }
|
|
|
|
if (DialogController.Instance)
|
|
// 然后触发Yarn节点
|
|
DialogController.Instance.StartDialogNode(_bodyModuleSystem.inHotErr ? "系统发热" : data.PlugInNodeName);
|
|
}
|
|
|
|
public void PlugOut()
|
|
{
|
|
FixSystemCenter.SystemDic.Get<IOscilloscopeSystem>()?.HideScanImage();
|
|
|
|
_bodyModuleSystem.oscilloscopeSystem.MessageBoxSetNull();
|
|
StartCoroutine(_bodyModuleSystem.oscilloscopeSystem.DisableDeepInButton());
|
|
|
|
// TODO 触发Yarn节点(似乎目前没有拔出对话?)
|
|
}
|
|
|
|
public void DeepIn()
|
|
{
|
|
if(DialogController.Instance)
|
|
DialogController.Instance.StartDialogNode(data.DeepInNodeName);
|
|
}
|
|
|
|
public Vector3 GetSocketPos()
|
|
{
|
|
return socketPos.position;
|
|
}
|
|
|
|
#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)
|
|
{
|
|
if (_targetSpriteRenderer != null && _highlightMaterial != null &&
|
|
_targetSpriteRenderer.material != _alarmMaterial)
|
|
{
|
|
_targetSpriteRenderer.material = enable ? _highlightMaterial : _originalMaterial;
|
|
}
|
|
}
|
|
|
|
public void Alarm(bool enable)
|
|
{
|
|
if (_targetSpriteRenderer != null && _alarmMaterial != null)
|
|
{
|
|
_targetSpriteRenderer.material = enable ? _alarmMaterial : _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;
|
|
|
|
// 功能信息
|
|
[Header("功能参数")] public bool cantStopRunning;
|
|
|
|
public string showPicPath;
|
|
public CheckPoint[] checkPoints;
|
|
|
|
// 只读
|
|
[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 Sprite ScreenPic =>
|
|
!string.IsNullOrEmpty(showPicPath) ? ResourceKit.LoadSpriteFromPsd(showPicPath) : null;
|
|
|
|
[JsonIgnore]
|
|
public Vector3 ModulePos
|
|
{
|
|
get => modulePos.ToVector3();
|
|
set => modulePos = new SerializableVector3(value);
|
|
}
|
|
|
|
[JsonIgnore]
|
|
public Vector3 SocketPos
|
|
{
|
|
get => socketPos.ToVector3();
|
|
set => socketPos = new SerializableVector3(value);
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public struct CheckPoint
|
|
{
|
|
public string id;
|
|
private SerializableVector3 _serializablePos3;
|
|
|
|
[JsonIgnore]
|
|
public Vector3 PointPos
|
|
{
|
|
get => _serializablePos3.ToVector3();
|
|
set => _serializablePos3 = new SerializableVector3(value);
|
|
}
|
|
}
|
|
} |