152 lines
4.1 KiB
C#
152 lines
4.1 KiB
C#
using AibisDream.Utility;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using Random = UnityEngine.Random;
|
|
|
|
namespace AibisDream.FixSystem
|
|
{
|
|
public class OscilloscopeSystem : MonoBehaviour
|
|
{
|
|
#region 组件索引
|
|
|
|
private TMP_Text _screenText;
|
|
private WaveformLine _waveformLine;
|
|
private ScanLine _scanLine;
|
|
private SpriteRenderer _screen;
|
|
private WaveSlider _waveSlider;
|
|
private SpriteButton _deepinButton;
|
|
|
|
#endregion
|
|
|
|
#region 状态
|
|
|
|
public bool isScanning;
|
|
public bool isPlaying;
|
|
public bool canScan;
|
|
|
|
#endregion
|
|
|
|
private void Awake()
|
|
{
|
|
FixSystemCenter.SystemDic.Register(this);
|
|
InitComponentRefs();
|
|
}
|
|
|
|
private void InitComponentRefs()
|
|
{
|
|
_screenText = transform.Find("Screen Text").GetComponent<TMP_Text>();
|
|
_waveformLine = transform.Find("Waveform Visualizer").GetComponent<WaveformLine>();
|
|
_scanLine = transform.Find("Scan Line").GetComponent<ScanLine>();
|
|
_screen = transform.Find("Screen").GetComponent<SpriteRenderer>();
|
|
_waveSlider = transform.Find("Wave Slider").GetComponent<WaveSlider>();
|
|
_deepinButton = new SpriteButton(transform.Find("Deepin Button").gameObject, false);
|
|
|
|
_waveformLine.SetLineSize(_screen.bounds.size.x, _screen.bounds.size.y);
|
|
_scanLine.SetScreenSize(_screen.bounds.size.x, _screen.bounds.size.y);
|
|
_screenText.text = "";
|
|
|
|
_deepinButton.OnButtonDown += StartScan;
|
|
_scanLine.ScanErrorEvent += OnScanSuccess;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
_deepinButton.CheckButtonClick();
|
|
}
|
|
|
|
#region 对外开放
|
|
|
|
/// <summary>
|
|
/// 屏幕上播放波形
|
|
/// </summary>
|
|
/// <param name="waveformType">波形类型</param>
|
|
/// <param name="hasError">是否有错误</param>
|
|
public void PlayWave(WaveformType waveformType, bool hasError)
|
|
{
|
|
_waveSlider.InitSlider();
|
|
isPlaying = true;
|
|
canScan = hasError;
|
|
_deepinButton.SetActive(canScan);
|
|
|
|
var initialRandomPhase = Mathf.PI * Random.Range(7, 13) * 0.1f;
|
|
_waveformLine.PlayWave(waveformType, initialRandomPhase, hasError);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 停止播放波形
|
|
/// </summary>
|
|
public void StopWave()
|
|
{
|
|
isPlaying = false;
|
|
canScan = false;
|
|
isScanning = false;
|
|
_deepinButton.SetActive(false);
|
|
_waveSlider.InitSlider();
|
|
|
|
_waveformLine.StopWave();
|
|
_scanLine.CloseScan();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 调整相位
|
|
/// </summary>
|
|
public void AdjustPhaseShift(float phaseShift)
|
|
{
|
|
if (isPlaying)
|
|
{
|
|
_waveformLine.AdjustPhaseShift(phaseShift);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 开始扫描
|
|
/// </summary>
|
|
public void StartScan()
|
|
{
|
|
if (!canScan) return;
|
|
|
|
float spikeX = _waveformLine.GetSpikeX();
|
|
StartCoroutine(_scanLine.ScanForError(spikeX));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 扫描成功后
|
|
/// </summary>
|
|
public void OnScanSuccess()
|
|
{
|
|
FixSystemCenter.SystemDic.Get<BodyModuleSystem>().HandleDeepIn();
|
|
_ = CommonUtil.Delay(2000, StopWave);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 显示消息
|
|
/// </summary>
|
|
/// <param name="message">消息</param>
|
|
public void ShowScreenMessage(string message)
|
|
{
|
|
_screenText.text = message;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 清除消息
|
|
/// </summary>
|
|
public void ClearScreenMessage()
|
|
{
|
|
_screenText.text = "";
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
|
|
public enum WaveformType
|
|
{
|
|
Default,
|
|
Sine,
|
|
Square,
|
|
Triangle,
|
|
Sawtooth,
|
|
HotNoise,
|
|
Composite,
|
|
Pulse
|
|
}
|
|
} |