105 lines
3.1 KiB
C#
105 lines
3.1 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using AibisDream.Framework;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
|
|
namespace AibisDream.FixSystem
|
|
{
|
|
public class BodyModuleSystem : MonoBehaviour
|
|
{
|
|
#region 内部索引
|
|
|
|
public BodyModule[] BodyModules { get; private set; }
|
|
public CableSystem CableSystem { get; private set; }
|
|
|
|
#endregion
|
|
|
|
#region 对外暴露事件
|
|
|
|
public UnityEvent<Socket> PlugInEvent => CableSystem.plugInEvent;
|
|
public UnityEvent<Socket> 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<BodyModule>();
|
|
CableSystem = transform.Find("Cable System").GetComponent<CableSystem>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 寻找最接近的Module
|
|
/// </summary>
|
|
/// <param name="sourcePos">源位置</param>
|
|
/// <param name="targetModule">最近的Module</param>
|
|
/// <returns>是否能找到目标范围内的Module</returns>
|
|
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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 插线系统是否插在插槽里
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public bool IsPlugInSocket()
|
|
{
|
|
return CableSystem.curSocket != null;
|
|
}
|
|
|
|
[Serializable]
|
|
public struct BodyModuleSystemConfig
|
|
{
|
|
public float plugSnappingRange;
|
|
|
|
public static BodyModuleSystemConfig GeneDefaultConfig()
|
|
{
|
|
return new BodyModuleSystemConfig
|
|
{
|
|
plugSnappingRange = 1f
|
|
};
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|