//draws all the walls using the given spritebatch and texture public void draw(SpriteBatch spriteBatch, Texture2D texture, bool gameEnds, bool active, Player player1, Player player2) { foreach (Wall wall in list) { wall.draw(spriteBatch, texture, gameEnds, active, player1, player2); } }
//level 3 = hard, AI player gets in turns medium or very hard targets public Vector2 hardTarget(Player player) { if (hardIsUsingMedium) { return mediumTarget(); } else { return nearestPill(player.getPosition("")); } }
//checks if player hits a moving wall while player is moving only towards one way public void checkPlayerHits(Player player1, Player player2) { if (player1.movingOnlyTowardsOneWay()) { if (player1.getPosition("").Intersects(getPosition("top"))) { player1.changeDirectionY(float.MinValue); } else if (player1.getPosition("").Intersects(getPosition("bottom"))) { player1.changeDirectionY(float.MaxValue); } else if (player1.getPosition("").Intersects(getPosition("left"))) { player1.changeDirectionX(float.MinValue); } else if (player1.getPosition("").Intersects(getPosition("right"))) { player1.changeDirectionX(float.MaxValue); } } if (player2.movingOnlyTowardsOneWay()) { if (player2.getPosition("").Intersects(getPosition("top"))) { player2.changeDirectionY(float.MinValue); } else if (player2.getPosition("").Intersects(getPosition("bottom"))) { player2.changeDirectionY(float.MaxValue); } else if (player2.getPosition("").Intersects(getPosition("left"))) { player2.changeDirectionX(float.MinValue); } else if (player2.getPosition("").Intersects(getPosition("right"))) { player2.changeDirectionX(float.MaxValue); } } }
public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; graphics.PreferredBackBufferHeight = 600; graphics.PreferredBackBufferWidth = 800; player1 = new Player(); clock = new Timer(); fps = new FPS(800); }
//returns new target for AI, target depends on the level of AI public Vector2 getDirection(Player player) { if (level == 1) { return easyTarget(); } else if (level == 2) { return mediumTarget(); } else if (level == 3) { return hardTarget(player); } else { return veryHardTarget(player); } }
//checks who won the game private void whoWon(Player player1, Player player2) { //player1 wins if (player1.getScore() > player2.getScore()) { winner = player1; loser = player2; } //player2 wins else if (player2.getScore() > player1.getScore()) { winner = player2; loser = player1; } //the game ends in a tie else { tie = true; winner = player1; } }
//creates a new gameplay screen public GameplayScreen(string player1Name, string player2Name, string nameOfTheMap, bool player1IsAI, bool player2IsAI, int player1AILevel, int player2AILevel) { //times for the transition TransitionOnTime = TimeSpan.FromSeconds(0.5); TransitionOffTime = TimeSpan.FromSeconds(0.5); //sets the escape-key as the pause button and enter as the restart game button pauseAction = new InputAction(new Keys[] { Keys.Escape }, true); endGameAction = new InputAction(new Keys[] { Keys.Enter }, true); screenWidth = 1024; screenHeight = 768; p1name = player1Name; p2name = player2Name; mapName = nameOfTheMap; p1ai = player1IsAI; p2ai = player2IsAI; p1aiLevel = player1AILevel; p2aiLevel = player2AILevel; clock = new Timer(20.0f); //fps = new FPS(screenWidth); map = new Map(mapName); pills = new Pills(map, 100, screenWidth, screenHeight, 32); player1 = new Player(0, 0, screenWidth, screenHeight, p1name, map, p1ai, p1aiLevel, pills); player2 = new Player(screenWidth - 32, screenHeight - 32, screenWidth, screenHeight, p2name, map, p2ai, p2aiLevel, pills); scores = new Scores(screenWidth); controls = new PlayerControls(); gameEnds = false; }
//changes directions of both players according to keyboard's current state public void checkKeyboardStatus(KeyboardState keyState, Player player1, Player player2) { if (player1.isInputEnabled()) { if (player1.isAI()) //player 1 is AI { player1.getAI().moveAIPlayer(player1); } else { if (keyState.IsKeyDown(Keys.W) && keyState.IsKeyDown(Keys.A)) { player1.changeBothDirections(float.MinValue, float.MinValue); } else if (keyState.IsKeyDown(Keys.W) && keyState.IsKeyDown(Keys.D)) { player1.changeBothDirections(float.MaxValue, float.MinValue); } else if (keyState.IsKeyDown(Keys.S) && keyState.IsKeyDown(Keys.A)) { player1.changeBothDirections(float.MinValue, float.MaxValue); } else if (keyState.IsKeyDown(Keys.S) && keyState.IsKeyDown(Keys.D)) { player1.changeBothDirections(float.MaxValue, float.MaxValue); } else if (keyState.IsKeyDown(Keys.W)) { player1.changeDirectionY(float.MinValue); } else if (keyState.IsKeyDown(Keys.S)) { player1.changeDirectionY(float.MaxValue); } else if (keyState.IsKeyDown(Keys.A)) { player1.changeDirectionX(float.MinValue); } else if (keyState.IsKeyDown(Keys.D)) { player1.changeDirectionX(float.MaxValue); } } } if (player2.isInputEnabled()) { if (player2.isAI()) //player 2 is AI { player2.getAI().moveAIPlayer(player2); } else { if (keyState.IsKeyDown(Keys.Up) && keyState.IsKeyDown(Keys.Left)) { player2.changeBothDirections(float.MinValue, float.MinValue); } else if (keyState.IsKeyDown(Keys.Up) && keyState.IsKeyDown(Keys.Right)) { player2.changeBothDirections(float.MaxValue, float.MinValue); } else if (keyState.IsKeyDown(Keys.Down) && keyState.IsKeyDown(Keys.Left)) { player2.changeBothDirections(float.MinValue, float.MaxValue); } else if (keyState.IsKeyDown(Keys.Down) && keyState.IsKeyDown(Keys.Right)) { player2.changeBothDirections(float.MaxValue, float.MaxValue); } else if (keyState.IsKeyDown(Keys.Up)) { player2.changeDirectionY(float.MinValue); } else if (keyState.IsKeyDown(Keys.Down)) { player2.changeDirectionY(float.MaxValue); } else if (keyState.IsKeyDown(Keys.Left)) { player2.changeDirectionX(float.MinValue); } else if (keyState.IsKeyDown(Keys.Right)) { player2.changeDirectionX(float.MaxValue); } } } }
public void moveUpAndRight(Player otherPlayer) { towardsOneWay = false; if (topEdges() || position.Intersects(otherPlayer.getPosition("bottom"))) { bounceFromUpAndRight(false); } else if (rightEdges() || position.Intersects(otherPlayer.getPosition("left"))) { bounceFromUpAndRight(true); } else { up(); right(); } }
public void moveUp(Player otherPlayer) { towardsOneWay = true; if (topEdges() || position.Intersects(otherPlayer.getPosition("bottom"))) { bounceDown(); } else { up(); } }
//moves player towards the direction vector public void moveTowardsDirection(Player otherPlayer) { if (playerGotStuck()) { position = startPosition; } if (position.X > direction.X && position.Y > direction.Y) { moveUpAndLeft(otherPlayer); } else if (position.X < direction.X && position.Y > direction.Y) { moveUpAndRight(otherPlayer); } else if (position.X > direction.X && position.Y < direction.Y) { moveDownAndLeft(otherPlayer); } else if (position.X < direction.X && position.Y < direction.Y) { moveDownAndRight(otherPlayer); } else if (position.X > direction.X) { moveLeft(otherPlayer); } else if (position.X < direction.X) { moveRight(otherPlayer); } else if (position.Y > direction.Y) { moveUp(otherPlayer); } else if (position.Y < direction.Y) { moveDown(otherPlayer); } }
public void moveRight(Player otherPlayer) { towardsOneWay = true; if (rightEdges() || position.Intersects(otherPlayer.getPosition("left"))) { bounceLeft(); } else { right(); } }
public void moveDownAndRight(Player otherPlayer) { towardsOneWay = false; if (bottomEdges() || position.Intersects(otherPlayer.getPosition("top"))) { bounceFromDownAndRight(false); } else if (rightEdges() || position.Intersects(otherPlayer.getPosition("left"))) { bounceFromDownAndRight(true); } else { down(); right(); } }
//sets a new target for AI player if it doesn't already have one or the previous target has been reached public void moveAIPlayer(Player player) { //if AI player hits wall enough times it will be given a random direction //fixes a bug where AI player got "stuck" on wall when the nearest pill was in the otherside of the wall if (player.wall("") || yetAnotherCounter > 0) { wallHitCounter++; if (wallHitCounter > 10) { if (!moveToRandomDirection) { player.setDirection(randomDirection()); moveToRandomDirection = true; } if (yetAnotherCounter > 80) { wallHitCounter = 0; yetAnotherCounter = 0; moveToRandomDirection = false; } else { yetAnotherCounter++; } } } //level 1 AI's direction change interval if (level == 1 && targetAcquired) { counter++; if (counter > 50) { counter = 0; targetAcquired = false; } } //level 3 AI's medium/very hard level change intervals else if (level == 3 && hardIsUsingMedium) { counter++; if (counter > 100) { hardIsUsingMedium = false; counter = 0; targetAcquired = false; } } else if (level == 3 && !hardIsUsingMedium) { counter++; if (counter > 100) { hardIsUsingMedium = true; counter = 0; } } //no target or no need to send the AI player to a random direction if (!targetAcquired && !moveToRandomDirection) { player.setDirection(getDirection(player)); } }
//draws the wall using the given spritebatch and texture public void draw(SpriteBatch spriteBatch, Texture2D texture, bool gameEnds, bool active, Player player1, Player player2) { if (!gameEnds && active) //game doesn't end and it isn't paused { if (isMoving) { moveWall(player1, player2); } if (isInflating) { inflateWall(player1, player2); } if (partTimeVisible) { hideOrShowWall(); } } if (isVisible) { spriteBatch.Draw(texture, position, color); } }
//draws the scores of given players with given sprite batch and font public void draw(SpriteBatch spriteBatch, SpriteFont font, Player player1, Player player2) { spriteBatch.DrawString(font, player1.getName() + ": " + player1.getScore(), p1, Color.White); spriteBatch.DrawString(font, player2.getName() + ": " + player2.getScore(), p2, Color.White); }
//level 4 = very hard, AI player gets always the location of the nearest pill and tries to go towards it public Vector2 veryHardTarget(Player player) { targetAcquired = false; return nearestPill(player.getPosition("")); }
//inflates the wall public void inflateWall(Player player1, Player player2) { checkPlayerHits(player1, player2); if (isMoving) { if (isVertical) { if (position.Width < 0 || position.Width > (originalPosition.Width + inflateLimit)) { speed = -1 * speed; } } else { if (position.Height < 0 || position.Height > (originalPosition.Height + inflateLimit)) { speed = -1 * speed; } } } else { if (position.Width < originalPosition.Width || position.Width > (originalPosition.Width + inflateLimit) || position.Height < originalPosition.Height || position.Height > (originalPosition.Height + inflateLimit)) { speed = -1 * speed; } } if (isVertical) { position.Width = position.Width + speed; } else { position.Height = position.Height + speed; } }
//creates a new endscreen public EndScreen(Player player1, Player player2) { tie = false; whoWon(player1, player2); }
//moves the wall public void moveWall(Player player1, Player player2) { checkPlayerHits(player1, player2); if (position.Intersects(originalPosition) || position.Intersects(limitPosition)) { speed = -1 * speed; } if (isVertical) //vertical wall, moves left and right { position.X = position.X + speed; } else //horizontal wall, moves up and down { position.Y = position.Y + speed; } }