石头功能
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
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<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
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user