public void FindNearQuadNodes(PFQuadCircle quadNode, List <PFQuadCircle> tempList) { PFQuadTree quadTree = root; __FindNearQuadNodes(root, quadNode, tempList); }
/// <summary> /// 添加节点 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="quadNode">节点</param> public PFQuadNodeState AddQuadNode(PFQuadCircle quadNode) { PFQuadNodeState state = quadNode.CheckState(this); int index = 0; switch (state) { //在自己范围内,如果在子节点内,则由子节点持有。如果与子节点相交或者不存在子节点,则自己持有 case PFQuadNodeState.In: if (children != null) { index = quadNode.point.x <= (rect.x + rect.x1) / 2 ? 0 : 1; index = quadNode.point.y <= (rect.y + rect.y1) / 2 ? index : index + 2; switch (children[index].AddQuadNode(quadNode)) { case (PFQuadNodeState.In): return(PFQuadNodeState.In); case (PFQuadNodeState.Intersect): break; case (PFQuadNodeState.Out): Debug.Fail("AddQuadNode 异常:子节点状态OUT"); return(PFQuadNodeState.Out); } } quadLinkedList.AddLast(quadNode); quadNode.parentLinkNode = quadLinkedList.Last; quadNode.parent = this; break; //相交则不保存,由父节点继续持有 case PFQuadNodeState.Intersect: if (parent == null) { UnityEngine.Debug.LogError("AddQuadNode 异常:节点不在根数节点内"); } break; case PFQuadNodeState.Out: UnityEngine.Debug.LogError("AddQuadNode 异常:状态OUT"); break; } //如果数量超过容纳范围,则将当前格子分裂4块 if ((children == null) && (quadLinkedList.Count > PATH_FINDER_CONTAIN_NUMBER)) { children = new PFQuadTree[4]; for (int i = 0; i < 4; i++) { int x = rect.x + (rect.x1 - rect.x) / 2 * (i % 2); int y = rect.y + (rect.y1 - rect.y) / 2 * (i / 2); int x1 = x + (rect.x1 - rect.x) / 2; int y1 = y + (rect.y1 - rect.y) / 2; children[i] = MctCacheManager.GetInstantiateFromCache <PFQuadTree>(); if (children[i] != null) { children[i].InitRuntime(root, this, new PFRect(x, y, x1, y1), depath + 1); } else { children[i] = new PFQuadTree(root, this, new PFRect(x, y, x1, y1), depath + 1); } } //将父节点的节点转到子节点 LinkedListNode <PFQuadCircle> node; LinkedListNode <PFQuadCircle> nodeNext; node = quadLinkedList.First; while (node != null) { nodeNext = node.Next; PFQuadCircle tempQuadNode = node.Value; index = tempQuadNode.point.x <= (rect.x + rect.x1) / 2 ? 0 : 1; index = tempQuadNode.point.y <= (rect.y + rect.y1) / 2 ? index : index + 2; switch (children[index].AddQuadNode(tempQuadNode)) { case (PFQuadNodeState.In): quadLinkedList.Remove(node); break; case (PFQuadNodeState.Intersect): break; case (PFQuadNodeState.Out): Debug.Fail("AddQuadNode 异常:分配子节点状态OUT"); break; } node = nodeNext; } } return(state); }
/// <summary> /// 移除节点 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="quadNode">节点</param> public void RemoveQuadNode(PFQuadCircle quadNode) { quadNode.parent.quadLinkedList.Remove(quadNode.parentLinkNode); quadNode.parentLinkNode = null; quadNode.parent = null; }