using UnityEngine; using AibisDream.FixSystem; using System.Collections.Generic; namespace AibisDream { public class ExpressionCableSystem : MonoBehaviour { [Header("电源子模块")] public ExpressionPowerSource powerSource; private ExpressionManager _expressionManager; private ExpressionSubSystem[] _expressionSubSystems; private const float MAX_DISTANCE = 1f; // 最大检测距离 private void Awake() { // 自动查找所有子物体中的表达子模块 _expressionSubSystems = GetComponentsInChildren(); } private void Start() { _expressionManager = transform.parent.transform.parent.GetComponent(); InitializeSystem(); } private void InitializeSystem() { // 初始化表达子模块 foreach (var subSystem in _expressionSubSystems) { subSystem.Initialize(this); } } // 当插头插入插槽时调用 public void OnPlugInserted(int subSystemIndex, int socketIndex) { _expressionManager.AdjustValue(subSystemIndex, 1); } // 当插头拔出插槽时调用 public void OnPlugRemoved(int subSystemIndex, int socketIndex) { _expressionManager.AdjustValue(subSystemIndex, -1); } public void Reset() { powerSource.Reset(); foreach (var subSystem in _expressionSubSystems) { subSystem.Reset(); } } public bool TryFindClosestSocket(Vector3 sourcePos, out ISocket targetSocket) { targetSocket = null; float minDistance = MAX_DISTANCE; // 遍历所有子模块 foreach (var subSystem in _expressionSubSystems) { // 遍历子模块中的所有插槽 foreach (var socket in subSystem.GetSockets()) { if (!socket.IsAvailable()) continue; float distance = Vector3.Distance(sourcePos, socket.GetSocketPos()); if (distance < minDistance) { minDistance = distance; targetSocket = socket; } } } return targetSocket != null; } } }