/* get a cell with the shortest distance */ private bool GetNextCell(ref GridSystem.Cell u) { var min = int.MaxValue; var ret = _queue[0]; foreach (var t in _queue) { if (dist[t.X, t.Y] >= min) { continue; } min = dist[t.X, t.Y]; ret = grid.GetCellAt(t.X, t.Y); } /* if every cell has a distance == INFINITE, * it means there are no available paths */ if (min == int.MaxValue) { return(false); } u = ret; _queue.Remove(ret); return(true); }
//Updates the game board to remove the tower public bool RemoveTower(Tower towerPtr) { GridSystem.Cell towerCell = GameGridSystem.MainGameGrid.GetCellAt(towerPtr.X, towerPtr.Y); towerCell.SetCell(false); _towersHolder.Remove(towerPtr); return(true); }
/* return true if the (neigh) is an exit point */ private bool _CheckNeighbour(GridSystem.Cell neigh, GridSystem.Cell from, int alt) { if (neigh.IsExit) { dist[neigh.X, neigh.Y] = alt; prev[neigh.X, neigh.Y] = from; return(true); } if (!neigh.IsBlocked) { if (alt < dist[neigh.X, neigh.Y]) { dist[neigh.X, neigh.Y] = alt; prev[neigh.X, neigh.Y] = from; } } return(false); }
void CreateBoardTiles() { BoardTiles = new Tile[GridWidth, GridHeight]; GridSystem.Grid gameGrid = GameGridSystem.MainGameGrid; foreach (var entrance in gameGrid.Entrances) { _gmInstance.SearchFlyingPath(entrance.X, entrance.Y); } Vector3 startPosition = new Vector3(0f, 0f, 0f); startPosition.x = 0 - (GridWidth / 2.0f) * TileSize; startPosition.y = 0 - (GridHeight / 2.0f) * TileSize; startPosition.z = 0f; //Create grid for (int y = 0; y < GridHeight; ++y) { for (int x = 0; x < GridWidth; ++x) { GridSystem.Cell cell = gameGrid.GetCellAt(x, y); if (cell.IsEntrance) { BoardTiles[x, y] = new Tile(TileEntrance, x, y, TileSize, _boardHolder, startPosition); } else if (cell.IsExit) { BoardTiles[x, y] = new Tile(TileExit, x, y, TileSize, _boardHolder, startPosition); } else if (cell.IsBlocked) { BoardTiles[x, y] = new Tile(TileObstacles, x, y, TileSize, _boardHolder, startPosition); } else { BoardTiles[x, y] = new Tile(TileGound, x, y, TileSize, _boardHolder, startPosition); } } } }
// Updates and checks if a tower can be build at grind position public bool BuildTower(Tower towerPtr) { GridSystem.Cell towerCell = GameGridSystem.MainGameGrid.GetCellAt(towerPtr.X, towerPtr.Y); towerCell.SetCell(true); foreach (var entrance in GameGridSystem.MainGameGrid.Entrances) { bool valid; List <GridSystem.Cell> paths = _gmInstance.SearchPathFrom(entrance.X, entrance.Y); valid = paths.Count != 0; if (!valid) { towerCell.SetCell(false); return(false); } } _towersHolder.Add(towerPtr); return(true); }
/* argument: (x): int. As the x-coordinate of the starting point, and * (y): int. As an y-coordinate of the starting point. * return: List<GridSystem.Cell>: the list contains all cells on the path * from the starting point to the closest exit point; However, * if there are no available path, the list should be EMPTY! */ private List <GridSystem.Cell> Search(int x, int y) { var path = new List <GridSystem.Cell>(); var u = new GridSystem.Cell(0, 0); var neigh = new GridSystem.Cell(0, 0); dist[x, y] = 0; /* keep searching until we run out all cells */ while (_queue.Count > 0) { /* assign u with the lowest dist in queue * if false returned, there're no available path. * pathfinding fail. */ if (!GetNextCell(ref u)) { RefreshCache(); return(path); } var alt = dist[u.X, u.Y] + 1; /* the distance is simply dist[u] + 1 */ /* foreach neighbour of u, updates their distance from an * entry point. Whenever the neighbour is an exit point, break. * for we have found the shortest path. */ if (u.Y != 0) { neigh = grid.GetCellAt(u.X, u.Y - 1); // the one close to the bottom of screen if (_CheckNeighbour(neigh, u, alt)) { break; } } if (u.X + 1 < grid.Width) { neigh = grid.GetCellAt(u.X + 1, u.Y); // the right one if (_CheckNeighbour(neigh, u, alt)) { break; } } if (u.X != 0) { neigh = grid.GetCellAt(u.X - 1, u.Y); // the left one if (_CheckNeighbour(neigh, u, alt)) { break; } } if (u.Y + 1 < grid.Height) { neigh = grid.GetCellAt(u.X, u.Y + 1); // the upper one if (_CheckNeighbour(neigh, u, alt)) { break; } } } /* if the program hits here, the neigh now should be one of exit points. * push it to the path, and tracking backwards to the start points */ path.Add(neigh); while (prev[neigh.X, neigh.Y] != null) { neigh = prev[neigh.X, neigh.Y]; path.Add(neigh); } path.Reverse(); RefreshCache(); return(path); }
public void SpawnEnemy(int wave) { List <GridSystem.Cell> entrances = GameManager.Instance.CurrentLevelManager.GameBoardSystem.GameGridSystem.MainGameGrid.Entrances; int entrance = Random.Range(0, entrances.Count); int temp = Random.Range(0, 4); if (wave == 1) { temp = 0; } if (wave == 2) { temp = 1; } if (wave == 3) { temp = 2; } if (wave == 4) { temp = 3; } if (wave % 5 == 0) { temp = Random.Range(4, 7); } List <GridSystem.Cell> path; if ((Enemy.Type)temp != Enemy.Type.Flying && (Enemy.Type)temp != Enemy.Type.BossFly) { path = GameManager.Instance.SearchPathFrom(entrances[entrance].X, entrances[entrance].Y); } else { path = GameManager.Instance.SearchFlyingPath(entrances[entrance].X, entrances[entrance].Y); } var tiles = new List <GameBoard.Tile>(); foreach (GridSystem.Cell t in path) { tiles.Add(_gameBoard.BoardTiles[t.X, t.Y]); } GridSystem.Cell startCell = entrances[0]; GameBoard.Tile startTile = tiles[0]; Vector3 spawnPosition = startTile.Position; GameObject enemeyGameObject = Instantiate(Enemies[temp], spawnPosition, Quaternion.identity) as GameObject; if (enemeyGameObject != null) { enemeyGameObject.transform.SetParent(this.transform); //Enemies.Add(enemeyGameObject); Enemy enemy = enemeyGameObject.GetComponent <Enemy>(); _enemies.Add(enemy); enemy.SetupEnemy(startCell.X, startCell.Y, tiles, path, (Enemy.Type)temp); _gameBoard.AddEnemy(enemy); } }