public Cell[] SearchRoute(Cell start, Cell target) { if(map[target.I][target.J] == null) return null; int[][] routeMap = new int[rows][]; for(int i = 0; i < rows; i++) { routeMap[i] = new int[columns]; for(int j = 0; j < columns; j++) routeMap[i][j] = map[i][j] != null ? -1 : -1000; } routeMap[start.I][start.J] = 0; int value = 0; bool isRun = true; do { var actualCells = GetCellsWithValue(value, routeMap); if(actualCells.Count() == 0) { isRun = false; continue; } foreach(var cell in actualCells) { var cellsToMark = GetNearestPositios(cell.I, cell.J, routeMap, -1); foreach(var cellToMark in cellsToMark) routeMap[cellToMark.I][cellToMark.J] = value + 1; } value++; if(routeMap[target.I][target.J] != -1) isRun = false; } while(isRun); if(routeMap[target.I][target.J] == -1) return null; List<Cell> result = new List<Cell>(); result.Add(map[target.I][target.J]); Cell currentCell = map[target.I][target.J]; int targetValue = routeMap[currentCell.I][currentCell.J] - 1; isRun = true; do { var cells = GetNearestPositios(currentCell.I, currentCell.J, routeMap, targetValue); currentCell = cells[0]; targetValue = routeMap[currentCell.I][currentCell.J] - 1; result.Add(currentCell); } while(targetValue > 0); result.Reverse(); return result.ToArray(); }
private void CellClickHandler(Cell cell) { if(Player.IsMove) return; var route = wave.SearchRoute(LastCell, cell.CellData); LastCell = cell.CellData; Player.Move(route); OnGameEndAction += BallsBuffer.OnGameEnd; }
public WaveRouteSearch(Cell[][] cellsMap, int rows, int columns) { map = cellsMap; this.rows = rows; this.columns = columns; }
public void InitRoom(Assets.Scripts.Data.Level level) { this.Rows = level.Rows; this.Columns = level.Columns; this.CellsMap = new Cell[level.Rows][]; wave = new WaveRouteSearch(level.Cells, level.Rows, level.Columns); for(int i = 0; i < this.Rows; i++) { this.CellsMap[i] = new Cell[this.Columns]; for(int j = 0; j < this.Columns; j++) { if(level.Cells[i][j] == null) continue; Cell cell = Instantiate(this.CellPrefab); cell.CellData = level.Cells[i][j]; cell.transform.parent = this.transform; cell.transform.position = new Vector3(i, 0, j); if(level.Cells[i][j].Type == CellType.Start) { Player.transform.position = new Vector3(i, Player.transform.position.y, j); LastCell = cell.CellData; } cell.OnClick += this.CellClickHandler; this.CellsMap[i][j] = cell; this.OnGameEndAction += cell.OnGameEnd; } } this.Walls = new Wall[2][] { new Wall[this.Rows], new Wall[this.Columns] }; for(int i = 0; i < 2; i++) { for(int j = 0; j < (i == 0 ? this.Rows : this.Columns); j++) { Wall wall = Instantiate(this.WallPrefab); wall.WallData = level.Walls[i][j]; wall.Buffer = BallsBuffer; wall.transform.parent = this.transform; wall.transform.localPosition = i == 0 ? new Vector3(j, 0, this.Columns - 0.45f) : new Vector3(this.Rows - 0.45f, 0, j); if(i == 1) wall.transform.Rotate(new Vector3(0, 90, 0)); wall.FireDirection = i == 0 ? new Vector3(0, 0, -10) : new Vector3(-10, 0, 0); this.Walls[i][j] = wall; this.OnGameEndAction += wall.OnGameEnd; } } Player.IsAlive = true; }