/// <summary>
        /// 空间查找可以可以连接的点
        /// </summary>
        /// <param name="item"></param>
        /// <param name="node"></param>
        /// <returns></returns>
        public static bool FindInstallableNode(LinkPort item, out LinkPort node)
        {
            if (item.ConnectedNode != null)
            {
                node = item.ConnectedNode;
                return(false);
            }

            Collider[] colliders = Physics.OverlapSphere(item.Pos, item.Range, LayerMask.GetMask(Layers.linknodeLayer));
            if (colliders != null && colliders.Length > 0)
            {
                foreach (var collider in colliders)
                {
                    LinkPort tempNode = collider.GetComponentInParent <LinkPort>();
                    if (tempNode == null || tempNode == item || tempNode.Body == item.Body || tempNode.ConnectedNode != null)
                    {
                        continue;
                    }
                    //主被动动连接点,非自身点,相同名,没有建立连接
                    var linkInfo = item.connectAble.Find((x) => x.itemName == tempNode.Body.Name && x.nodeId == tempNode.NodeID);
                    if (linkInfo != null)
                    {
                        //同属于一个源
                        node = tempNode;
                        return(true);
                    }
                }
            }
            node = null;
            return(false);
        }
        public void SetActiveItem(LinkItem item, bool detach)
        {
            this.pickedUpItem = item;

            ///如果目标已经被使用,阻止和其他元素断开
            if (item.Used)
            {
                List <LinkPort> disconnected  = new List <LinkPort>();
                LinkPort[]      connectedPort = item.GetLinkedPorts();

                if (detach)
                {
                    for (int i = 0; i < connectedPort.Length; i++)
                    {
                        LinkPort port      = connectedPort[i];
                        LinkPort otherPort = port.ConnectedNode;

                        LinkUtil.DetachNodes(port, otherPort);
                        disconnected.Add(port);
                        disconnected.Add(otherPort);
                    }

                    if (onDisconnected != null)
                    {
                        onDisconnected.Invoke(disconnected.ToArray());
                    }
                }
            }
        }
 /// <summary>
 /// 空间查找触发的点
 /// </summary>
 /// <param name="item"></param>
 /// <param name="nodes"></param>
 /// <returns></returns>
 public static bool FindTriggerNodes(LinkPort item, out List <LinkPort> nodes)
 {
     nodes = null;
     Collider[] colliders = Physics.OverlapSphere(item.Pos, item.Range, LayerMask.GetMask(Layers.linknodeLayer));
     if (colliders != null && colliders.Length > 0)
     {
         foreach (var collider in colliders)
         {
             LinkPort tempNode = collider.GetComponentInParent <LinkPort>();
             if (tempNode == null || tempNode == item || tempNode.Body == item.Body)
             {
                 continue;
             }
             else
             {
                 if (nodes == null)
                 {
                     nodes = new List <Actions.LinkPort>();
                 }
                 nodes.Add(tempNode);
             }
         }
     }
     return(nodes != null && nodes.Count > 0);
 }
        public void Update()
        {
            timer += Time.deltaTime;
            if (pickedUpItem != null && timer > spanTime)
            {
                timer = 0f;

                if (FindConnectableObject())
                {
                    if (onMatch != null)
                    {
                        onMatch(activeNode, targetNode);
                    }
                }
                else
                {
                    if (onDisMatch != null && activeNode != null && targetNode != null)
                    {
                        onDisMatch.Invoke(targetNode, activeNode);
                    }

                    activeNode = null;
                    targetNode = null;
                }
            }
        }
        private IEnumerator MoveBToA(LinkPort portA, LinkPort portB)
        {
            //var linkInfoA = portA.connectAble.Find(x => x.itemName == portB.Body.Name);
            var linkInfoB = portB.connectAble.Find(x => x.itemName == portA.Body.Name);

            Vector3 pos;
            Vector3 eular;

            LinkUtil.GetWorldPosFromTarget(portA.Body, linkInfoB.relativePos, linkInfoB.relativeDir, out pos, out eular);

            var startPos     = portB.Body.transform.position;
            var startforward = portB.Body.transform.forward;

            if (quickLink || autoLinkTime < 0.1f)
            {
                yield return(new WaitForSeconds(autoLinkTime));

                portB.Body.transform.eulerAngles = eular;
                portB.Body.transform.position    = pos;
                LinkUtil.UpdateBrotherPos(portB.Body, new List <LinkItem>());
            }
            else
            {
                var context = new List <LinkItem>();
                for (float j = 0; j < autoLinkTime; j += Time.deltaTime)
                {
                    yield return(null);

                    portB.Body.transform.position    = Vector3.Lerp(startPos, pos, j / autoLinkTime);
                    portB.Body.transform.eulerAngles = Vector3.Lerp(startforward, eular, j / autoLinkTime);
                    context.Clear();
                    LinkUtil.UpdateBrotherPos(portB.Body, context);
                }
            }
        }
 public bool Attach(LinkPort item)
 {
     item.ConnectedNode = this;
     ConnectedNode      = item;
     item.ResetTransform();
     item.Body.transform.SetParent(Body.transform);
     return(true);
 }
 void OnDisMath(LinkPort itemA, LinkPort itemB)
 {
     highter.UnHighLightTarget(itemA.gameObject);
     highter.UnHighLightTarget(itemB.gameObject);
     if (linkItem)
     {
         linkItem.isMatching = false;
     }
 }
        public LinkPort Detach(Transform parent)
        {
            LinkPort outItem = ConnectedNode;

            if (ConnectedNode != null)
            {
                ConnectedNode.ConnectedNode = null;
                ConnectedNode = null;
            }
            outItem.transform.SetParent(parent);
            return(outItem);
        }
        void OnMatch(LinkPort itemA, LinkPort itemB)
        {
            highter.HighLightTarget(itemA.gameObject, Color.green);
            var linkInfo = itemA.connectAble.Find(x => x.itemName == itemB.Body.Name && x.nodeId == itemB.NodeID);

            LinkUtil.ResetTargetTranform(itemA.Body, itemB.Body, linkInfo.relativePos, linkInfo.relativeDir);
            LinkUtil.UpdateBrotherPos(itemA.Body, new List <LinkItem>());
            if (linkItem)
            {
                linkItem.isMatching = true;
            }
        }
        public void TryConnect()
        {
            if (activeNode != null && targetNode != null)
            {
                LinkUtil.AttachNodes(activeNode, targetNode);

                if (onConnected != null)
                {
                    onConnected.Invoke(new LinkPort[] { activeNode, targetNode });
                }

                activeNode = null;
                targetNode = null;
            }
        }
        public bool FindConnectableObject()
        {
            if (pickedUpItem != null)
            {
                LinkPort tempNode;
                foreach (var item in pickedUpItem.GroupNodes)
                {
                    if (LinkUtil.FindInstallableNode(item, out tempNode))
                    {
                        activeNode = item;
                        targetNode = tempNode;
                        return(true);
                    }
                }
            }

            return(false);
        }
        public static void TryRecordConnect(LinkPort node_A, LinkPort node_B)
        {
            if (!node_A || !node_B)
            {
                return;
            }
            LinkItem item_A = node_A.GetComponentInParent <LinkItem>();
            LinkItem item_B = node_B.GetComponentInParent <LinkItem>();

            if (node_A == null || node_B == null || item_A == null || item_B == null)
            {
                return;
            }

            var confer = EditorUtility.DisplayDialog("[connected]", item_A.Name + ":" + (node_A.name) + "<->" + item_B.Name + ":" + (node_B.name), "确认");

            if (confer)
            {
                LinkInfo nodeArecored = node_A.connectAble.Find((x) => x.itemName == item_B.name && x.nodeId == node_B.NodeID);
                LinkInfo nodeBrecored = node_B.connectAble.Find((x) => x.itemName == item_A.name && x.nodeId == node_A.NodeID);
                //已经记录过
                if (nodeArecored == null)
                {
                    nodeArecored = new LinkInfo();
                    node_A.connectAble.Add(nodeArecored);
                }
                if (nodeBrecored == null)
                {
                    nodeBrecored = new LinkInfo();
                    node_B.connectAble.Add(nodeBrecored);
                }

                nodeArecored.itemName = item_B.Name;
                nodeBrecored.itemName = item_A.Name;
                nodeArecored.nodeId   = node_B.NodeID;
                nodeBrecored.nodeId   = node_A.NodeID;
                LinkUtil.RecordTransform(nodeArecored, nodeBrecored, item_A.transform, item_B.transform);
                EditorUtility.SetDirty(node_A);
                EditorUtility.SetDirty(node_B);
            }
        }
 public static void DetachNodes(LinkPort moveAblePort, LinkPort staticPort)
 {
     moveAblePort.ConnectedNode = null;
     staticPort.ConnectedNode   = null;
 }
 public static void AttachNodes(LinkPort moveAblePort, LinkPort staticPort)
 {
     moveAblePort.ConnectedNode = staticPort;
     staticPort.ConnectedNode   = moveAblePort;
     moveAblePort.ResetTransform();
 }
 public void SetDisableItem()
 {
     pickedUpItem = null;
     targetNode   = null;
     activeNode   = null;
 }
 public void InitPortGroup(LinkPort port, LinkPort[] otherPorts)
 {
     this.port       = port;
     this.otherPorts = otherPorts;
     selection       = new bool[otherPorts.Length];
 }