public void Initialize(TileMap_MapData.TileMap_TileData correspondingTile, TileMap_MapData.TileMap_TileData destinationTile) { Parent = null; Passable = correspondingTile.Passable; CalculateDistanceToDestination(destinationTile); }
private void Refresh(TileMap_MapData.TileMap_TileData[,] tileData, TileMap_MapData.TileMap_TileData destinationTile) { openList.Clear(); closedList.Clear(); FinalTrack.Clear(); currentNode = null; for (int z = 0; z < column; z++) { for (int x = 0; x < row; x++) { node[x, z].Initialize(tileData[x, z], destinationTile); } } }
private void CalculateDistanceToDestination(TileMap_MapData.TileMap_TileData destinationTile) { int destinationX = destinationTile.Index.x; int destinationZ = destinationTile.Index.z; int xDistance = Mathf.Abs(destinationX - Index.x); int zDistance = Mathf.Abs(destinationZ - Index.z); if (xDistance - zDistance == 0) { DistanceToDestination = 14 * zDistance; } else { int linearDistance = Mathf.Abs(xDistance - zDistance); int furtherAxis = (xDistance - zDistance > 0) ? xDistance : zDistance; int diagonalDistance = furtherAxis - linearDistance; DistanceToDestination = linearDistance * 10 + diagonalDistance * 14; } }
public bool FindPath(TileMap_MapData.TileMap_TileData[,] tileData, TileMap_MapData.TileMap_TileData startingTile, TileMap_MapData.TileMap_TileData destinationTile) { Refresh(tileData, destinationTile); currentNode = node[startingTile.Index.x, startingTile.Index.z]; closedList.Add(currentNode); int failureCount = row * column; while (true) { for (int z = currentNode.Index.z - 1; z < currentNode.Index.z + 2; z++) { for (int x = currentNode.Index.x - 1; x < currentNode.Index.x + 2; x++) { if (NodeIndexAvailable(x, z) == false) { continue; } if (node[x, z].Passable == false) { continue; } if (NodeInClosedList(node[x, z]) == true) { continue; } if (NodeInOpenList(node[x, z]) == false) { node[x, z].Parent = currentNode; node[x, z].CalculateCostToDestination(); openList.Add(node[x, z]); } else { Node_AStar newData = new Node_AStar(node[x, z]); newData.Parent = currentNode; newData.CalculateCostToDestination(); if (newData.CostToDestination < node[x, z].CostToDestination) { node[x, z].Parent = currentNode; node[x, z].CalculateCostToDestination(); } } } } int lowestCost = 99999999; for (int i = 0; i < openList.Count; i++) { if (openList[i].CostToDestination < lowestCost) { lowestCost = openList[i].CostToDestination; currentNode = openList[i]; } } if (currentNode == node[destinationTile.Index.x, destinationTile.Index.z]) { int whileBreaker = row * column; while (true) { FinalTrack.Add(currentNode); if (currentNode == node[startingTile.Index.x, startingTile.Index.z]) { return(true); } currentNode = currentNode.Parent; whileBreaker--; if (whileBreaker < 0) { return(false); } } } openList.Remove(currentNode); closedList.Add(currentNode); failureCount--; if (failureCount < 0) { return(false); } } }
// Update is called once per frame void Update() { if (Input.GetKeyDown(KeyCode.UpArrow)) { currentTileIndex.z++; if ((currentTileIndex.z >= TilesColumn) || (MapData.TileData[currentTileIndex.x, currentTileIndex.z].Passable == false)) { currentTileIndex.z--; return; } SetPosition(currentTileIndex); } if (Input.GetKeyDown(KeyCode.DownArrow)) { currentTileIndex.z--; if ((currentTileIndex.z < 0) || (MapData.TileData[currentTileIndex.x, currentTileIndex.z].Passable == false)) { currentTileIndex.z++; return; } SetPosition(currentTileIndex); } if (Input.GetKeyDown(KeyCode.LeftArrow)) { currentTileIndex.x--; if ((currentTileIndex.x < 0) || (MapData.TileData[currentTileIndex.x, currentTileIndex.z].Passable == false)) { currentTileIndex.x++; return; } SetPosition(currentTileIndex); } if (Input.GetKeyDown(KeyCode.RightArrow)) { currentTileIndex.x++; if ((currentTileIndex.x >= TilesRow - 1) || (MapData.TileData[currentTileIndex.x, currentTileIndex.z].Passable == false)) { currentTileIndex.x--; return; } SetPosition(currentTileIndex); } if (Input.GetKeyDown(KeyCode.Space)) { TileMap_MapData.TileMap_TileData destination = null; for (int z = 0; z < TilesColumn; z++) { for (int x = 0; x < TilesRow; x++) { if (MapData.TileData[x, z].Type == TileMap_MapData.TileType.Destination) { destination = MapData.TileData[x, z]; break; } } } bool pathFound = false; pathFound = aStar.FindPath(MapData.TileData, MapData.TileData[currentTileIndex.x, currentTileIndex.z], destination); if (pathFound == true) { for (int i = 0; i < aStar.FinalTrack.Count; i++) { MapData.TileData[aStar.FinalTrack[i].Index.x, aStar.FinalTrack[i].Index.z].Update(TileMap_MapData.TileType.Track); } tileMap.GetComponent <TileMap>().SetupTexture(); } else { Debug.Log("Let's give up."); } } }
public Node_AStar(TileMap_MapData.TileMap_TileData correspondingTile) { Index = correspondingTile.Index; }