2025-10-19 19:38:52 +08:00
|
|
|
using System;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace Script.Gameplay.Connect
|
|
|
|
{
|
|
|
|
// 只负责在场景中绘制连接线,并处理信号传递
|
|
|
|
public class ConnectionLine : MonoBehaviour
|
|
|
|
{
|
2025-10-20 19:40:55 +08:00
|
|
|
[SerializeField] private MonoBehaviour monoPointA;
|
|
|
|
[SerializeField] private MonoBehaviour monoPointB;
|
|
|
|
private IConnectable _pointA;
|
|
|
|
private IConnectable _pointB;
|
2025-10-19 19:38:52 +08:00
|
|
|
|
|
|
|
private LineRenderer line;
|
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
{
|
2025-10-20 19:40:55 +08:00
|
|
|
_pointA = monoPointA as IConnectable;
|
|
|
|
_pointB = monoPointB as IConnectable;
|
2025-10-19 19:38:52 +08:00
|
|
|
|
|
|
|
line = GetComponent<LineRenderer>();
|
|
|
|
|
2025-10-20 19:40:55 +08:00
|
|
|
SetLineRendererPositions();
|
2025-10-19 19:38:52 +08:00
|
|
|
}
|
|
|
|
|
2025-10-20 19:40:55 +08:00
|
|
|
public void SetConnectable(IConnectable pointA, IConnectable pointB)
|
2025-10-19 19:38:52 +08:00
|
|
|
{
|
2025-10-20 19:40:55 +08:00
|
|
|
if(pointA == null || pointB == null)
|
2025-10-19 19:38:52 +08:00
|
|
|
{
|
2025-10-20 19:40:55 +08:00
|
|
|
Debug.Log("ConnectionLine requires two valid IConnectable points.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (pointA == pointB)
|
|
|
|
{
|
|
|
|
Debug.Log("ConnectionLine cannot connect the same point.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_pointA = pointA;
|
|
|
|
_pointB = pointB;
|
|
|
|
pointA.ConnectionLines.Add(this);
|
|
|
|
pointB.ConnectionLines.Add(this);
|
|
|
|
SetLineRendererPositions();
|
2025-10-19 19:38:52 +08:00
|
|
|
}
|
2025-10-20 19:40:55 +08:00
|
|
|
|
|
|
|
private void SetLineRendererPositions()
|
2025-10-19 19:38:52 +08:00
|
|
|
{
|
2025-10-20 19:40:55 +08:00
|
|
|
if (_pointA != null && _pointB != null)
|
|
|
|
{
|
|
|
|
line.SetPositions(new Vector3[]
|
|
|
|
{
|
|
|
|
_pointA.GetPosition(),
|
|
|
|
_pointB.GetPosition()
|
|
|
|
});
|
|
|
|
}
|
2025-10-19 19:38:52 +08:00
|
|
|
}
|
2025-10-20 10:33:00 +08:00
|
|
|
|
2025-10-20 19:40:55 +08:00
|
|
|
public void SignalActive(bool active, GameObject sender)
|
2025-10-20 10:33:00 +08:00
|
|
|
{
|
2025-10-20 19:40:55 +08:00
|
|
|
if (_pointA != null && _pointB != null)
|
|
|
|
{
|
|
|
|
if (sender == _pointA.GetGameObject())
|
|
|
|
{
|
|
|
|
_pointB.SignalActive(active, sender);
|
|
|
|
}
|
|
|
|
else if (sender == _pointB.GetGameObject())
|
|
|
|
{
|
|
|
|
_pointA.SignalActive(active, sender);
|
|
|
|
}
|
|
|
|
}
|
2025-10-20 10:33:00 +08:00
|
|
|
}
|
2025-10-19 19:38:52 +08:00
|
|
|
|
|
|
|
private void OnDestroy()
|
|
|
|
{
|
2025-10-20 19:40:55 +08:00
|
|
|
_pointA.ConnectionLines.Remove(this);
|
|
|
|
_pointB.ConnectionLines.Remove(this);
|
2025-10-19 19:38:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|