///////////////////////////////////////////||\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ ///////////////////////////////////// CONSTRUCTOR \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ ///////////////////////////////////////////||\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ //Pre: Row, Column, and the tile type for this tile //Post: The Tile is created using the row, column, and tile type //Desc: A constructor for the current tile public Tile(int row, int column, TileType tileType) { //Sets the current row, column, and tile type Row = row; Column = column; CurrentTileType = tileType; //Calculates the X and Y position of the current tile CalcPostion(); //If the current tile is a wall tile if (CurrentTileType == TileType.Wall) { //Sets four index's to the lines array representing four lines to the tile Lines = new RaycastLine[4]; //Sets the lines for raycasting Lines[0] = new RaycastLine(true, false, new Vector2(PositionRect.X, PositionRect.Y), new Vector2(PositionRect.X, PositionRect.Y + PositionRect.Height)); Lines[1] = new RaycastLine(true, false, new Vector2(PositionRect.X + PositionRect.Width, PositionRect.Y), new Vector2(PositionRect.X + PositionRect.Width, PositionRect.Y + PositionRect.Height)); Lines[2] = new RaycastLine(false, true, new Vector2(PositionRect.X, PositionRect.Y), new Vector2(PositionRect.X + PositionRect.Width, PositionRect.Y)); Lines[3] = new RaycastLine(false, true, new Vector2(PositionRect.X, PositionRect.Y + PositionRect.Height), new Vector2(PositionRect.X + PositionRect.Width, PositionRect.Y + PositionRect.Height)); } //Sets the enemy to unexistant (for map creation) EnemyExists = false; EnemyPlaced = null; }
//Pre: The two lines that the intersection point has to be calculated for //Post: The intersection point between the two lines is returned //Desc: A method for calculating the intersection point between two lines private Vector2 CalcIntersectionPoint(RaycastLine firstLine, RaycastLine secondLine) { //Variables for the X and Y of the intersection point double x = 0; double y = 0; //If the second line is not a vertical line if (!secondLine.VerticalLine) { //If the first line is not horizontal or vertical if (!firstLine.HorizontalLine && !firstLine.VerticalLine) { //Calculates the X of the intersection point x = firstLine.LineM - secondLine.LineM; x = (secondLine.LineB - firstLine.LineB) / x; //Calculates the Y of the intersection point y = (firstLine.LineM * x) + firstLine.LineB; } //If the first line is vertical else if (!firstLine.HorizontalLine && firstLine.VerticalLine) { //Sets the X and Y of the intersection x = firstLine.LineB; y = secondLine.LineB; } } else { //If the first line is not horizontal or vertical if (!firstLine.HorizontalLine && !firstLine.VerticalLine) { //Calculates the Y of the intersection point y = (firstLine.LineM * secondLine.LineB) + firstLine.LineB; //Calculates the X of the intersection point x = (y - firstLine.LineB) / firstLine.LineM; } //If the first line is horizontal else if (firstLine.HorizontalLine && !firstLine.VerticalLine) { //Sets the X and Y of the intersection x = secondLine.LineB; y = firstLine.LineB; } } //Returns the intersection point return(new Vector2((float)x, (float)y)); }
//Pre: The player and game tiles //Post: A boolean is returned for whether the player is in the enemy's line of sight //Desc: A method for determining whether the enemy can see the player private bool IsPlayerInView(Player player, Tile[,] gameTiles) { //Creates vectors for the player and enemy center positions Vector2 centerPlayer = new Vector2(player.Position.X + (player.GetBounds().Width / 2), player.Position.Y + (player.GetBounds().Height / 2)); Vector2 centerEnemy = new Vector2(Position.X + (bounds.Width / 2), Position.Y + (bounds.Height / 2)); //Variable for the player's line RaycastLine playerLine; //If the X's and Y's for the enemy and player are not the same if (centerPlayer.X != centerEnemy.X && centerPlayer.Y != centerEnemy.Y) { //Creates the line from the player to the enemy playerLine = new RaycastLine(false, false, centerPlayer, centerEnemy); } //If they are the same Y values (horizontal line) else if (centerPlayer.Y == centerEnemy.Y) { //Creates the line from the player to the enemy playerLine = new RaycastLine(false, true, centerPlayer, centerEnemy); } //If they are the same X values (vertical line) else { //Creates the line from the player to the enemy playerLine = new RaycastLine(true, false, centerPlayer, centerEnemy); } //Calculates the quadrant for the enemy relative to the player Quadrant playerQuadrant = GetQuadrant(centerPlayer, centerEnemy); //Temp vector for storing the location of the current intersection point Vector2 tempVector = new Vector2(0, 0); //Boolean for whether the enemy can see the player bool playerInView = true; //For each tile in the game tiles array foreach (Tile tile in gameTiles) { //If the current tile is a wall tile if (tile.CurrentTileType == TileType.Wall) { //For each line in the tile foreach (RaycastLine line in tile.Lines) { //Sets the temp vector to the current intersection point tempVector = CalcIntersectionPoint(playerLine, line); //Checks if the quadrant of the position relative to the player is the same quadrant as the enemy to the player if (GetQuadrant(centerPlayer, tempVector) == playerQuadrant) { //If the temp vector x is between the x's of the player and enemy if (tempVector.X >= centerPlayer.X && tempVector.X <= centerEnemy.X || tempVector.X >= centerEnemy.X && tempVector.X <= centerPlayer.X) { //If the temp vector y is between teh y's of the player and enemy if (tempVector.Y >= centerPlayer.Y && tempVector.Y <= centerEnemy.Y || tempVector.Y >= centerEnemy.Y && tempVector.Y <= centerPlayer.Y) { //Checks if the intersection is between the bounds of the tile if (tempVector.X >= tile.PositionRect.X && tempVector.X <= tile.PositionRect.X + tile.PositionRect.Width && tempVector.Y >= tile.PositionRect.Y && tempVector.Y <= tile.PositionRect.Y + tile.PositionRect.Height) { //Sets the player to not being in the enemies view playerInView = false; break; } } } } } //If the player is not in the enemies view if (!playerInView) { //Breaks from the for each loop break; } } } //Returns whether the player is in the enemies view return(playerInView); }