public Grid(int SquareSize, int defDist) { gridSquares = new Squares[GameManager.WIDTH, GameManager.HEIGHT]; for (int y = 0; y < GameManager.HEIGHT; y++) { for (int x = 0; x < GameManager.WIDTH; x++) { gridSquares[x, y] = new Squares(SquareSize, new Vector2(x * SquareSize + GameManager.BORDERLEFT, y * SquareSize + GameManager.BORDERTOP), x, y, defDist); } } gridBorder = new Vector2(gridSquares[0, 0].sqrLoc.X, gridSquares[0, 0].sqrLoc.Y); gridStatus = gridFlags.empty; updateTimer = TimeSpan.Zero; GenerateNewMap(); gridSquares[(int)GameManager.ENDPOINT.x, (int)GameManager.ENDPOINT.y].typeOfSquare |= Squares.SqrFlags.StopPoint; gridSquares[(int)GameManager.ENDPOINT.x, (int)GameManager.ENDPOINT.y].Building = Squares.BuildingType.Base; gridStatus = gridFlags.endPoint; pathFound = GridManager.GridPaths(gridSquares); }
public bool PathMove(Squares[,] squares, int height, int width, ref Vector2 EnemyVect, ref Vector2 ScreenPos, float speed, float time, Vector2 Direction, string EnemyType) { if (!Moving) { ScreenPos = new Vector2(Node.X, Node.Y); if (EnemyType != "Helicopter") { if (currentCoord.x + 1 < width) // check array wont go out of bounds { if (!squares[(int)currentCoord.x + 1, (int)currentCoord.y].typeOfSquare.HasFlag(Squares.SqrFlags.Wall)) //check next square is not a wall { if (squares[(int)currentCoord.x + 1, (int)currentCoord.y].sqrCoord.counter < tempInt) // check the square distance from endpoint is less than the current pos. { tempInt = squares[(int)currentCoord.x + 1, (int)currentCoord.y].sqrCoord.counter; // set the tempint to new distance value nextCoord = new Coordinates((int)currentCoord.x + 1, (int)currentCoord.y, tempInt); // set temp coord to the aipos + direction. } } } if (currentCoord.x - 1 >= 0) // check array wont go out of bounds { if (!squares[(int)currentCoord.x - 1, (int)currentCoord.y].typeOfSquare.HasFlag(Squares.SqrFlags.Wall)) //check next square is not a wall { if (squares[(int)currentCoord.x - 1, (int)currentCoord.y].sqrCoord.counter < tempInt) // check the square distance from endpoint is less than the current pos. { tempInt = squares[(int)currentCoord.x - 1, (int)currentCoord.y].sqrCoord.counter; // set the tempint to new distance value nextCoord = new Coordinates((int)currentCoord.x - 1, (int)currentCoord.y, tempInt); // set temp coord to the aipos + direction. } if (squares[(int)currentCoord.x - 1, (int)currentCoord.y].sqrCoord.counter == tempInt && GameManager.rnd.Next(0, 2) == 1) { tempInt = squares[(int)currentCoord.x - 1, (int)currentCoord.y].sqrCoord.counter; // set the tempint to new distance value nextCoord = new Coordinates((int)currentCoord.x - 1, (int)currentCoord.y, tempInt); // set temp coord to the aipos + direction. } } } if (currentCoord.y + 1 < height) // check array wont go out of bounds { if (!squares[(int)currentCoord.x, (int)currentCoord.y + 1].typeOfSquare.HasFlag(Squares.SqrFlags.Wall)) //check next square is not a wall { if (squares[(int)currentCoord.x, (int)currentCoord.y + 1].sqrCoord.counter < tempInt) // check the square distance from endpoint is less than the current pos. { tempInt = squares[(int)currentCoord.x, (int)currentCoord.y + 1].sqrCoord.counter; // set the tempint to new distance value nextCoord = new Coordinates((int)currentCoord.x, (int)currentCoord.y + 1, tempInt); // set temp coord to the aipos + direction. } if (squares[(int)currentCoord.x, (int)currentCoord.y + 1].sqrCoord.counter == tempInt && GameManager.rnd.Next(0, 2) == 1) { tempInt = squares[(int)currentCoord.x, (int)currentCoord.y + 1].sqrCoord.counter; // set the tempint to new distance value nextCoord = new Coordinates((int)currentCoord.x, (int)currentCoord.y + 1, tempInt); // set temp coord to the aipos + direction. } } } if (currentCoord.y - 1 >= 0) // check array wont go out of bounds { if (!squares[(int)currentCoord.x, (int)currentCoord.y - 1].typeOfSquare.HasFlag(Squares.SqrFlags.Wall)) //check next square is not a wall { if (squares[(int)currentCoord.x, (int)currentCoord.y - 1].sqrCoord.counter < tempInt) // check the square distance from endpoint is less than the current pos. { tempInt = squares[(int)currentCoord.x, (int)currentCoord.y - 1].sqrCoord.counter; // set the tempint to new distance value nextCoord = new Coordinates((int)currentCoord.x, (int)currentCoord.y - 1, tempInt); // set temp coord to the aipos + direction. } if (squares[(int)currentCoord.x, (int)currentCoord.y - 1].sqrCoord.counter == tempInt && GameManager.rnd.Next(0, 2) == 1) { tempInt = squares[(int)currentCoord.x, (int)currentCoord.y - 1].sqrCoord.counter; // set the tempint to new distance value nextCoord = new Coordinates((int)currentCoord.x, (int)currentCoord.y - 1, tempInt); // set temp coord to the aipos + direction. } } } } else if (EnemyType == "Helicopter") { List<Coordinates> availableCoord = new List<Coordinates>(); foreach (Squares square in squares) { if (square.sqrCoord.counter != GameManager.DEFAULYDIST) availableCoord.Add(square.sqrCoord); } int indexNext = GameManager.rnd.Next(0, availableCoord.Count); nextCoord = availableCoord[indexNext]; if (GameManager.rnd.Next(0, 100) == 1) nextCoord = GameManager.ENDPOINT; } Node = new Vector2((nextCoord.x * GameManager.SQUARESIZE) + GameManager.SQUARESIZE / 2, (int)GameManager.grid.gridBorder.Y + (nextCoord.y * GameManager.SQUARESIZE) + GameManager.SQUARESIZE / 2); distance = Vector2.Distance(new Vector2(currentCoord.x, currentCoord.y), new Vector2(nextCoord.x, nextCoord.y)); Direction = new Vector2(nextCoord.x - currentCoord.x, nextCoord.y - currentCoord.y); Movement = Direction; Direction.Normalize(); previousVect = new Vector2(currentCoord.x, currentCoord.y); Moving = true; } if (Moving) { EnemyVect += Direction * speed / GameManager.FPS ; if (Vector2.Distance(previousVect, EnemyVect) >= distance) { if(Direction.Y >= 0 && Direction.X >= 0) ScreenPos = new Vector2(Node.X, Node.Y); Moving = false; if (EnemyType == "Helicopter") { for (int i = 0; i < 3; i++) { EnemyManager.SpawnEnemy(EnemyManager.SpawnSoldierString, EnemyVect - new Vector2(2 * i / 4, 0)); } } return false; } return true; } else return false; }
public static bool InaccessibleSquareCheck(Squares[,] Grid, Coordinates SquareCoords) { Squares[,] TempGrid; TempGrid = Grid; TempGrid[(int)SquareCoords.x, (int)SquareCoords.y].typeOfSquare = Squares.SqrFlags.Wall; TempGrid[(int)SquareCoords.x, (int)SquareCoords.y].Building = Squares.BuildingType.Trench; if (GridPaths(TempGrid)) { TempGrid[(int)SquareCoords.x, (int)SquareCoords.y].typeOfSquare = Squares.SqrFlags.Unoccupied; TempGrid[(int)SquareCoords.x, (int)SquareCoords.y].Building = Squares.BuildingType.None; return true; } else { TempGrid[(int)SquareCoords.x, (int)SquareCoords.y].typeOfSquare = Squares.SqrFlags.Unoccupied; TempGrid[(int)SquareCoords.x, (int)SquareCoords.y].Building = Squares.BuildingType.None; return false; } }
public bool PathMove(Squares[,] squares, int height, int width, ref Vector2 EnemyVect, ref Vector2 ScreenPos, float speed, float time, Vector2 Direction) { bool wallfound = false; if (!Moving) { ScreenPos = new Vector2(Node.X, Node.Y); if (currentCoord.x + 1 < width) // check array wont go out of bounds { if (squares[(int)currentCoord.x + 1, (int)currentCoord.y].typeOfSquare.HasFlag(Squares.SqrFlags.Wall)) //check next square is not a wall { tempInt = squares[(int)currentCoord.x + 1, (int)currentCoord.y].sqrCoord.counter; // set the tempint to new distance value nextCoord = new Coordinates((int)currentCoord.x + 1, (int)currentCoord.y, tempInt); // set temp coord to the aipos + direction. wallfound = true; } } if (currentCoord.x - 1 >= 0) // check array wont go out of bounds { if (squares[(int)currentCoord.x - 1, (int)currentCoord.y].typeOfSquare.HasFlag(Squares.SqrFlags.Wall)) //check next square is not a wall { if ((wallfound == true && GameManager.rnd.Next(0, 5) == 1) || !wallfound) { tempInt = squares[(int)currentCoord.x - 1, (int)currentCoord.y].sqrCoord.counter; // set the tempint to new distance value nextCoord = new Coordinates((int)currentCoord.x - 1, (int)currentCoord.y, tempInt); // set temp coord to the aipos + direction. wallfound = true; } } } if (currentCoord.y + 1 < height) // check array wont go out of bounds { if (squares[(int)currentCoord.x, (int)currentCoord.y + 1].typeOfSquare.HasFlag(Squares.SqrFlags.Wall)) //check next square is not a wall { if ((wallfound == true && GameManager.rnd.Next(0, 5) == 1) || !wallfound) { tempInt = squares[(int)currentCoord.x, (int)currentCoord.y + 1].sqrCoord.counter; // set the tempint to new distance value nextCoord = new Coordinates((int)currentCoord.x, (int)currentCoord.y + 1, tempInt); // set temp coord to the aipos + direction. wallfound = true; } } } if (currentCoord.y - 1 >= 0 && currentCoord.x < 20) // check array wont go out of bounds { if (squares[(int)currentCoord.x, (int)currentCoord.y - 1].typeOfSquare.HasFlag(Squares.SqrFlags.Wall)) //check next square is not a wall { if ((wallfound == true && GameManager.rnd.Next(0, 5) == 1) || !wallfound) { tempInt = squares[(int)currentCoord.x, (int)currentCoord.y - 1].sqrCoord.counter; // set the tempint to new distance value nextCoord = new Coordinates((int)currentCoord.x, (int)currentCoord.y - 1, tempInt); // set temp coord to the aipos + direction. wallfound = true; } } } Node = new Vector2((nextCoord.x * GameManager.SQUARESIZE) + GameManager.SQUARESIZE / 2, (int)GameManager.grid.gridBorder.Y + (nextCoord.y * GameManager.SQUARESIZE) + GameManager.SQUARESIZE / 2); distance = Vector2.Distance(new Vector2(currentCoord.x, currentCoord.y), new Vector2(nextCoord.x, nextCoord.y)); Direction = new Vector2(nextCoord.x - currentCoord.x, nextCoord.y - currentCoord.y); Movement = Direction; Direction.Normalize(); previousVect = new Vector2(currentCoord.x, currentCoord.y); Moving = true; } if (Moving) { EnemyVect += Direction * speed * time; if (Vector2.Distance(previousVect, EnemyVect) >= distance) { if (Direction.Y >= 0 && Direction.X >= 0) ScreenPos = new Vector2(Node.X, Node.Y); Moving = false; return false; } return true; } else return false; }
public static bool HasNeighbour(Squares.BuildingType typeOfBuilding, Coordinates coord) { // Check North if (coord.y > 0) if (GameManager.grid.gridSquares[(int)coord.x, (int)coord.y - 1].Building == typeOfBuilding) return true; // Check East if (coord.x < GameManager.WIDTH - 1) if (GameManager.grid.gridSquares[(int)coord.x + 1, (int)coord.y].Building == typeOfBuilding) return true; // Check South if (coord.y < GameManager.HEIGHT - 1) if (GameManager.grid.gridSquares[(int)coord.x, (int)coord.y + 1].Building == typeOfBuilding) return true; // Check West if (coord.x > 0) if (GameManager.grid.gridSquares[(int)coord.x - 1, (int)coord.y].Building == typeOfBuilding) return true; return false; }
public Grid(int SquareSize, int defDist) { gridSquares = new Squares[GameManager.WIDTH, GameManager.HEIGHT]; for (int y = 0; y < GameManager.HEIGHT; y++) for (int x = 0; x < GameManager.WIDTH; x++) gridSquares[x, y] = new Squares(SquareSize, new Vector2(x * SquareSize + GameManager.BORDERLEFT, y * SquareSize + GameManager.BORDERTOP), x, y, defDist); gridBorder = new Vector2(gridSquares[0, 0].sqrLoc.X, gridSquares[0, 0].sqrLoc.Y); gridStatus = gridFlags.empty; updateTimer = TimeSpan.Zero; GenerateNewMap(); gridSquares[(int)GameManager.ENDPOINT.x, (int)GameManager.ENDPOINT.y].typeOfSquare |= Squares.SqrFlags.StopPoint; gridSquares[(int)GameManager.ENDPOINT.x, (int)GameManager.ENDPOINT.y].Building = Squares.BuildingType.Base; gridStatus = gridFlags.endPoint; pathFound = GridManager.GridPaths(gridSquares); }
static void squaresCounter(int counter, Squares[,] Grid, List<Coordinates> tempCoords) { for (int v = 0; v < tempCoords.Count; v++) { if (Grid[(int)tempCoords[v].x, (int)tempCoords[v].y].sqrCoord.counter == 0 || Grid[(int)tempCoords[v].x, (int)tempCoords[v].y].sqrCoord.counter == GameManager.DEFAULYDIST) Grid[(int)tempCoords[v].x, (int)tempCoords[v].y].sqrCoord.counter = counter; } }
static int GetTrenchCount(Squares[,] Grid) { int count = 0; foreach (Squares square in Grid) { if (square.Building.HasFlag(Squares.BuildingType.Trench) || square.Building.HasFlag(Squares.BuildingType.Tower)) count++; } return count; }
public static bool GridPaths(Squares[,] Grid) { GridReset(Grid); bool loopStop = true; bool done = false; int count = 0; List<Coordinates> coords; List<Coordinates> tempCoords; Coordinates currentElement; coords = new List<Coordinates>(); tempCoords = new List<Coordinates>(); currentElement = new Coordinates(0,0); if (!done) { coords.Add(GameManager.ENDPOINT); currentElement = coords[count]; Grid[(int)GameManager.ENDPOINT.x, (int)GameManager.ENDPOINT.y].sqrCoord.counter = GameManager.ENDPOINT.counter; done = true; } while (loopStop) { //Check right square if (currentElement.x + 1 < GameManager.WIDTH) if (!Grid[(int)currentElement.x + 1, (int)currentElement.y].typeOfSquare.HasFlag(Squares.SqrFlags.Wall)) tempCoords.Add(new Coordinates(currentElement.x + 1, currentElement.y, currentElement.counter + 1)); //check left square if (currentElement.x - 1 >= 0) if (!Grid[(int)currentElement.x - 1, (int)currentElement.y].typeOfSquare.HasFlag(Squares.SqrFlags.Wall)) tempCoords.Add(new Coordinates(currentElement.x - 1, currentElement.y, currentElement.counter + 1)); //check lower square if (currentElement.y + 1 < GameManager.HEIGHT) if (!Grid[(int)currentElement.x, (int)currentElement.y + 1].typeOfSquare.HasFlag(Squares.SqrFlags.Wall)) tempCoords.Add(new Coordinates(currentElement.x, currentElement.y + 1, currentElement.counter + 1)); //check upper square if (currentElement.y - 1 >= 0) if (!Grid[(int)currentElement.x, (int)currentElement.y - 1].typeOfSquare.HasFlag(Squares.SqrFlags.Wall)) tempCoords.Add(new Coordinates(currentElement.x, currentElement.y - 1, currentElement.counter + 1)); duplicateCheck(ref count, tempCoords, coords); squaresCounter(currentElement.counter + 1, Grid, tempCoords); for (int i = 0; i < tempCoords.Count; i++) coords.Add(tempCoords[i]); count++; if (count < coords.Count()) currentElement = coords[count]; tempCoords.Clear(); if (count == (GameManager.WIDTH * GameManager.HEIGHT) - GetTrenchCount(Grid)) loopStop = false; } Grid[(int)GameManager.ENDPOINT.x, (int)GameManager.ENDPOINT.y].sqrCoord.counter = GameManager.ENDPOINT.counter; if (!CheckSquareCounters(Grid)) return false; return true; }
static void GridReset(Squares[,] Grid) { for (int y = 0; y < GameManager.HEIGHT; y++) for (int x = 0; x < GameManager.WIDTH; x++) { Grid[x, y].sqrCoord.counter = GameManager.DEFAULYDIST; } }
public static bool CheckSquareCounters(Squares[,] Grid) { foreach (Squares Square in Grid) { if (Square.sqrCoord.counter == GameManager.DEFAULYDIST && Square.typeOfSquare == Squares.SqrFlags.Unoccupied) return false; } return true; }