2025-10-19 19:38:52 +08:00
|
|
|
using System;
|
|
|
|
|
using UnityEngine;
|
2025-10-22 17:34:58 +08:00
|
|
|
using Script.Gameplay.Interface;
|
2025-10-19 19:38:52 +08:00
|
|
|
|
|
|
|
|
namespace Script.Gameplay.Connect
|
|
|
|
|
{
|
|
|
|
|
// 只负责在场景中绘制连接线,并处理信号传递
|
2025-10-22 17:34:58 +08:00
|
|
|
public class ConnectionLine : MonoBehaviour, ISignalReceiver, ISignalSender
|
2025-10-19 19:38:52 +08:00
|
|
|
{
|
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
|
|
|
|
2025-10-27 10:31:01 +08:00
|
|
|
public IConnectable PointA
|
|
|
|
|
{
|
|
|
|
|
get => _pointA;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IConnectable PointB
|
|
|
|
|
{
|
|
|
|
|
get => _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-22 17:34:58 +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;
|
|
|
|
|
}
|
2025-10-27 10:31:01 +08:00
|
|
|
|
2025-10-20 19:40:55 +08:00
|
|
|
if (pointA == pointB)
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("ConnectionLine cannot connect the same point.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-10-27 10:31:01 +08:00
|
|
|
|
2025-10-20 19:40:55 +08:00
|
|
|
_pointA = pointA;
|
|
|
|
|
_pointB = pointB;
|
|
|
|
|
pointA.ConnectionLines.Add(this);
|
|
|
|
|
pointB.ConnectionLines.Add(this);
|
|
|
|
|
SetLineRendererPositions();
|
2025-10-19 19:38:52 +08:00
|
|
|
}
|
2025-10-22 17:34:58 +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-22 17:34:58 +08:00
|
|
|
|
|
|
|
|
// public void SignalActive(bool active, GameObject sender)
|
|
|
|
|
// {
|
|
|
|
|
// if (_pointA != null && _pointB != null)
|
|
|
|
|
// {
|
|
|
|
|
// if (sender == _pointA.GetGameObject())
|
|
|
|
|
// {
|
|
|
|
|
// _pointB.OnSignalReceived(active, sender);
|
|
|
|
|
// }
|
|
|
|
|
// else if (sender == _pointB.GetGameObject())
|
|
|
|
|
// {
|
|
|
|
|
// _pointA.OnSignalReceived(active, sender);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
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
|
|
|
}
|
2025-10-22 17:34:58 +08:00
|
|
|
|
2025-10-26 22:13:15 +08:00
|
|
|
public int NeedSignalCount { get; set; }
|
|
|
|
|
public int CurrentNeedSignalCount { get; set; }
|
|
|
|
|
|
2025-10-22 17:34:58 +08:00
|
|
|
public void OnSignalReceived(bool active, GameObject sender)
|
|
|
|
|
{
|
2025-10-28 08:50:06 +08:00
|
|
|
SendSignal(active, sender);
|
2025-10-22 17:34:58 +08:00
|
|
|
}
|
|
|
|
|
|
2025-10-27 10:31:01 +08:00
|
|
|
public bool IsEnableSendSignal { get; set; } = true;
|
|
|
|
|
|
|
|
|
|
public void SendSignal(bool active, GameObject sender)
|
2025-10-22 17:34:58 +08:00
|
|
|
{
|
2025-10-28 08:50:06 +08:00
|
|
|
if (_pointA.GetGameObject() != sender)
|
2025-10-22 17:34:58 +08:00
|
|
|
{
|
2025-10-28 08:50:06 +08:00
|
|
|
var pointA = _pointA as ISignalReceiver;
|
|
|
|
|
pointA?.OnSignalReceived(active, this.gameObject);
|
2025-10-22 17:34:58 +08:00
|
|
|
}
|
2025-10-27 10:31:01 +08:00
|
|
|
|
2025-10-28 08:50:06 +08:00
|
|
|
if (_pointB.GetGameObject() != sender)
|
2025-10-22 17:34:58 +08:00
|
|
|
{
|
2025-10-28 08:50:06 +08:00
|
|
|
var pointB = _pointB as ISignalReceiver;
|
|
|
|
|
pointB?.OnSignalReceived(active, this.gameObject);
|
2025-10-22 17:34:58 +08:00
|
|
|
}
|
|
|
|
|
}
|
2025-10-19 19:38:52 +08:00
|
|
|
}
|
|
|
|
|
}
|