using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; namespace AibisDream.FixSystem { public class BodyModuleSystem : MonoBehaviour { public BodyModule[] BodyModules { get; private set; } public CableSystem CableSystem { get; private set; } #region 对外暴露事件 public UnityEvent PlugInEvent => CableSystem.plugInEvent; public UnityEvent PlugOutEvent => CableSystem.plugOutEvent; public UnityEvent PlugStartDrag => CableSystem.plugStartDrag; public UnityEvent PlugEndDrag => CableSystem.plugEndDrag; #endregion [SerializeField] public BodyModuleSystemConfig config = BodyModuleSystemConfig.GeneDefaultConfig(); private void Awake() { InitComponentRefs(); } private void InitComponentRefs() { BodyModules = transform.GetComponentsInChildren(); CableSystem = transform.Find("Cable System").GetComponent(); } /// /// 寻找最接近的Module /// /// 源位置 /// 最近的Module /// 是否能找到目标范围内的Module public bool TryFindClosestSocket(Vector3 sourcePos, out BodyModule targetModule) { var closestDistance = Mathf.Infinity; BodyModule closestModule = null; // 寻找最接近的BodyModule foreach (var bodyModule in BodyModules) { float tempDistance = Vector3.Distance(sourcePos, bodyModule.SocketPos); if (tempDistance < closestDistance) { closestDistance = tempDistance; closestModule = bodyModule; } } // 判断距离是否在目标范围内,在的话就返回,不在的话返回个空的 if (closestDistance < config.plugSnappingRange && closestModule) { targetModule = closestModule; return true; } else { targetModule = null; return false; } } /// /// 插线系统是否插在插槽里 /// /// public bool IsPlugInSocket() { return CableSystem.curSocket != null; } [Serializable] public struct BodyModuleSystemConfig { public float plugSnappingRange; public static BodyModuleSystemConfig GeneDefaultConfig() { return new BodyModuleSystemConfig { plugSnappingRange = 1f }; } } } }