refactor(Connect): 重构连接体系,实现一个物体拥有多个连接线
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user