refactor(Connect): 重构连接体系,实现一个物体拥有多个连接线

This commit is contained in:
2025-10-20 19:40:55 +08:00
parent bb2c5aa436
commit 0a1aa83425
11 changed files with 187 additions and 140 deletions

View File

@@ -6,66 +6,73 @@ namespace Script.Gameplay.Connect
// 只负责在场景中绘制连接线,并处理信号传递
public class ConnectionLine : MonoBehaviour
{
[SerializeField] private MonoBehaviour monoSource;
[SerializeField] private MonoBehaviour monoTarget;
private IConnectable _output;
private IConnectable _input;
[SerializeField] private MonoBehaviour monoPointA;
[SerializeField] private MonoBehaviour monoPointB;
private IConnectable _pointA;
private IConnectable _pointB;
private LineRenderer line;
private void Awake()
{
_output = monoSource as IConnectable;
_input = monoTarget as IConnectable;
_pointA = monoPointA as IConnectable;
_pointB = monoPointB as IConnectable;
line = GetComponent<LineRenderer>();
if (_output != null && _input != null)
SetLineRendererPositions();
}
public void SetConnectable(IConnectable pointA, IConnectable pointB)
{
if(pointA == null || pointB == null)
{
SetConnectable(_output, _input);
Debug.Log("ConnectionLine requires two valid IConnectable points.");
return;
}
}
public void SetConnectable(IConnectable output, IConnectable input)
{
_output = output;
_input = input;
_output.IsConnectedOutput = true;
_input.IsConnectedInput = true;
line.SetPositions(new Vector3[]
if (pointA == pointB)
{
_output.GetPosition(),
_input.GetPosition()
});
}
public void ReceiveSignal(bool active)
{
SendSignal(active);
}
private void SendSignal(bool active)
{
_input.ReceiveSignal(active,this.gameObject);
Debug.Log("ConnectionLine cannot connect the same point.");
return;
}
_pointA = pointA;
_pointB = pointB;
pointA.ConnectionLines.Add(this);
pointB.ConnectionLines.Add(this);
SetLineRendererPositions();
}
public void DeleteConnection()
private void SetLineRendererPositions()
{
Destroy(this.gameObject);
if (_pointA != null && _pointB != null)
{
line.SetPositions(new Vector3[]
{
_pointA.GetPosition(),
_pointB.GetPosition()
});
}
}
public void SignalActive(bool active, GameObject sender)
{
if (_pointA != null && _pointB != null)
{
if (sender == _pointA.GetGameObject())
{
_pointB.SignalActive(active, sender);
}
else if (sender == _pointB.GetGameObject())
{
_pointA.SignalActive(active, sender);
}
}
}
private void OnDestroy()
{
if (_output != null)
{
_output.IsConnectedOutput = false;
}
if (_input != null)
{
_input.IsConnectedInput = false;
}
_pointA.ConnectionLines.Remove(this);
_pointB.ConnectionLines.Remove(this);
}
}
}

View File

@@ -23,20 +23,35 @@ namespace Script.Gameplay.Connect
GeneratePreviousConnectionLines(preConnectionLines);
}
public ConnectionLine GenerateConnectionLine(IConnectable source, IConnectable target)
public ConnectionLine GenerateConnectionLine(IConnectable a, IConnectable b)
{
GameObject lineObject = Instantiate(linePrefab, this.transform);
ConnectionLine connectionLine = lineObject.GetComponent<ConnectionLine>();
if (connectionLine != null)
{
source.OutputConnectionLine = connectionLine;
target.InputConnectionLine = connectionLine;
connectionLine.SetConnectable(source, target);
connectionLine.SetConnectable(a, b);
connectionLines.Add(connectionLine);
return connectionLine;
}
return null;
}
public void CutTargetConnectionLines(IConnectable target)
{
List<ConnectionLine> linesToRemove = new List<ConnectionLine>();
foreach (var line in connectionLines)
{
if (line != null && (line.Equals(target)))
{
linesToRemove.Add(line);
}
}
foreach (var line in linesToRemove)
{
connectionLines.Remove(line);
Destroy(line.gameObject);
}
}
public void GeneratePreviousConnectionLines(List<PreviousConnection> previousConnections)
{

View File

@@ -8,12 +8,9 @@ namespace Script.Gameplay.Connect
void OnGazeEnter(); // 玩家开始注视时触发
void OnGazeExit(); // 玩家停止注视时触发
Vector3 GetPosition(); // 获取连接点位置
GameObject GetGameObject(); // 获取连接点物体
string GetConnectableName(); // 获取连接点名称
public ConnectionLine OutputConnectionLine { get; set; }
public ConnectionLine InputConnectionLine { get; set; }
bool IsConnectedOutput { get; set; } // 是否作为输出端连接
bool IsConnectedInput { get; set; } // 是否作为输入端连接
void ReceiveSignal(bool active, GameObject sender); // 接收信号
void SendSignal(bool active, GameObject sender); // 发出信号
public List<ConnectionLine> ConnectionLines { get; set; }
void SignalActive(bool active, GameObject sender); // 被激活
}
}

View File

@@ -0,0 +1,8 @@
using UnityEngine;
namespace Script.Gameplay.Connect
{
public interface ISignalSender
{
public void SendSignal(bool active);
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: dc3af72006f342c3b8b7526a5b09e6ee
timeCreated: 1760958923