Files
aibis-dream/Assets/Scripts/FixSystemNew/ScanLine.cs
T
2024-11-30 08:04:03 +08:00

120 lines
3.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections;
using UnityEngine;
namespace AibisDream.FixSystem
{
public class ScanLine : MonoBehaviour
{
#region 组件索引
private LineRenderer _line;
#endregion
#region 部分参数(后面改成从screen获取
private float _screenLeftX;
private float _screenRightX;
private float _topY;
private float _bottomY;
private const float ScanSpeed = 2f;
public event Action ScanErrorEvent;
#endregion
private void Awake()
{
InitComponentRefs();
}
private void InitComponentRefs()
{
_line = GetComponent<LineRenderer>();
_line.enabled = false;
_line.positionCount = 2;
}
public IEnumerator ScanForError(float spikeX)
{
_line.enabled = true;
FixSystemCenter.SystemDic.Get<OscilloscopeSystem>().isScanning = true;
// 初始化扫描线
InitStartLine();
// 扫描线右移
var currentX = _screenLeftX;
var lastX = currentX;
while (currentX <= _screenRightX)
{
// 横坐标右移
currentX += Time.deltaTime * ScanSpeed;
SetLinePos(currentX);
// 检查是否扫到尖峰
if (CheckSpikeInRange(lastX, currentX, spikeX))
{
// 修改线条
MarkLineAsError(spikeX);
// 触发扫描事件
yield return new WaitForSeconds(1f);
ScanErrorEvent?.Invoke();
break;
}
lastX = currentX;
yield return null;
}
FixSystemCenter.SystemDic.Get<OscilloscopeSystem>().isScanning = false;
}
private void InitStartLine()
{
// 初始状态是一根靠左的竖线
SetLinePos(_screenLeftX);
_line.enabled = true;
_line.startColor = Color.white;
_line.endColor = Color.white;
}
private void SetLinePos(float posX)
{
var topPosition = new Vector3(posX, _topY, 0);
var bottomPosition = new Vector3(posX, _bottomY, 0);
_line.SetPosition(0, topPosition);
_line.SetPosition(1, bottomPosition);
}
private bool CheckSpikeInRange(float x1, float x2, float spikeX)
{
var left = Mathf.Min(x1, x2);
var right = Mathf.Max(x1, x2);
return spikeX >= left && spikeX <= right;
}
private void MarkLineAsError(float spikeX)
{
SetLinePos(spikeX);
_line.startColor = Color.red;
_line.endColor = Color.red;
}
public void CloseScan()
{
_line.enabled = false;
}
public void SetScreenSize(float width, float height)
{
_screenLeftX = - width / 2;
_screenRightX = width / 2;
_topY = height / 2;
_bottomY = - height / 2;
}
}
}