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

@@ -1,11 +1,12 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Script.Gameplay.Interface;
using Script.Gameplay.Connect;
namespace Script.Gameplay.Facility
{
public class ButtonInteractController : MonoBehaviour, IInteractable, IEditableComponent, IConnectable
public class ButtonInteractController : MonoBehaviour, IInteractable, IEditableComponent, IConnectable, ISignalSender
{
#region Interactable
@@ -20,7 +21,7 @@ namespace Script.Gameplay.Facility
public void Interact(GameObject interactor)
{
if (!Interactable) return;
StartCoroutine(SendSignalCoroutine(interactor));
StartCoroutine(SendSignalCoroutine());
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);
SendSignal(false, sender);
SendSignal(false);
Interactable = true;
// 按钮弹起的动画或效果可以在这里添加
}
@@ -81,27 +83,30 @@ namespace Script.Gameplay.Facility
return transform.position;
}
public GameObject GetGameObject()
{
return gameObject;
}
public string GetConnectableName()
{
return 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 List<ConnectionLine> ConnectionLines { get; set; } = new List<ConnectionLine>();
public void SignalActive(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;
}
public GameObject GetGameObject()
{
return gameObject;
}
public string GetConnectableName()
{
return 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 List<ConnectionLine> ConnectionLines { get; set; } = new List<ConnectionLine>();
public void SignalActive(bool active, GameObject sender)
{
Interact(sender);
}
public void SendSignal(bool active, GameObject sender)
{
OutputConnectionLine.ReceiveSignal(active);
}
#endregion
}
}

View File

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

View File

@@ -1,10 +1,11 @@
using System.Collections.Generic;
using UnityEngine;
using Script.Gameplay.Interface;
using Script.Gameplay.Connect;
namespace Script.Gameplay.Facility
{
public class PressurePlateController : MonoBehaviour, IEditableComponent, IConnectable
public class PressurePlateController : MonoBehaviour, IEditableComponent, IConnectable, ISignalSender
{
[SerializeField] private bool isActive = true;
[SerializeField] private LayerMask detectLayer = ~0; // 检测所有层可在Inspector中指定
@@ -16,10 +17,11 @@ namespace Script.Gameplay.Facility
private void FixedUpdate()
{
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)
{
SendSignal(hasObject, gameObject);
SendSignal(hasObject);
lastState = hasObject;
}
}
@@ -39,25 +41,37 @@ namespace Script.Gameplay.Facility
#region Connectable
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 OnGazeEnter()
{
// 压力板不响应输入信号
}
public void SendSignal(bool active, GameObject sender)
public void OnGazeExit()
{
if (OutputConnectionLine != null)
}
public Vector3 GetPosition() => transform.position;
public GameObject GetGameObject()
{
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)
{
OutputConnectionLine.ReceiveSignal(active);
foreach (var line in ConnectionLines)
{
line.SignalActive(active, this.gameObject);
}
}
}
@@ -71,5 +85,4 @@ namespace Script.Gameplay.Facility
}
#endif
}
}
}