public void DebugDrawShip() { foreach (ShipPiece piece in shipPieces) { for (int j = 0; j < piece.Height; ++j) { for (int i = 0; i < piece.Width; ++i) { CellTemplate cell = piece.GetShipCell(i, j); if (piece.GetShipCell(i, j).CellState != 0) { Vector3 p = new Vector3(piece.Position.x + i, piece.Position.y + j, 0) * 3.2f; if ((piece.GetShipCell(i, j).CurWallState & WallState.Up) != WallState.None) { Debug.DrawLine(transform.TransformPoint(p + new Vector3(1.5f, 1.5f, 0)), transform.TransformPoint(p + new Vector3(-1.5f, 1.5f, 0)), Color.red); } if ((piece.GetShipCell(i, j).CurWallState & WallState.Right) != WallState.None) { Debug.DrawLine(transform.TransformPoint(p + new Vector3(1.5f, -1.5f, 0)), transform.TransformPoint(p + new Vector3(1.5f, 1.5f, 0)), Color.red); } if ((piece.GetShipCell(i, j).CurWallState & WallState.Down) != WallState.None) { Debug.DrawLine(transform.TransformPoint(p + new Vector3(-1.5f, -1.5f, 0)), transform.TransformPoint(p + new Vector3(1.5f, -1.5f, 0)), Color.red); } if ((piece.GetShipCell(i, j).CurWallState & WallState.Left) != WallState.None) { Debug.DrawLine(transform.TransformPoint(p + new Vector3(-1.5f, 1.5f, 0)), transform.TransformPoint(p + new Vector3(-1.5f, -1.5f, 0)), Color.red); } } } } int connectionCount = piece.GetConnectionCount(); for (int i = 0; i < connectionCount; ++i) { ShipConnection connection = piece.GetConnectionByIndex(i); Vector3 p0 = new Vector3(connection.LocalCell.x + piece.Position.x, connection.LocalCell.y + piece.Position.y, 0) * 3.2f; Vector3 p1 = new Vector3(connection.OtherCell.x + connection.OtherPiece.Position.x, connection.OtherCell.y + connection.OtherPiece.Position.y, 0) * 3.2f; Debug.DrawLine( transform.TransformPoint(p0), transform.TransformPoint(p1), Color.green); } } }
public NavCell[] AStarSearch(ref int newTState, NavCell[] overrideStart = null) { openList.Clear(); closedList.Clear(); var startCell = grids[0].FindCellByPosition(start); var goalCell = grids[grids.Count - 1].FindCellByPosition(goal); startCell.heuristic = (goal - startCell.position).magnitude; openList.Add(startCell); int itterations = 0; while (openList.Count > 0) { itterations++; var bestCell = GetBestCell(); openList.Remove(bestCell); var neighbours = bestCell.OwningGrid.GetMooreNeighbours(bestCell); for (int i = 0; i < 4; i++) { var curCell = neighbours[i]; if (curCell == null) { bool connectionMade = false; if (bestCell.Cell.HasConnections) { Vector2Int compareDir = new Vector2Int(0, 1); switch (i) { case 1: compareDir = new Vector2Int(1, 0); break; case 2: compareDir = new Vector2Int(0, -1); break; case 3: compareDir = new Vector2Int(-1, 0); break; } // Collect new neighbours ShipConnection connection = bestCell.Cell.OwningPiece.GetConnectionByCellPos(bestCell.position, compareDir); if (connection != null) { connectionMade = true; // Attempt to locate existing grid NavGrid connectedGrid = null; foreach (NavGrid grid in grids) { if (connection.OtherPiece == grid.PieceCreatedFrom) { connectedGrid = grid; break; } } // Else Generate new grid from piece if (connectedGrid == null) { connectedGrid = new NavGrid(connection.OtherPiece); connectedGrid.Generate(); grids.Add(connectedGrid); } // Assign the cell on the other grid as the cur cell and continue pathfinding as normal curCell = connectedGrid.FindCellByPosition(connection.OtherCell + connection.OtherPiece.Position); } } if (!connectionMade) { continue; } } if (curCell == goalCell) { curCell.parent = bestCell; return(ConstructPath(curCell, ref newTState, overrideStart)); } // Cell / Wall state logic if (curCell.Cell.CellState == 0) // Cell state is 0 (piece does not contain this cell as walkable area). { // Add to closed and continue if (!closedList.Contains(curCell)) { closedList.Add(curCell); } continue; } var g = bestCell.cost + (curCell.position - bestCell.position).magnitude; var h = (goal - curCell.position).magnitude; if (openList.Contains(curCell) && curCell.f < (g + h)) { continue; } if (closedList.Contains(curCell) && curCell.f < (g + h)) { continue; } curCell.cost = g; curCell.heuristic = h; curCell.parent = bestCell; if (!openList.Contains(curCell)) { openList.Add(curCell); } } if (!closedList.Contains(bestCell)) { closedList.Add(bestCell); } if (itterations > 10000) { Debug.Log("MAX ITTERATIONS REACHED, NOT GOOD"); break; } } return(null); }