public static AStarPathNode ConvertToAStarPathNode(this TilePosition tile) { AStarPathNode result = new AStarPathNode(); result.Column = tile.Column; result.Row = tile.Row; return result; }
private static void DealNeighbourPathNode(AStarPathNode currentPathNode, AStarPathNode neighbourPathNode, AStarPathNode endPathNode, List <AStarPathNode> openList, List <AStarPathNode> closeList, Dictionary <int, AStarPathNode> openDictionary, Dictionary <int, AStarPathNode> closeDictionary) { if (neighbourPathNode.IsValidNode() && !IsInList(neighbourPathNode, closeDictionary)) { if (!IsInList(neighbourPathNode, openDictionary)) { neighbourPathNode.ParentPathNode = currentPathNode; neighbourPathNode.ValueG = neighbourPathNode.CalculateValueG(currentPathNode); neighbourPathNode.ValueH = neighbourPathNode.CalculateValueH(endPathNode); s_OpenList.Add(neighbourPathNode); s_OpenDictionary.Add(neighbourPathNode.Column + (neighbourPathNode.Row << 16), neighbourPathNode); } else { neighbourPathNode = openDictionary[neighbourPathNode.Column + (neighbourPathNode.Row << 16)]; int valueG = neighbourPathNode.CalculateValueG(currentPathNode); if (valueG < neighbourPathNode.ValueG) { neighbourPathNode.ParentPathNode = currentPathNode; neighbourPathNode.ValueG = valueG; neighbourPathNode.ValueH = neighbourPathNode.CalculateValueH(endPathNode); } } } }
private static void DealNeighbourPathNode(AStarPathNode currentPathNode, AStarPathNode neighbourPathNode, AStarPathNode endPathNode, SortedDictionary <int, List <AStarPathNode> > openList, List <AStarPathNode> closeList, Dictionary <int, AStarPathNode> openDictionary, Dictionary <int, AStarPathNode> closeDictionary) { if (neighbourPathNode.IsValidNode() && !IsInList(neighbourPathNode, closeDictionary)) { if (!IsInList(neighbourPathNode, openDictionary)) { neighbourPathNode.ParentPathNode = currentPathNode; neighbourPathNode.ValueG = neighbourPathNode.CalculateValueG(currentPathNode); neighbourPathNode.ValueH = neighbourPathNode.CalculateValueH(endPathNode); if (openList.ContainsKey(neighbourPathNode.ValueF)) { openList[neighbourPathNode.ValueF].Add(neighbourPathNode); } else { openList.Add(neighbourPathNode.ValueF, new List <AStarPathNode>() { neighbourPathNode }); } openDictionary.Add(neighbourPathNode.Column + (neighbourPathNode.Row << 16), neighbourPathNode); } else { neighbourPathNode = openDictionary[neighbourPathNode.Column + (neighbourPathNode.Row << 16)]; openList[neighbourPathNode.ValueF].Remove(neighbourPathNode); if (openList[neighbourPathNode.ValueF].Count == 0) { openList.Remove(neighbourPathNode.ValueF); } int valueG = neighbourPathNode.CalculateValueG(currentPathNode); if (valueG < neighbourPathNode.ValueG) { neighbourPathNode.ParentPathNode = currentPathNode; neighbourPathNode.ValueG = valueG; neighbourPathNode.ValueH = neighbourPathNode.CalculateValueH(endPathNode); } if (openList.ContainsKey(neighbourPathNode.ValueF)) { openList[neighbourPathNode.ValueF].Add(neighbourPathNode); } else { openList.Add(neighbourPathNode.ValueF, new List <AStarPathNode>() { neighbourPathNode }); } } } }
private static AStarPathNode GetNewNodeFromPool() { AStarPathNode result = null; if (s_CurrentIndex == s_NodePool.Count) { result = new AStarPathNode(); s_NodePool.Add(result); } else { result = s_NodePool[s_CurrentIndex]; } s_CurrentIndex++; return(result); }
private void CreateGrid() { if (grid != null) { return; } grid = new AStarPathNode[sizeX, sizeY]; for (int x = 0; x < sizeX; x++) { for (int y = 0; y < sizeY; y++) { grid[x, y] = new AStarPathNode() { IsWall = false, X = (position.x * TileSize.x) + x, Y = (position.y * TileSize.y) + y, }; } } }
private IEnumerator MoveToNode(AStarPathNode node) { if (node.X != (int)transform.position.x || node.Y != (int)transform.position.y) { float t = 0.0f; while (true) { t = Mathf.Clamp01(t + Time.deltaTime); Vector3 pos = new Vector3((float)node.X + 0.5f, transform.position.y, (float)node.Y + 0.5f); transform.position = Vector3.Lerp(transform.position, pos, t); yield return(new WaitForEndOfFrame()); float diffX = Mathf.Abs((float)node.X + 0.5f - transform.position.x); float diffY = Mathf.Abs((float)node.Y + 0.5f - transform.position.z); if (diffX < 0.001f && diffY < 0.001f) { transform.position = new Vector3((float)node.X + 0.5f, transform.position.y, (float)node.Y + 0.5f); break; } } } }
public static TilePosition ConvertToTilePosition(this AStarPathNode node) { return new TilePosition(node.Column, node.Row); }
private static bool IsInList(AStarPathNode neighbourPathNode, Dictionary <int, AStarPathNode> dictionary) { return(dictionary.ContainsKey(neighbourPathNode.Column + (neighbourPathNode.Row << 16))); }
private static List <AStarPathNode> CalculatePath(IGCalculator calculator, AStarPathNode startPathNode, AStarPathNode endPathNode) { bool existPath = false; /* * Debug.Log("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"); * Debug.Log(startPathNode.Row + "," + startPathNode.Column); */ startPathNode.GCalculator = calculator; s_OpenList.Add(startPathNode.ValueF, new List <AStarPathNode>() { startPathNode }); s_OpenDictionary.Add(startPathNode.Column + (startPathNode.Row << 16), startPathNode); RecycleAllNode(); while (s_OpenList.Count != 0) { SortedDictionary <int, List <AStarPathNode> > .Enumerator enumerator = s_OpenList.GetEnumerator(); enumerator.MoveNext(); AStarPathNode currentPathNode = enumerator.Current.Value[0]; enumerator.Current.Value.RemoveAt(0); if (enumerator.Current.Value.Count == 0) { s_OpenList.Remove(enumerator.Current.Key); } s_OpenDictionary.Remove(currentPathNode.Column + (currentPathNode.Row << 16)); if (endPathNode.Column == currentPathNode.Column && endPathNode.Row == currentPathNode.Row) { existPath = true; endPathNode = currentPathNode; break; } s_CloseList.Add(currentPathNode); s_CloseDictionary.Add(currentPathNode.Column + (currentPathNode.Row << 16), currentPathNode); //leftbottom AStarPathNode leftBottomPathNode = GetNewNodeFromPool(); //new AStarPathNode(); leftBottomPathNode.Row = currentPathNode.Row + 1; leftBottomPathNode.Column = currentPathNode.Column - 1; leftBottomPathNode.GCalculator = calculator; DealNeighbourPathNode(currentPathNode, leftBottomPathNode, endPathNode, s_OpenList, s_CloseList, s_OpenDictionary, s_CloseDictionary); //lefttop AStarPathNode leftTopPathNode = GetNewNodeFromPool();//new AStarPathNode(); leftTopPathNode.Row = currentPathNode.Row - 1; leftTopPathNode.Column = currentPathNode.Column - 1; leftTopPathNode.GCalculator = calculator; DealNeighbourPathNode(currentPathNode, leftTopPathNode, endPathNode, s_OpenList, s_CloseList, s_OpenDictionary, s_CloseDictionary); //rightbottom AStarPathNode rightBottomPathNode = GetNewNodeFromPool();//new AStarPathNode(); rightBottomPathNode.Row = currentPathNode.Row + 1; rightBottomPathNode.Column = currentPathNode.Column + 1; rightBottomPathNode.GCalculator = calculator; DealNeighbourPathNode(currentPathNode, rightBottomPathNode, endPathNode, s_OpenList, s_CloseList, s_OpenDictionary, s_CloseDictionary); //righttop AStarPathNode rightTopPathNode = GetNewNodeFromPool();//new AStarPathNode(); rightTopPathNode.Row = currentPathNode.Row - 1; rightTopPathNode.Column = currentPathNode.Column + 1; rightTopPathNode.GCalculator = calculator; DealNeighbourPathNode(currentPathNode, rightTopPathNode, endPathNode, s_OpenList, s_CloseList, s_OpenDictionary, s_CloseDictionary); //bottom AStarPathNode bottomPathNode = GetNewNodeFromPool();//new AStarPathNode(); bottomPathNode.Row = currentPathNode.Row + 1; bottomPathNode.Column = currentPathNode.Column; bottomPathNode.GCalculator = calculator; DealNeighbourPathNode(currentPathNode, bottomPathNode, endPathNode, s_OpenList, s_CloseList, s_OpenDictionary, s_CloseDictionary); //left AStarPathNode leftPathNode = GetNewNodeFromPool();//new AStarPathNode(); leftPathNode.Row = currentPathNode.Row; leftPathNode.Column = currentPathNode.Column - 1; leftPathNode.GCalculator = calculator; DealNeighbourPathNode(currentPathNode, leftPathNode, endPathNode, s_OpenList, s_CloseList, s_OpenDictionary, s_CloseDictionary); //right AStarPathNode rightPathNode = GetNewNodeFromPool();//new AStarPathNode(); rightPathNode.Row = currentPathNode.Row; rightPathNode.Column = currentPathNode.Column + 1; rightPathNode.GCalculator = calculator; DealNeighbourPathNode(currentPathNode, rightPathNode, endPathNode, s_OpenList, s_CloseList, s_OpenDictionary, s_CloseDictionary); //top AStarPathNode topPathNode = GetNewNodeFromPool();//new AStarPathNode(); topPathNode.Row = currentPathNode.Row - 1; topPathNode.Column = currentPathNode.Column; topPathNode.GCalculator = calculator; DealNeighbourPathNode(currentPathNode, topPathNode, endPathNode, s_OpenList, s_CloseList, s_OpenDictionary, s_CloseDictionary); } s_OpenList.Clear(); s_CloseList.Clear(); s_OpenDictionary.Clear(); s_CloseDictionary.Clear(); if (existPath) { List <AStarPathNode> path = new List <AStarPathNode>(); AStarPathNode pathNode = endPathNode; while (pathNode.Column != startPathNode.Column || pathNode.Row != startPathNode.Row) { path.Add(pathNode); pathNode = pathNode.ParentPathNode; } path.Add(startPathNode); path.Reverse(); return(path); } return(null); }
private static List <AStarPathNode> CalculatePath(IGCalculator calculator, AStarPathNode startPathNode, AStarPathNode endPathNode) { bool existPath = false; startPathNode.GCalculator = calculator; s_OpenList.Add(startPathNode); s_OpenDictionary.Add(startPathNode.Column + (startPathNode.Row << 16), startPathNode); //int p = 0; RecycleAllNode(); while (s_OpenList.Count != 0) { s_OpenList.Sort(); AStarPathNode currentPathNode = s_OpenList[0]; s_OpenList.RemoveAt(0); s_OpenDictionary.Remove(currentPathNode.Column + (currentPathNode.Row << 16)); if (endPathNode.Column == currentPathNode.Column && endPathNode.Row == currentPathNode.Row) { existPath = true; endPathNode = currentPathNode; break; } s_CloseList.Add(currentPathNode); s_CloseDictionary.Add(currentPathNode.Column + (currentPathNode.Row << 16), currentPathNode); //leftbottom AStarPathNode leftBottomPathNode = GetNewNodeFromPool(); //new AStarPathNode(); leftBottomPathNode.Row = currentPathNode.Row + 1; leftBottomPathNode.Column = currentPathNode.Column - 1; leftBottomPathNode.GCalculator = calculator; DealNeighbourPathNode(currentPathNode, leftBottomPathNode, endPathNode, s_OpenList, s_CloseList, s_OpenDictionary, s_CloseDictionary); //lefttop AStarPathNode leftTopPathNode = GetNewNodeFromPool();//new AStarPathNode(); leftTopPathNode.Row = currentPathNode.Row - 1; leftTopPathNode.Column = currentPathNode.Column - 1; leftTopPathNode.GCalculator = calculator; DealNeighbourPathNode(currentPathNode, leftTopPathNode, endPathNode, s_OpenList, s_CloseList, s_OpenDictionary, s_CloseDictionary); //rightbottom AStarPathNode rightBottomPathNode = GetNewNodeFromPool();//new AStarPathNode(); rightBottomPathNode.Row = currentPathNode.Row + 1; rightBottomPathNode.Column = currentPathNode.Column + 1; rightBottomPathNode.GCalculator = calculator; DealNeighbourPathNode(currentPathNode, rightBottomPathNode, endPathNode, s_OpenList, s_CloseList, s_OpenDictionary, s_CloseDictionary); //righttop AStarPathNode rightTopPathNode = GetNewNodeFromPool();//new AStarPathNode(); rightTopPathNode.Row = currentPathNode.Row - 1; rightTopPathNode.Column = currentPathNode.Column + 1; rightTopPathNode.GCalculator = calculator; DealNeighbourPathNode(currentPathNode, rightTopPathNode, endPathNode, s_OpenList, s_CloseList, s_OpenDictionary, s_CloseDictionary); //bottom AStarPathNode bottomPathNode = GetNewNodeFromPool();//new AStarPathNode(); bottomPathNode.Row = currentPathNode.Row + 1; bottomPathNode.Column = currentPathNode.Column; bottomPathNode.GCalculator = calculator; DealNeighbourPathNode(currentPathNode, bottomPathNode, endPathNode, s_OpenList, s_CloseList, s_OpenDictionary, s_CloseDictionary); //left AStarPathNode leftPathNode = GetNewNodeFromPool();//new AStarPathNode(); leftPathNode.Row = currentPathNode.Row; leftPathNode.Column = currentPathNode.Column - 1; leftPathNode.GCalculator = calculator; DealNeighbourPathNode(currentPathNode, leftPathNode, endPathNode, s_OpenList, s_CloseList, s_OpenDictionary, s_CloseDictionary); //right AStarPathNode rightPathNode = GetNewNodeFromPool();//new AStarPathNode(); rightPathNode.Row = currentPathNode.Row; rightPathNode.Column = currentPathNode.Column + 1; rightPathNode.GCalculator = calculator; DealNeighbourPathNode(currentPathNode, rightPathNode, endPathNode, s_OpenList, s_CloseList, s_OpenDictionary, s_CloseDictionary); //top AStarPathNode topPathNode = GetNewNodeFromPool();//new AStarPathNode(); topPathNode.Row = currentPathNode.Row - 1; topPathNode.Column = currentPathNode.Column; topPathNode.GCalculator = calculator; DealNeighbourPathNode(currentPathNode, topPathNode, endPathNode, s_OpenList, s_CloseList, s_OpenDictionary, s_CloseDictionary); //p += 8; } //Debug.Log(p); s_OpenList.Clear(); s_CloseList.Clear(); s_OpenDictionary.Clear(); s_CloseDictionary.Clear(); if (existPath) { List <AStarPathNode> path = new List <AStarPathNode>(); AStarPathNode pathNode = endPathNode; while (pathNode.Column != startPathNode.Column || pathNode.Row != startPathNode.Row) { path.Add(pathNode); pathNode = pathNode.ParentPathNode; } path.Add(startPathNode); path.Reverse(); return(path); } return(null); }