public void GeneratePath(int startRowIndex, int startColIndex, int endRowIndex, int endColIndex, ref List <Vector3> moveGridList) { if (AStarPathScript.gridMap != null) { AStarPathScript.gridMap.Clear(); } else { AStarPathScript.gridMap = new List <List <bool> >(); } for (int i = 0; i < rowCount; i++) { List <bool> boolList = new List <bool>(); for (int j = 0; j < colCount; j++) { boolList.Insert(j, false); } AStarPathScript.gridMap.Insert(i, boolList); } for (int i = 0; i < tileList.Count; i++) { if (tileList[i].type == TileScript.Type.Walkable_Tile || tileList[i].type == TileScript.Type.Event_Tile || tileList[i].type == TileScript.Type.Fortune_Tile || tileList[i].type == TileScript.Type.Unfortune_Tile || tileList[i].type == TileScript.Type.Prison_Tile) { AStarPathScript.gridMap[tileList[i].rowIndex][tileList[i].colIndex] = true; } } if (AStarPathScript.startIndex == null) { AStarPathScript.startIndex = new NodeIndex(); } AStarPathScript.startIndex.i = startRowIndex; AStarPathScript.startIndex.j = startColIndex; if (AStarPathScript.endIndex == null) { AStarPathScript.endIndex = new NodeIndex(); } AStarPathScript.endIndex.i = endRowIndex; AStarPathScript.endIndex.j = endColIndex; /*for(int i = 0; i < rowCount; i++) * { * List<bool> boolList = new List<bool>(); * * for(int j = 0; j < colCount; j++) * { * if(map[i,j] == 1) * { * boolList.Insert(j, true); * } * else if(map[i,j] == 88) * { * if(AStarPathScript.startIndex == null) * { * AStarPathScript.startIndex = new NodeIndex(); * } * AStarPathScript.startIndex.i = i; * AStarPathScript.startIndex.j = j; * boolList.Insert(j, true); * } * else if(map[i,j] == 99) * { * if(AStarPathScript.endIndex == null) * { * AStarPathScript.endIndex = new NodeIndex(); * } * AStarPathScript.endIndex.i = i; * AStarPathScript.endIndex.j = j; * boolList.Insert(j, true); * } * else * { * boolList.Insert(j, false); * } * } * AStarPathScript.gridMap.Insert(i, boolList); * }*/ AStarPathScript.heuristicType = AStarPathScript.HEURISTIC_METHODS.MANHATTAN; AStarPathScript.FindPath(); if (AStarPathScript.isPathFound) { for (int i = 0; i < AStarPathScript.pathNodes.Count; i++) { Vector3 nodePos = new Vector3(AStarPathScript.pathNodes[i].nodeIndex.j - colCount / 2, 0.0f, rowCount / 2 - AStarPathScript.pathNodes[i].nodeIndex.i); moveGridList.Add(nodePos); } } }
//draw path to player public Vector3 GeneratePath(GameObject from, GameObject to) { //Debug.Log("GeneratePath : " + from.transform.position + " "+ to.transform.position); int startCol = Mathf.RoundToInt((from.transform.position.x - minX) / tileSize); int startRow = Mathf.RoundToInt((from.transform.position.y - minY) / tileSize); int targetCol = Mathf.RoundToInt((to.transform.position.x - minX) / tileSize); int targetRow = Mathf.RoundToInt((to.transform.position.y - minY) / tileSize); startRow = mapRowCount - startRow - 1; targetRow = mapRowCount - targetRow - 1; //Debug.Log ("startCol " + startCol + " startRow " + startRow); //Debug.Log ("targetCol " + targetCol + " targetRow " + targetRow); for (int i = 0; i < mapRowCount; i++) { for (int j = 0; j < mapColCount; j++) { if (i == startRow && j == startCol) { if (AStarPathScript.startIndex == null) { AStarPathScript.startIndex = new NodeIndex(); } AStarPathScript.startIndex.i = i; AStarPathScript.startIndex.j = j; } else if (i == targetRow && j == targetCol) { if (AStarPathScript.endIndex == null) { AStarPathScript.endIndex = new NodeIndex(); } AStarPathScript.endIndex.i = i; AStarPathScript.endIndex.j = j; } } } AStarPathScript.heuristicType = AStarPathScript.HEURISTIC_METHODS.MANHATTAN; AStarPathScript.FindPath(); Vector3 nextPosition = from.transform.position; if (AStarPathScript.isPathFound) { nextPosition.x = AStarPathScript.pathNodes[1].nodeIndex.j * tileSize + minX; nextPosition.y = (mapRowCount - AStarPathScript.pathNodes[1].nodeIndex.i - 1) * tileSize + minY; //Debug.Log("NEXT NODE : " + AStarPathScript.pathNodes[0].nodeIndex.i + " "+ AStarPathScript.pathNodes[0].nodeIndex.j); pathNodesList.Clear(); /*for (int i = 0; i < AStarPathScript.pathNodes.Count; i++) * { * pathNodesList.Add (AStarPathScript.pathNodes [i]); * Vector3 markerPos = Vector3.zero; * markerPos.x = AStarPathScript.pathNodes[i].nodeIndex.j * tileSize + minX; * markerPos.y = (mapRowCount - AStarPathScript.pathNodes[i].nodeIndex.i - 1) * tileSize + minY; * Instantiate (pathMarkerPrefab, markerPos, Quaternion.identity); * //Debug.Log(AStarPathScript.pathNodes[i].nodeIndex.j + " "+ AStarPathScript.pathNodes[i].nodeIndex.i); * }*/ } /*string gridText = ""; * for(int i = 0; i < mapRowCount; i++) * { * for (int j = 0; j < mapColCount; j++) * { * bool isDisplayed = false; * for (int k = 0; k < AStarPathScript.pathNodes.Count; k++) * { * if (AStarPathScript.pathNodes [k].nodeIndex.j == j && * AStarPathScript.pathNodes [k].nodeIndex.i == i) * { * gridText += "X"; * isDisplayed = true; * break; * } * } * if(!isDisplayed) * { * gridText += gridList [i, j] + " "; * } * } * gridText += "\n"; * } * * Debug.Log (gridText);*/ //Debug.Log ("nextPosition " + nextPosition.x + " " + nextPosition.y); //pathNodesList.RemoveAt (0); return(nextPosition); }
public void GeneratePath(int startRowIndex, int startColIndex, int endRowIndex, int endColIndex, ref List <Vector3> moveGridList) { if (AStarPathScript.gridMap != null) { AStarPathScript.gridMap.Clear(); } else { AStarPathScript.gridMap = new List <List <bool> >(); } for (int i = 0; i < rowCount; i++) { List <bool> boolList = new List <bool>(); for (int j = 0; j < colCount; j++) { boolList.Insert(j, false); } AStarPathScript.gridMap.Insert(i, boolList); } for (int i = 0; i < tileList.Count; i++) { if (tileList[i].type == TileScript.Type.PLATFORM) { AStarPathScript.gridMap[tileList[i].rowIndex][tileList[i].colIndex] = true; } } if (AStarPathScript.startIndex == null) { AStarPathScript.startIndex = new NodeIndex(); } AStarPathScript.startIndex.i = startRowIndex; AStarPathScript.startIndex.j = startColIndex; if (AStarPathScript.endIndex == null) { AStarPathScript.endIndex = new NodeIndex(); } AStarPathScript.endIndex.i = endRowIndex; AStarPathScript.endIndex.j = endColIndex; /*for(int i = 0; i < rowCount; i++) * { * List<bool> boolList = new List<bool>(); * * for(int j = 0; j < colCount; j++) * { * if(map[i,j] == 1) * { * boolList.Insert(j, true); * } * else if(map[i,j] == 88) * { * if(AStarPathScript.startIndex == null) * { * AStarPathScript.startIndex = new NodeIndex(); * } * AStarPathScript.startIndex.i = i; * AStarPathScript.startIndex.j = j; * boolList.Insert(j, true); * } * else if(map[i,j] == 99) * { * if(AStarPathScript.endIndex == null) * { * AStarPathScript.endIndex = new NodeIndex(); * } * AStarPathScript.endIndex.i = i; * AStarPathScript.endIndex.j = j; * boolList.Insert(j, true); * } * else * { * boolList.Insert(j, false); * } * } * AStarPathScript.gridMap.Insert(i, boolList); * }*/ AStarPathScript.heuristicType = AStarPathScript.HEURISTIC_METHODS.MANHATTAN; AStarPathScript.FindPath(); if (AStarPathScript.isPathFound) { for (int i = 0; i < AStarPathScript.pathNodes.Count; i++) { Vector3 nodePos = new Vector3(AStarPathScript.pathNodes[i].nodeIndex.j * 0.64f + 3.92f - colCount / 2, rowCount / 2 - AStarPathScript.pathNodes[i].nodeIndex.i * 0.64f - 0.72f, 0.0f); //Instantiate(pathPrefab, new Vector3(1.0f * AStarPathScript.pathNodes[i].nodeIndex.j, 2.0f, -1.0f * AStarPathScript.pathNodes[i].nodeIndex.i) - new Vector3(1.0f * mapColCount / 2 - 0.5f, 0.0f, -1.0f * mapRowCount / 2 + 0.5f), Quaternion.identity); moveGridList.Add(nodePos); } } }