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 public class ConnectionLine : MonoBehaviour
{ {
[SerializeField] private MonoBehaviour monoSource; [SerializeField] private MonoBehaviour monoPointA;
[SerializeField] private MonoBehaviour monoTarget; [SerializeField] private MonoBehaviour monoPointB;
private IConnectable _output; private IConnectable _pointA;
private IConnectable _input; private IConnectable _pointB;
private LineRenderer line; private LineRenderer line;
private void Awake() private void Awake()
{ {
_output = monoSource as IConnectable; _pointA = monoPointA as IConnectable;
_input = monoTarget as IConnectable; _pointB = monoPointB as IConnectable;
line = GetComponent<LineRenderer>(); line = GetComponent<LineRenderer>();
if (_output != null && _input != null) SetLineRendererPositions();
{
SetConnectable(_output, _input);
}
} }
public void SetConnectable(IConnectable output, IConnectable input) public void SetConnectable(IConnectable pointA, IConnectable pointB)
{ {
_output = output; if(pointA == null || pointB == null)
_input = input; {
_output.IsConnectedOutput = true; Debug.Log("ConnectionLine requires two valid IConnectable points.");
_input.IsConnectedInput = true; 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();
}
private void SetLineRendererPositions()
{
if (_pointA != null && _pointB != null)
{
line.SetPositions(new Vector3[] line.SetPositions(new Vector3[]
{ {
_output.GetPosition(), _pointA.GetPosition(),
_input.GetPosition() _pointB.GetPosition()
}); });
} }
public void ReceiveSignal(bool active)
{
SendSignal(active);
} }
private void SendSignal(bool active) public void SignalActive(bool active, GameObject sender)
{ {
_input.ReceiveSignal(active,this.gameObject); if (_pointA != null && _pointB != null)
{
if (sender == _pointA.GetGameObject())
{
_pointB.SignalActive(active, sender);
} }
else if (sender == _pointB.GetGameObject())
public void DeleteConnection()
{ {
Destroy(this.gameObject); _pointA.SignalActive(active, sender);
}
}
} }
private void OnDestroy() private void OnDestroy()
{ {
if (_output != null) _pointA.ConnectionLines.Remove(this);
{ _pointB.ConnectionLines.Remove(this);
_output.IsConnectedOutput = false;
}
if (_input != null)
{
_input.IsConnectedInput = false;
}
} }
} }
} }

View File

@@ -23,21 +23,36 @@ namespace Script.Gameplay.Connect
GeneratePreviousConnectionLines(preConnectionLines); GeneratePreviousConnectionLines(preConnectionLines);
} }
public ConnectionLine GenerateConnectionLine(IConnectable source, IConnectable target) public ConnectionLine GenerateConnectionLine(IConnectable a, IConnectable b)
{ {
GameObject lineObject = Instantiate(linePrefab, this.transform); GameObject lineObject = Instantiate(linePrefab, this.transform);
ConnectionLine connectionLine = lineObject.GetComponent<ConnectionLine>(); ConnectionLine connectionLine = lineObject.GetComponent<ConnectionLine>();
if (connectionLine != null) if (connectionLine != null)
{ {
source.OutputConnectionLine = connectionLine; connectionLine.SetConnectable(a, b);
target.InputConnectionLine = connectionLine;
connectionLine.SetConnectable(source, target);
connectionLines.Add(connectionLine); connectionLines.Add(connectionLine);
return connectionLine; return connectionLine;
} }
return null; 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) public void GeneratePreviousConnectionLines(List<PreviousConnection> previousConnections)
{ {
foreach (var preConnection in previousConnections) foreach (var preConnection in previousConnections)

View File

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

View File

@@ -1,11 +1,12 @@
using System.Collections; using System.Collections;
using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using Script.Gameplay.Interface; using Script.Gameplay.Interface;
using Script.Gameplay.Connect; using Script.Gameplay.Connect;
namespace Script.Gameplay.Facility namespace Script.Gameplay.Facility
{ {
public class ButtonInteractController : MonoBehaviour, IInteractable, IEditableComponent, IConnectable public class ButtonInteractController : MonoBehaviour, IInteractable, IEditableComponent, IConnectable, ISignalSender
{ {
#region Interactable #region Interactable
@@ -20,7 +21,7 @@ namespace Script.Gameplay.Facility
public void Interact(GameObject interactor) public void Interact(GameObject interactor)
{ {
if (!Interactable) return; if (!Interactable) return;
StartCoroutine(SendSignalCoroutine(interactor)); StartCoroutine(SendSignalCoroutine());
Debug.Log("Button pressed"); Debug.Log("Button pressed");
} }
@@ -34,14 +35,15 @@ namespace Script.Gameplay.Facility
// 可选:取消高亮 // 可选:取消高亮
} }
private IEnumerator SendSignalCoroutine(GameObject sender) private IEnumerator SendSignalCoroutine()
{ {
SendSignal(true, sender); SendSignal(true);
Interactable = false;
// 按钮压下的动画或效果可以在这里添加 // 按钮压下的动画或效果可以在这里添加
yield return new WaitForSeconds(signalDuration); yield return new WaitForSeconds(signalDuration);
SendSignal(false);
SendSignal(false, sender); Interactable = true;
// 按钮弹起的动画或效果可以在这里添加 // 按钮弹起的动画或效果可以在这里添加
} }
@@ -81,27 +83,30 @@ namespace Script.Gameplay.Facility
return transform.position; return transform.position;
} }
public GameObject GetGameObject()
{
return gameObject;
}
public string GetConnectableName() public string GetConnectableName()
{ {
return gameObject.name; return gameObject.name;
} }
public ConnectionLine OutputConnectionLine { get; set; } public List<ConnectionLine> ConnectionLines { get; set; } = new List<ConnectionLine>();
public ConnectionLine InputConnectionLine { get; set; } public void SignalActive(bool active, GameObject sender)
public bool IsConnectedOutput { get; set; }
public bool IsConnectedInput { get; set; }
public void ReceiveSignal(bool active, GameObject sender)
{ {
// 按钮通常不响应输入信号,可留空或自定义逻辑 // 按钮通常不接收信号,因此这里可以留空
} }
public void SendSignal(bool active, GameObject sender) public void SendSignal(bool active)
{ {
if (OutputConnectionLine != null) if (ConnectionLines != null)
{ {
OutputConnectionLine.ReceiveSignal(active); foreach (var line in ConnectionLines)
{
line.SignalActive(active, this.gameObject);
}
} }
} }

View File

@@ -95,27 +95,22 @@ namespace Script.Gameplay.Facility
return transform.position; return transform.position;
} }
public GameObject GetGameObject()
{
return gameObject;
}
public string GetConnectableName() public string GetConnectableName()
{ {
return gameObject.name; return gameObject.name;
} }
public ConnectionLine OutputConnectionLine { get; set; } public List<ConnectionLine> ConnectionLines { get; set; } = new List<ConnectionLine>();
public ConnectionLine InputConnectionLine { get; set; } public void SignalActive(bool active, GameObject sender)
public bool IsConnectedOutput { get; set; }
public bool IsConnectedInput { get; set; }
public void ReceiveSignal(bool active, GameObject sender)
{ {
Interact(sender); Interact(sender);
} }
public void SendSignal(bool active, GameObject sender)
{
OutputConnectionLine.ReceiveSignal(active);
}
#endregion #endregion
} }
} }

View File

@@ -1,10 +1,11 @@
using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using Script.Gameplay.Interface; using Script.Gameplay.Interface;
using Script.Gameplay.Connect; using Script.Gameplay.Connect;
namespace Script.Gameplay.Facility namespace Script.Gameplay.Facility
{ {
public class LeverInteractController : MonoBehaviour, IInteractable, IEditableComponent, IConnectable public class LeverInteractController : MonoBehaviour, IInteractable, IEditableComponent, IConnectable, ISignalSender
{ {
#region Interactable #region Interactable
@@ -20,7 +21,7 @@ namespace Script.Gameplay.Facility
{ {
if (!Interactable) return; if (!Interactable) return;
isPulled = !isPulled; isPulled = !isPulled;
SendSignal(isPulled, interactor); SendSignal(isPulled);
// 可选:拉杆动画 // 可选:拉杆动画
Debug.Log(isPulled ? "Lever pulled down" : "Lever reset"); Debug.Log(isPulled ? "Lever pulled down" : "Lever reset");
} }
@@ -58,29 +59,39 @@ namespace Script.Gameplay.Facility
#region Connectable #region Connectable
public void OnGazeEnter() { } public void OnGazeEnter()
public void OnGazeExit() { }
public Vector3 GetPosition() => transform.position;
public string GetConnectableName() => gameObject.name;
public ConnectionLine OutputConnectionLine { get; set; }
public ConnectionLine InputConnectionLine { get; set; }
public bool IsConnectedOutput { get; set; }
public bool IsConnectedInput { get; set; }
public void ReceiveSignal(bool active, GameObject sender)
{ {
// 拉杆通常不响应输入信号
} }
public void SendSignal(bool active, GameObject sender) public void OnGazeExit()
{ {
if (OutputConnectionLine != null) }
public Vector3 GetPosition() => transform.position;
public GameObject GetGameObject()
{ {
OutputConnectionLine.ReceiveSignal(active); return gameObject;
}
public string GetConnectableName() => gameObject.name;
public List<ConnectionLine> ConnectionLines { get; set; }= new List<ConnectionLine>();
public void SignalActive(bool active, GameObject sender)
{
//
}
public void SendSignal(bool active)
{
if (ConnectionLines != null)
{
foreach (var line in ConnectionLines)
{
line.SignalActive(active, this.gameObject);
}
} }
} }
#endregion #endregion
} }
} }

View File

@@ -1,10 +1,11 @@
using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using Script.Gameplay.Interface; using Script.Gameplay.Interface;
using Script.Gameplay.Connect; using Script.Gameplay.Connect;
namespace Script.Gameplay.Facility namespace Script.Gameplay.Facility
{ {
public class PressurePlateController : MonoBehaviour, IEditableComponent, IConnectable public class PressurePlateController : MonoBehaviour, IEditableComponent, IConnectable, ISignalSender
{ {
[SerializeField] private bool isActive = true; [SerializeField] private bool isActive = true;
[SerializeField] private LayerMask detectLayer = ~0; // 检测所有层可在Inspector中指定 [SerializeField] private LayerMask detectLayer = ~0; // 检测所有层可在Inspector中指定
@@ -16,10 +17,11 @@ namespace Script.Gameplay.Facility
private void FixedUpdate() private void FixedUpdate()
{ {
if (!isActive) return; if (!isActive) return;
bool hasObject = Physics.CheckBox(transform.position + plateOffset, plateSize * 0.5f, Quaternion.identity, detectLayer); bool hasObject = Physics.CheckBox(transform.position + plateOffset, plateSize * 0.5f, Quaternion.identity,
detectLayer);
if (hasObject != lastState) if (hasObject != lastState)
{ {
SendSignal(hasObject, gameObject); SendSignal(hasObject);
lastState = hasObject; lastState = hasObject;
} }
} }
@@ -39,25 +41,37 @@ namespace Script.Gameplay.Facility
#region Connectable #region Connectable
public void OnGazeEnter() { } public void OnGazeEnter()
public void OnGazeExit() { }
public Vector3 GetPosition() => transform.position;
public string GetConnectableName() => gameObject.name;
public ConnectionLine OutputConnectionLine { get; set; }
public ConnectionLine InputConnectionLine { get; set; }
public bool IsConnectedOutput { get; set; }
public bool IsConnectedInput { get; set; }
public void ReceiveSignal(bool active, GameObject sender)
{ {
// 压力板不响应输入信号
} }
public void SendSignal(bool active, GameObject sender) public void OnGazeExit()
{ {
if (OutputConnectionLine != null) }
public Vector3 GetPosition() => transform.position;
public GameObject GetGameObject()
{ {
OutputConnectionLine.ReceiveSignal(active); return gameObject;
}
public string GetConnectableName() => gameObject.name;
public List<ConnectionLine> ConnectionLines { get; set; } = new List<ConnectionLine>();
public void SignalActive(bool active, GameObject sender)
{
//
}
public void SendSignal(bool active)
{
if (ConnectionLines != null)
{
foreach (var line in ConnectionLines)
{
line.SignalActive(active, this.gameObject);
}
} }
} }
@@ -72,4 +86,3 @@ namespace Script.Gameplay.Facility
#endif #endif
} }
} }

View File

@@ -19,14 +19,14 @@ namespace Script.Gameplay.Player
private IConnectable outTarget; private IConnectable outTarget;
private IConnectable inputTarget; private IConnectable inputTarget;
public event Action<IConnectable> OnSetOutputTarget; public event Action<IConnectable> OnSetPointA;
public event Action<IConnectable> OnSetInputTarget; public event Action<IConnectable> OnSetPointB;
void Start() void Start()
{ {
inputManager = InputManager.Instance; inputManager = InputManager.Instance;
inputManager.Input.Player.SetOutput.performed += ctx => SetOutTarget(currentTarget); inputManager.Input.Player.SetOutput.performed += ctx => SetPointA(currentTarget);
inputManager.Input.Player.SetInput.performed += ctx => SetInputTarget(currentTarget); inputManager.Input.Player.SetInput.performed += ctx => SetPointB(currentTarget);
inputManager.Input.Player.Connect.performed += ctx => CreateConnection(); inputManager.Input.Player.Connect.performed += ctx => CreateConnection();
inputManager.Input.Player.CutLine.performed += ctx => CutConnectLine(currentTarget); inputManager.Input.Player.CutLine.performed += ctx => CutConnectLine(currentTarget);
@@ -77,34 +77,27 @@ namespace Script.Gameplay.Player
return currentTarget; return currentTarget;
} }
public void SetOutTarget(IConnectable target) public void SetPointA(IConnectable target)
{ {
if (target == null) return; if (target == null) return;
if(!IsEnableConnecting) return; if(!IsEnableConnecting) return;
if (!target.IsConnectedOutput)
{
outTarget = target; outTarget = target;
OnSetOutputTarget?.Invoke(outTarget); OnSetPointA?.Invoke(outTarget);
}
} }
public void SetInputTarget(IConnectable target) public void SetPointB(IConnectable target)
{ {
if (target == null) return; if (target == null) return;
if(!IsEnableConnecting) return; if(!IsEnableConnecting) return;
if (!target.IsConnectedInput) inputTarget = target;
{ OnSetPointB?.Invoke(inputTarget);
inputTarget = currentTarget;
OnSetInputTarget?.Invoke(inputTarget);
}
} }
public void CutConnectLine(IConnectable target) public void CutConnectLine(IConnectable target)
{ {
if (target == null) return; if (target == null) return;
if(target.OutputConnectionLine != null && target.InputConnectionLine != null) return; if(!IsEnableConnecting) return;
target.OutputConnectionLine?.DeleteConnection(); ConnectionLineManager.Instance.CutTargetConnectionLines(target);
target.InputConnectionLine?.DeleteConnection();
} }
private void CreateConnection() private void CreateConnection()

View File

@@ -23,8 +23,8 @@ namespace UI
private void OnGetConnectController(PlayerConnectController controller) private void OnGetConnectController(PlayerConnectController controller)
{ {
_playerConnectController = controller; _playerConnectController = controller;
_playerConnectController.OnSetInputTarget += UpdateInputText; _playerConnectController.OnSetPointB += UpdateInputText;
_playerConnectController.OnSetOutputTarget += UpdateOutputText; _playerConnectController.OnSetPointA += UpdateOutputText;
UpdateInputText(null); UpdateInputText(null);
UpdateOutputText(null); UpdateOutputText(null);
} }