This commit is contained in:
2024-11-15 01:24:07 +08:00
parent fccc58f5b9
commit db5b4fc2ed
23 changed files with 606 additions and 399 deletions
+114
View File
@@ -0,0 +1,114 @@
using System;
using System.Collections;
using UnityEngine;
namespace AibisDream.FixSystem
{
public class ScanLine : MonoBehaviour
{
#region
private OscilloscopeScreen _screen;
private LineRenderer _line;
#endregion
#region 线
private bool _isScanning;
#endregion
#region screen获取
private float _screenHeight;
private float _screenWidth;
private float _screenLeftX;
private float _screenRightX;
private float _topY;
private float _bottomY;
private float _spikeX;
private float _scanSpeed;
private event Action ScanErrorEvent;
#endregion
private void Awake()
{
InitComponentRefs();
}
private void InitComponentRefs()
{
_screen = transform.parent.GetComponent<OscilloscopeScreen>();
_line = GetComponent<LineRenderer>();
}
public IEnumerator ScanForError()
{
// 初始化扫描线
InitStartLine();
// 扫描线右移
var currentX = _screenLeftX;
var lastX = currentX;
bool isSpikeFound = false;
while (currentX <= _screenRightX)
{
// 横坐标右移
currentX += Time.deltaTime * _scanSpeed;
SetLinePos(currentX);
// 检查是否扫到尖峰
if (CheckSpikeInRange(lastX, currentX))
{
isSpikeFound = true;
// 修改线条
MarkLineAsError();
// 触发扫描事件
yield return new WaitForSeconds(1f);
ScanErrorEvent?.Invoke();
}
}
}
private void InitStartLine()
{
// 初始状态是一根靠左的竖线
SetLinePos(_screenLeftX);
_line.enabled = true;
}
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)
{
var left = Mathf.Min(x1, x2);
var right = Mathf.Max(x1, x2);
return _spikeX >= left && _spikeX <= right;
}
private void MarkLineAsError()
{
SetLinePos(_spikeX);
_line.startColor = Color.red;
_line.endColor = Color.red;
}
public void CloseScan()
{
_line.enabled = false;
}
}
}