public void Navigate(ShipRuntime targetShip, Vector2Int globalStartPos, Vector2Int globalEndPos) { Init(); this.targetShip = targetShip; if (globalStartPos == globalEndPos) { StartCoroutine("DelaySearch"); return; } nextIndex = 1; CellTemplate startCell = targetShip.GetCellByGlobalPos(globalStartPos); CellTemplate endCell = targetShip.GetCellByGlobalPos(globalEndPos); // Invalid path supplied if (startCell == null || endCell == null || startCell.CellState == 0 || endCell.CellState == 0) { return; } ShipPiece startPiece = targetShip.GetPieceByGlobalCellPos(globalStartPos); ShipPiece endPiece = targetShip.GetPieceByGlobalCellPos(globalEndPos); NavGrid startGrid = new NavGrid(startPiece); startGrid.Generate(); NavGrid goalGrid = startGrid; if (startPiece != endPiece) { goalGrid = new NavGrid(endPiece); goalGrid.Generate(); } AStarAlgorithm aStarAlgorithm = new AStarAlgorithm(startGrid, goalGrid, globalStartPos, globalEndPos); int newTTmp = 0; curPath = aStarAlgorithm.AStarSearch(ref newTTmp); wiggleTimer = Random.Range(0, 6.28318f); }
public bool Navigate(ShipRuntime targetShip, Vector2Int globalStartPos, Vector2Int globalEndPos) { if (globalStartPos == globalEndPos) { return(false); } NavCell[] overrideStart = null; // Cur, prev and T evaluation if (curPath != null) { if (nextIndex < curPath.Length) { overrideStart = new NavCell[2]; // Store C0 and C1 overrideStart[0] = curPath[nextIndex - 1]; overrideStart[1] = curPath[nextIndex]; } } nextIndex = 1; CellTemplate startCell = targetShip.GetCellByGlobalPos(globalStartPos); CellTemplate endCell = targetShip.GetCellByGlobalPos(globalEndPos); // Invalid path supplied if (startCell == null || endCell == null || startCell.CellState == 0 || endCell.CellState == 0) { return(false); } ShipPiece startPiece = targetShip.GetPieceByGlobalCellPos(globalStartPos); ShipPiece endPiece = targetShip.GetPieceByGlobalCellPos(globalEndPos); NavGrid startGrid = new NavGrid(startPiece); startGrid.Generate(); NavGrid goalGrid = startGrid; if (startPiece != endPiece) { goalGrid = new NavGrid(endPiece); goalGrid.Generate(); } AStarAlgorithm aStarAlgorithm = new AStarAlgorithm(startGrid, goalGrid, globalStartPos, globalEndPos); int newTState = -1; curPath = aStarAlgorithm.AStarSearch(ref newTState, overrideStart); // Either reset, invert or leave move timer switch (newTState) { case -1: moveTimer = 0; break; case 1: moveTimer = 1 - moveTimer; break; } pathTracer.SetPositions(GetNavArray()); return(curPath != null); }