Files
aibis-dream/Assets/Scripts/FixSystem/Scope/OscilloscopeController.cs
T

438 lines
14 KiB
C#

using UnityEngine;
using System.Collections.Generic;
using System.Collections;
using RainbowArt.CleanFlatUI;
using UnityEngine.UI;
using System.Text.RegularExpressions;
using DG.Tweening;
using TMPro;
using AibisDream;
using Yarn.Unity;
using Unity.VisualScripting;
public class OscilloscopeController : MonoBehaviour
{
public WaveformVisualizer waveformVisualizer;
public LineRenderer scanLine;
//public SliderCircular phaseSlider;
public Slider phaseSlider;
public SliderCircular checkLine;
public Button DeepInButton;
private float phaseShiftMax = Mathf.PI * 1.5f;
private Plug plug;
private EyeTransitionManager transitionManager;
private bool setInitSliderValue = false;
private float scanSpeed = 2f;
public TMP_Text screenText;
private Socket currentSocket;
private int heatNum = 1;
public int HeatNum
{
get { return heatNum; }
set
{
heatNum = value;
}
}
private bool cantReduce = false;
public GameObject heatMap;
HeatMapController hm;
//public GameObject heatMapButton;
void Start()
{
heatNum = 0;
scanLine.startWidth = 0.05f;
scanLine.endWidth = 0.05f;
scanLine.gameObject.SetActive(false);
transitionManager = FindObjectOfType<EyeTransitionManager>();
plug = FindObjectOfType<Plug>();
DeepInButton.GetComponent<Button>().onClick.AddListener(DeepIn);
screenText.text = "";
currentSocket = null;
if (plug != null)
{
plug.OnPlugInserted += HandlePlugInserted;
plug.OnPlugRemoved += HandlePlugRemoved;
}
hm = FindObjectOfType<HeatMapController>();
}
[YarnCommand("SetHeat")]
public void SetHeat(int heat, bool unReduceAble = false)
{
cantReduce = unReduceAble;
HeatNum=heat;
hm.SetTemptureDirect(heat);
}
[YarnCommand("ShowHeatMap")]
public void ShowHeatMap(bool show)
{
heatMap.SetActive(show);
heatMap.GetComponent<DraggableUI>().isDone = false;
}
public void StartReduceHeat()
{
StartCoroutine(ReduceValueCoroutine());
}
IEnumerator ReduceValueCoroutine()
{
if (cantReduce)
{
Debug.Log("降温失败");
heatMap.GetComponent<DraggableUI>().isDone = true;
yield return new WaitForSeconds(1);
DialogController.Instance.StartDialogNode("降温失败");
yield break;
}
else
{
Debug.Log("降温成功");
heatMap.GetComponent<DraggableUI>().isDone = true;
float duration = 2f; // 动画时长为 2 秒
// 使用 DOTween 来动画化数值变化
HeatNum=0;
hm.SetTemptureSmooth(HeatNum,duration);
yield return new WaitForSeconds(duration);
//yield return DOTween.To(() => HeatNum, x => HeatNum = x, 0, duration).WaitForCompletion();
DialogController.Instance.StartDialogNode("降温成功");
// 动画完成后,数值已经降为 0,可以在这里添加进一步的逻辑
Debug.Log("Value has reached zero.");
}
}
private void HandlePlugInserted(Plug plug)
{
string originalString = plug.currentSocket.name;
string trimmedString = originalString.Replace("Socket", "");
DialogController.Instance.SetVariable("$currentSocket", trimmedString);
DialogController.Instance.StartDialogNode("PlugInsert");
// 这里可以处理插入后的波形显示逻辑
Debug.Log("Plug inserted into the socket, starting waveform display.");
}
private void HandlePlugRemoved()
{
// 这里可以处理移除后的清除波形逻辑
screenText.text = "";
Debug.Log("Plug removed from the socket, stopping waveform display.");
}
public void DeepIn()
{
if (plug == null)
{
Debug.Log("未找到插头");
return;
}
currentSocket = plug.currentSocket;
if (currentSocket == null || currentSocket.targetData == null)
{
Debug.Log("未插入任何目标模块");
screenText.text = "未插入任何目标模块";
return;
}
string originalString = currentSocket.name;
string trimmedString = originalString.Replace("Socket", "");
string yarnString = "On" + trimmedString + "DeepIn";
if (currentSocket.WaitYarnCommand)
{
DialogController.Instance.StartDialogNode(yarnString);
// DialogController.Instance.SetVariable("$ViewToDeepIn",trimmedString);
// DialogController.Instance.StartDialogNode("DeepInView");
}
else if(heatNum>0)
{
screenText.text="模块过热,无法深入";
screenText.color=Color.red;
//transitionManager.SetViewAvailability(trimmedString, true);
//DeepInButton.interactable=false;
//HandleDeepIn();
}
else if(currentSocket.targetData.IsRunning())
{
screenText.text="记忆正在被外部连续调取,无法进入";
screenText.color=Color.red;
}
else if(currentSocket.targetData.HasExternalError())
{
//transitionManager.SetViewAvailability(trimmedString, true);
DeepInButton.interactable=false;
HandleDeepIn();
}
else
{
StartCoroutine(ScanForError(trimmedString)); // 启动扫描协程
}
}
private void DeepInexecution()
{
string originalString = currentSocket.name;
string trimmedString = originalString.Replace("Socket", "");
StartCoroutine(transitionManager.StartTransitionCoroutine(trimmedString));
DeepInButton.interactable=true;
phaseSlider.interactable=true;
//screenText.text="";
plug.Enable_plugInput();
DialogController.OnDialogueComplete-=DeepInexecution;
}
private void HandleDeepIn()
{
if (currentSocket.notAlowedDeep)
{
DialogController.OnDialogueComplete+=DeepInexecution;
}
string originalString = currentSocket.name;
string trimmedString = originalString.Replace("Socket", "");
string yarnString = "On" + trimmedString + "DeepIn";
if (DialogController.Instance.CheckIfNodeExistInCurrentYarnProject(yarnString))
{
DialogController.Instance.StartDialogNode(yarnString);
if (!currentSocket.notAlowedDeep)
{
DialogController.OnDialogueComplete += DeepInexecution;
}
}
else
{
if (!currentSocket.notAlowedDeep)
{
DeepInexecution();
}
}
}
void HandleExternalError()
{
scanLine.gameObject.SetActive(false);
phaseSlider.interactable = false;
waveformVisualizer.gameObject.SetActive(false);
screenText.text = "外部错误已关联,可以直接深入";
}
private IEnumerator ScanForError(string trimmedString)
{
phaseSlider.interactable = false;
plug.Disable_plugInput();
scanLine.gameObject.SetActive(true);
// 设置扫描线初始位置(左侧)
float startX = -waveformVisualizer.spriteWidth / 2;
float endX = waveformVisualizer.spriteWidth / 2;
float currentX = startX;
// 上下点的位置,x 相同,y 为正负数
Vector3 topPosition = new Vector3(currentX, waveformVisualizer.spriteHeight / 2, 0);
Vector3 bottomPosition = new Vector3(currentX, -waveformVisualizer.spriteHeight / 2, 0);
scanLine.SetPosition(0, topPosition);
scanLine.SetPosition(1, bottomPosition);
//float scanSpeed = waveformVisualizer.scanSpeed;
bool isErrorFound = false;
// 计算目标位置对应的 x 值
float spikeX =
(waveformVisualizer.SpikeIndex / (float)waveformVisualizer.samples) * waveformVisualizer.spriteWidth -
waveformVisualizer.spriteWidth / 2;
float previousX = currentX;
screenText.text = "搜寻中...";
// 从左到右扫描
while (currentX <= endX)
{
// 更新扫描线的位置,x 向右移动
currentX += Time.deltaTime * scanSpeed;
topPosition.x = currentX;
bottomPosition.x = currentX;
scanLine.SetPosition(0, topPosition);
scanLine.SetPosition(1, bottomPosition);
// 检查是否跨越了 spikeX
if (waveformVisualizer.isErrorFind && ((previousX < spikeX && currentX >= spikeX) ||
(previousX > spikeX && currentX <= spikeX)))
{
isErrorFound = true;
topPosition.x = spikeX;
bottomPosition.x = spikeX;
scanLine.SetPosition(0, topPosition);
scanLine.SetPosition(1, bottomPosition);
scanLine.startColor = Color.red;
scanLine.endColor = Color.red;
//Debug.Log("找到错误,可以开始深入");
//screenText.text="找到错误,可以开始深入";
yield return new WaitForSeconds(1f);
// transitionManager.SetViewAvailability(trimmedString, true);
DeepInButton.interactable = false;
HandleDeepIn();
break;
}
previousX = currentX; // 更新 previousX 为当前的 currentX
yield return null; // 等待下一帧
}
if (!isErrorFound)
{
Debug.Log("未找到错误,无法深入");
screenText.text = "未找到错误,无法深入";
//transitionManager.SetViewAvailability(trimmedString, false);
}
// 扫描完成,停留片刻后隐藏扫描线
yield return new WaitForSeconds(0.1f);
scanLine.gameObject.SetActive(false);
scanLine.startColor = Color.white;
scanLine.endColor = Color.white;
phaseSlider.interactable = true;
plug.Enable_plugInput();
//screenText.text="";
}
void Update()
{
if (plug != null)
{
var currentSocket = plug.currentSocket;
if (currentSocket != null && currentSocket.targetData != null)
{
var targetData = currentSocket.targetData;
if (targetData != null)
{
if (heatNum > 0)
{
screenText.text="模块过热,无法深入";
screenText.color=Color.red;
waveformVisualizer.waveformType = WaveformType.Noise;
waveformVisualizer.clarity = 1;
return;
}
else if(heatNum<=0)
{
screenText.text="";
}
if(targetData.IsRunning())
{
screenText.text="记忆正在被外部连续调取,无法进入";
screenText.color=Color.red;
//waveformVisualizer.gameObject.SetActive(false);
return;
}
else if(targetData.HasExternalError())
{
screenText.text="记忆模块,允许直接进入";
screenText.color=Color.yellow;
//waveformVisualizer.gameObject.SetActive(false);
return;
}
else if (currentSocket.targetData.isCorrect)
{
string originalString = currentSocket.name;
string trimmedString = originalString.Replace("Socket", "");
screenText.text = trimmedString + ":未检测到错误";
screenText.color = Color.green;
return;
}
else if (!currentSocket.targetData.isCorrect)
{
string originalString = currentSocket.name;
string trimmedString = originalString.Replace("Socket", "");
screenText.text = trimmedString + ":异常,拖拽滑动条,寻找波形抖动并深入";
screenText.color = Color.red;
//DialogController.Instance.StartDialogNode("发现异常模块");
}
if (!setInitSliderValue)
{
setInitSliderValue = true;
phaseSlider.value = targetData.sliderValue;
}
waveformVisualizer.waveformType = targetData.waveformType;
waveformVisualizer.clarity = 1;
float phaseShift = 0f;
if (phaseSlider != null)
{
float sliderPhase = (phaseSlider.value / 8f) * phaseShiftMax;
phaseShift = sliderPhase + targetData.initialRandomPhase;
targetData.sliderValue = phaseSlider.value;
}
if (phaseSlider.value > 0)
{
waveformVisualizer.SetPhaseShift(phaseShift);
}
else
{
waveformVisualizer.SetPhaseShift(0);
}
waveformVisualizer.gameObject.SetActive(true);
if (!targetData.isCorrect)
{
waveformVisualizer.SetError(targetData);
}
}
}
else
{
ResetVisualizer();
}
}
}
void ResetVisualizer()
{
screenText.text="未插入有效模块";
screenText.color=Color.white;
waveformVisualizer.waveformType = WaveformType.Noise;
waveformVisualizer.clarity = 0;
waveformVisualizer.ResetWaveform();
waveformVisualizer.gameObject.SetActive(true);
phaseSlider.value = 0;
waveformVisualizer.SetPhaseShift(0);
setInitSliderValue = false;
}
}