This commit is contained in:
2024-12-12 01:07:26 +08:00
parent 2667419d98
commit 3fcff0598a
55 changed files with 1861 additions and 964 deletions
@@ -0,0 +1,123 @@
using System;
using System.Collections;
using UnityEngine;
namespace AibisDream.FixSystem
{
public class ScanLine : MonoBehaviour
{
#region
private LineRenderer _line;
private OscilloscopeSystem _oscilloscopeSystem;
#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()
{
_oscilloscopeSystem = GetComponentInParent<OscilloscopeSystem>();
_line = GetComponent<LineRenderer>();
_line.enabled = false;
_line.positionCount = 2;
}
public IEnumerator ScanForError(float spikeX)
{
_line.enabled = true;
_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;
}
_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;
}
}
}