Advertisement
Guest User

Turbo

a guest
Jul 2nd, 2012
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 39.38 KB | None | 0 0
  1. // --------------------------------------------------------------------------------------------------------------------
  2.  
  3. // <summary>
  4. //   This screen implements the actual game logic. It is just a
  5. //   placeholder to get the idea across: you'll probably want to
  6. //   put some more interesting gameplay in here!
  7. // </summary>
  8. // --------------------------------------------------------------------------------------------------------------------
  9.  
  10.  
  11. namespace Pong
  12. {
  13.     using System;
  14.     using System.Collections.Generic;
  15.  
  16.     using Microsoft.Xna.Framework;
  17.     using Microsoft.Xna.Framework.Audio;
  18.     using Microsoft.Xna.Framework.Content;
  19.     using Microsoft.Xna.Framework.Graphics;
  20.     using Microsoft.Xna.Framework.Input;
  21.     using Microsoft.Xna.Framework.Media;
  22.  
  23.  
  24.  
  25.     /// <summary>
  26.     /// This screen implements the actual game logic. It is just a
  27.     /// placeholder to get the idea across: you'll probably want to
  28.     /// put some more interesting gameplay in here!
  29.     /// </summary>
  30.     public class GameplayScreen : GameScreen
  31.     {
  32.  
  33.        
  34.         #region Fields
  35.  
  36.         // For shaders
  37.         Effect gscaleEffect;
  38.         float percent;
  39.         KeyboardState current, previous;
  40.         public bool gScaleActivated;
  41.         public float ElapsedTime;
  42.         public float percentscale;
  43.  
  44.         // Graphics stuff
  45.         private Vector2 activatedVec;
  46.         private SpriteFont arial;
  47.         private Texture2D backgroundTexture;
  48.         private ContentManager contentManager;
  49.         private int coolDown;
  50.         private Vector2 deactivatedVec;
  51.         private int disableCooldown;
  52.         private SpriteFont font;
  53.         private HUD hud;
  54.         private Menu menu;
  55.         private Input input;
  56.         private float pauseAlpha;
  57.         private Animation powerupAnimate;
  58.  
  59.         // All things having to do with the powerup
  60.         private Powerup playerOnePowerup;
  61.         private Powerup playerTwoPowerup;
  62.         private int powerDisableCooldown = 2000;
  63.         private int powerEnableCooldown = 5000;
  64.         private bool powerupReady;
  65.         private Vector2 promptVec;
  66.         private Vector2 leftBatVec;
  67.         private Vector2 rightBatVec;
  68.         private Vector2 ballSpeedVec;
  69.         private Random random;
  70.         private int resetTimer;
  71.         private bool resetTimerInUse;
  72.  
  73.         private SpriteBatch spriteBatch;
  74.         private double timer; // timer for the powerup countdown
  75.         private SpriteFont timerFont;
  76.         private Vector2 timerVector;
  77.         private Vector2 vec;
  78.         private Vector2 vec2;
  79.         private double powerupResetDelay;
  80.         // The timer for when the menu will appear after a game resets
  81.         private double screenLoadDelay;
  82.  
  83.         public static GameplayScreen Instance;
  84.         public static GameStates gamestate;
  85.         public int screenHeight;
  86.         public int screenWidth;
  87.         public ParticleEngine particleEngine;
  88.         public Ball ball;
  89.         public Bat leftBat;
  90.         public AIBat rightBat;
  91.         public bool side;
  92.         public bool lastScored;
  93.  
  94.         //   public Song GamePlaySong { get; private set; }
  95.         public PlayerIndex? controllingp { get; set; }
  96.         public enum GameStates
  97.         {
  98.             Menu,
  99.             Running,
  100.             Paused,
  101.             End,
  102.             Won,
  103.             Lost
  104.         }
  105.  
  106.         #endregion
  107.  
  108.         #region Methods
  109.  
  110.         /// <summary>
  111.         /// Constructor.
  112.         /// </summary>
  113.         public GameplayScreen()
  114.         {
  115.             TransitionOnTime = TimeSpan.FromSeconds(1.5);
  116.             TransitionOffTime = TimeSpan.FromSeconds(0.5);
  117.         }
  118.  
  119.         protected void Initialize()
  120.         {
  121.             // Start the score off at 0
  122.             lastScored = false;
  123.             resetTimer = 0;
  124.             menu = new Menu(); // Draws text on the screen after the game to say who won
  125.             resetTimerInUse = true;
  126.             SetUpSingle(); // Sets up a single player game
  127.             ball = new Ball(contentManager,
  128.                             new Vector2 (ScreenManager.Game.GraphicsDevice.Viewport.TitleSafeArea.Width,
  129.                             ScreenManager.Game.GraphicsDevice.Viewport.TitleSafeArea.Height),
  130.                             leftBat, rightBat);
  131.        
  132.             input = new Input();
  133.             hud = new HUD(this);
  134.             gamestate = GameStates.Running;
  135.      
  136.             // The timer for when the menu will appear after a game resets.
  137.             // Screen will auto appear after 8 seconds
  138.             screenLoadDelay = 8.0;
  139.             powerupResetDelay = 6.0;
  140.  
  141.             // Places the powerup animation inside of the surrounding box
  142.             // TODO Needs to be cleaned up, instead of using hard pixel values
  143.             powerupAnimate = new Animation(contentManager.Load<Texture2D>(@"gfx/powerupSpriteSheet"),
  144.                                             new Vector2(103, 44), 64, 64, 4, 5);
  145.  
  146.             // Rolls a random powerup
  147.             random = new Random();
  148.             vec = new Vector2(100, 50);
  149.             vec2 = new Vector2(100, 100);
  150.             promptVec = new Vector2(50, 25);
  151.             leftBatVec = new Vector2(100, 200);
  152.             rightBatVec = new Vector2(100, 250);
  153.             ballSpeedVec = new Vector2(100, 300);
  154.  
  155.             // Starting value for the cooldown for the powerup timer
  156.             timer = 3000.0f;
  157.          
  158.             // Where the timer appears on the screen
  159.             timerVector = new Vector2(10, 10);
  160.  
  161.             // JEP - one time creation of powerup objects
  162.             playerOnePowerup = new Powerup();
  163.             playerOnePowerup.Activated += PowerupActivated;
  164.             playerOnePowerup.Deactivated += PowerupDeactivated;
  165.  
  166.             playerTwoPowerup = new Powerup();
  167.             playerTwoPowerup.Activated += PowerupActivated;
  168.             playerTwoPowerup.Deactivated += PowerupDeactivated;
  169.  
  170.             // Location where activated / deactivated powerup notifications will appear
  171.             activatedVec = new Vector2(100, 125);
  172.             deactivatedVec = new Vector2(100, 150);
  173.             powerupReady = false;
  174.  
  175.             // JEP 5/17 - draw code uses screenwidth/height
  176.             screenWidth = ScreenManager.Game.GraphicsDevice.Viewport.TitleSafeArea.Width;
  177.             screenHeight = ScreenManager.Game.GraphicsDevice.Viewport.TitleSafeArea.Height;
  178.            
  179.             // Sets the default percent scale for the black and white SlowMo effect
  180.             percentscale = 0.01f;
  181.             gScaleActivated = false;
  182.         }
  183.  
  184.         /// <summary>
  185.         /// Load graphics content for the game.
  186.         /// </summary>
  187.         public override void LoadContent()
  188.         {
  189.             if (contentManager == null)
  190.             {
  191.                 contentManager = new ContentManager(ScreenManager.Game.Services, "Content");
  192.             }
  193.             this.Initialize();
  194.             arial = contentManager.Load<SpriteFont>(@"gfx/fonts/Arial"); // for game scores
  195.             spriteBatch = new SpriteBatch(ScreenManager.GraphicsDevice);
  196.             backgroundTexture = contentManager.Load<Texture2D>(@"gfx/Backgrounds/StageBackgroundScreen1");
  197.             hud.LoadContent(contentManager);
  198.             AudioManager.Instance.PlaySoundTrack(); // Plays the music from AudioManager class
  199.             font = contentManager.Load<SpriteFont>(@"gfx/fonts/Arial"); // Used by the Powerup      
  200.             timerFont = contentManager.Load<SpriteFont>(@"gfx/fonts/Arial"); // Used or the countdown time for
  201.             ScreenManager.Game.ResetElapsedTime();
  202.  
  203.             // Everything to do with particles
  204.             List<Texture2D> textures = new List<Texture2D>();
  205.             textures.Add(contentManager.Load<Texture2D>(@"gfx/particle/circle"));
  206.             textures.Add(contentManager.Load<Texture2D>(@"gfx/particle/star"));
  207.             textures.Add(contentManager.Load<Texture2D>(@"gfx/particle/diamond"));
  208.             particleEngine = new ParticleEngine(textures, new Vector2());
  209.  
  210.             // For black and white shader. Starts off by default.
  211.             gscaleEffect = contentManager.Load<Effect>(@"gfx/effects/hit");
  212.            //percent = 1;
  213.         }
  214.  
  215.         //////////////////////////////////// DRAW ///////////////////////////////////////////////////////
  216.  
  217.         /// <summary>
  218.         /// Draws the gameplay screen.
  219.         /// </summary>
  220.         public override void Draw(GameTime gameTime)
  221.         {
  222.             ScreenManager.GraphicsDevice.Clear(ClearOptions.Target, Color.CornflowerBlue, 0, 0);
  223.             SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  224.  
  225.             if (gamestate == GameStates.Running)
  226.             {
  227.                 // For shader: If % is <= 0 then draw all objects normally.
  228.                 if (gScaleActivated == false)
  229.                 {
  230.                     spriteBatch.Begin();
  231.                     // Draws background
  232.                     spriteBatch.Draw(backgroundTexture, new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
  233.                     //// Slow mo stuff from Michael Harts
  234.                     //spriteBatch.Draw(rect, new Rectangle((int)position.X, (int)position.Y, 300, 300), Color.Red);
  235.                     // Draws Animation for Powerup
  236.                     powerupAnimate.Draw(spriteBatch);
  237.                     //Draws the HUD
  238.                     hud.Draw(gameTime, spriteBatch);
  239.                     // Draws the bats and ball
  240.                     leftBat.Draw(spriteBatch);
  241.                     rightBat.Draw(spriteBatch);
  242.                     // Draw the ball
  243.                     ball.Draw(spriteBatch);
  244.                     // Draws the particle engine
  245.                     particleEngine.Draw(spriteBatch);
  246.                     // Draws powerup timer countdown
  247.                     spriteBatch.DrawString(timerFont, ((int)timer / 1000).ToString(), timerVector, Color.White);
  248.                     #region powerup display (text for debugging)
  249.                     // Text that appears on the screen to notify the player of powerups. Currently there for debug.
  250.                     if (gamestate == GameStates.Running)
  251.                     {
  252.                         spriteBatch.DrawString(font, "Press A or Left Alt button to generate powerup", promptVec, Color.White);
  253.  
  254.                         spriteBatch.DrawString(font, "LeftBat Speed = " + leftBat.moveSpeed, leftBatVec, Color.White);
  255.                         spriteBatch.DrawString(font, "RightBat Speed = " + rightBat.moveSpeed, rightBatVec, Color.White);
  256.                         spriteBatch.DrawString(font, "Ball Speed = " + ball.speed, ballSpeedVec, Color.White);
  257.  
  258.                         if (powerupReady)
  259.                         {
  260.                             spriteBatch.DrawString(font, "Powerup Type: " + playerOnePowerup.Type, vec, Color.White);
  261.                             spriteBatch.DrawString(
  262.                                 font, "Press Left Bumper or  Left Ctrl to activate powerup", vec2, Color.White);
  263.                         }
  264.  
  265.                         // JEP - changed check to use powerup status
  266.                         if (!playerOnePowerup.IsAlive)
  267.                         {
  268.                             spriteBatch.DrawString(font, "Powerup deactivated", deactivatedVec, Color.White);
  269.                         }
  270.  
  271.                         if (playerOnePowerup.IsAlive)
  272.                         {
  273.                             spriteBatch.DrawString(font, "Powerup activated", activatedVec, Color.White);
  274.                         }
  275.                     }
  276.                     #endregion
  277.                     // Draws the GameScreen class, and all associated text during the game
  278.                     base.Draw(gameTime);
  279.                     spriteBatch.End();
  280.                 }
  281.                 else // Otherwise activate the grayscale effect
  282.                 {
  283.                     // The actual grayscale effect
  284.                     spriteBatch.Begin(0, null, null, null, null, gscaleEffect);
  285.                     // Draws background
  286.                     spriteBatch.Draw(backgroundTexture, new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
  287.                     //Draws the HUD
  288.                     hud.Draw(gameTime, spriteBatch);
  289.                     // Draws bats
  290.                     leftBat.Draw(spriteBatch);
  291.                     rightBat.Draw(spriteBatch);
  292.                     // Draw the ball
  293.                     ball.Draw(spriteBatch);
  294.                     // Draws the particle engine
  295.                     particleEngine.Draw(spriteBatch);
  296.                     // Draws the GameScreen class, and all associated text during the game
  297.                     // Draws powerup timer countdown
  298.                     spriteBatch.DrawString(timerFont, ((int)timer / 1000).ToString(), timerVector, Color.White);
  299.                     #region powerup display (text for debugging)
  300.                     // Text that appears on the screen to notify the player of powerups. Currently there for debug.
  301.                     if (gamestate == GameStates.Running)
  302.                     {
  303.                         spriteBatch.DrawString(font, "Press A or Left Alt button to generate powerup", promptVec, Color.White);
  304.  
  305.  
  306.                         spriteBatch.DrawString(font, "LeftBat Speed = " + leftBat.moveSpeed, leftBatVec, Color.White);
  307.                         spriteBatch.DrawString(font, "RightBat Speed = " + rightBat.moveSpeed, rightBatVec, Color.White);
  308.                         spriteBatch.DrawString(font, "Ball Speed = " + ball.speed, ballSpeedVec, Color.White);
  309.  
  310.                         if (powerupReady)
  311.                         {
  312.                             spriteBatch.DrawString(font, "Powerup Type: " + playerOnePowerup.Type, vec, Color.White);
  313.                             spriteBatch.DrawString(
  314.                                 font, "Press Left Bumper or  Left Ctrl to activate powerup", vec2, Color.White);
  315.                         }
  316.  
  317.                         // JEP - changed check to use powerup status
  318.                         if (!playerOnePowerup.IsAlive)
  319.                         {
  320.                             spriteBatch.DrawString(font, "Powerup deactivated", deactivatedVec, Color.White);
  321.                         }
  322.  
  323.                         if (playerOnePowerup.IsAlive)
  324.                         {
  325.                             spriteBatch.DrawString(font, "Powerup activated", activatedVec, Color.White);
  326.                         }
  327.                     }
  328.                     #endregion
  329.                     base.Draw(gameTime);
  330.                     spriteBatch.End();
  331.              
  332.                 }
  333.             }
  334.  
  335.             else if (gamestate == GameStates.End || gamestate == GameStates.Won || gamestate == GameStates.Lost)
  336.             {
  337.                 spriteBatch.Begin();
  338.                 spriteBatch.Draw(backgroundTexture, new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
  339.                 menu.DrawEndScreen(spriteBatch, screenWidth, arial);
  340.                 spriteBatch.End();
  341.             }
  342.  
  343.             // If the game is transitioning on or off, fade it out to black.
  344.             if (TransitionPosition > 0 || pauseAlpha > 0)
  345.             {
  346.                 float alpha = MathHelper.Lerp(1f - TransitionAlpha, 1f, pauseAlpha / 2);
  347.                 ScreenManager.FadeBackBufferToBlack(alpha);
  348.             }
  349.         }
  350.  
  351.  
  352.         //////////////////////////////////////////// UPDATE ////////////////////////////////////////
  353.         /// <summary>
  354.         /// Lets the game respond to player input. Unlike the Update method,
  355.         /// this will only be called when the gameplay screen is active.
  356.         /// </summary>
  357.         public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
  358.         {
  359.             base.Update(gameTime, otherScreenHasFocus, false);
  360.  
  361.             // Gradually fade in or out depending on whether we are covered by the pause screen.
  362.             if (coveredByOtherScreen)
  363.             {
  364.                 pauseAlpha = Math.Min(pauseAlpha + 1f / 32, 1);
  365.                 MediaPlayer.Pause(); // Pause gameplay music
  366.             }
  367.             else
  368.             {
  369.                 pauseAlpha = Math.Max(pauseAlpha - 1f / 32, 0);
  370.                 MediaPlayer.Resume(); // Resum gameplay music
  371.             }
  372.  
  373.  
  374.  //////////////////// If the game is *not* paused, then update the following: /////////////////////
  375.  
  376.             if (IsActive)
  377.             {
  378.                 #region Movement of bat update
  379.                 // Controls movement of the bats              
  380.                 if (rightBat.GetType() != typeof(AIBat))
  381.                 {
  382.                     if (input.IsLeftPaddleMovingDown(ControllingPlayer))
  383.                     {
  384.                         leftBat.MoveDown();
  385.                     }
  386.                     else if (input.IsLeftPaddleMovingUp(ControllingPlayer))
  387.                     {
  388.                         leftBat.MoveUp();
  389.                     }
  390.  
  391.                     if (input.IsRightPaddleMovingDown(ControllingPlayer))
  392.                     {
  393.                         rightBat.MoveDown();
  394.                     }
  395.                     else if (input.IsRightPaddleMovingUp(ControllingPlayer))
  396.                     {
  397.                         rightBat.MoveUp();
  398.                     }
  399.                 }
  400.                 else if (rightBat.GetType() == typeof(AIBat))
  401.                 {
  402.                     if (input.IsLeftPaddleMovingDown(ControllingPlayer))
  403.                     {
  404.                         leftBat.MoveDown();
  405.                     }
  406.                     else if (input.IsLeftPaddleMovingUp(ControllingPlayer))
  407.                     {
  408.                         leftBat.MoveUp();
  409.                     }
  410.  
  411.                     if (input.IsRightPaddleMovingDown(ControllingPlayer))
  412.                     {
  413.                         leftBat.MoveDown();
  414.                     }
  415.                     else if (input.IsRightPaddleMovingUp(ControllingPlayer))
  416.                     {
  417.                         leftBat.MoveUp();
  418.                     }
  419.                 }
  420.                 #endregion
  421.  
  422.  
  423.                 #region Powerup timer
  424.                 // updates timer for powerups so that it counts down
  425.                 if (gamestate == GameStates.Running)
  426.                 {
  427.                     // begins to count down for the powerup
  428.                     timer -= gameTime.ElapsedGameTime.TotalMilliseconds;
  429.  
  430.                     if (this.powerupReady)
  431.                     {
  432.                         powerupResetDelay -= gameTime.ElapsedGameTime.TotalSeconds;
  433.  
  434.                         if (powerupResetDelay < 0)
  435.                         {
  436.                             powerupResetDelay = 6;
  437.                             // if a powerup has just expired then....
  438.                             timer = 10000.0f; // start the timer at 10 seconds
  439.  
  440.                         }
  441.                     }
  442.                 }
  443.                #endregion
  444.  
  445.                 #region Powerup Update
  446.                 // JEP 5/3 - don't allow a new powerup to be rolled until the current one is done
  447.                 if (input.RollPowerup(ControllingPlayer) && !playerOnePowerup.IsAlive && timer <= 0.0f)
  448.                 {
  449.                     // generate a random powerup
  450.                     var type = (PowerupType)random.Next(Enum.GetNames(typeof(PowerupType)).Length);
  451.  
  452.                     // Animation for the powerups
  453.                     powerupAnimate.Update(gameTime);
  454.  
  455.                     switch (type)
  456.                     {
  457.                         case PowerupType.BigBalls:
  458.                             {
  459.                                 // JEP - called new method instead of recreating
  460.                                 playerOnePowerup.Reset(type, 5.0f, 1.0f, 0);
  461.                                 break;
  462.                             }
  463.                         //case PowerupType.ShrinkBalls:
  464.                         //    {
  465.                         //        // JEP - called new method instead of recreating
  466.                         //        playerOnePowerup.Reset(type, 5.0f, 1.0f, 0);
  467.                         //        break;
  468.                         //    }
  469.                         //case PowerupType.BigPaddle:
  470.                         //    {
  471.                         //        // JEP - called new method instead of recreating
  472.                         //        playerOnePowerup.Reset(type, 5.0f, 1.0f, 0);
  473.                         //        break;
  474.                         //    }
  475.                         //case PowerupType.ShrinkEnemy:
  476.                         //    {
  477.                         //        // JEP - called new method instead of recreating
  478.                         //        playerOnePowerup.Reset(type, 5.0f, 1.0f, 0);
  479.                         //        break;
  480.                         //    }
  481.                         //case PowerupType.SpeedBall:
  482.                         //    {
  483.                         //        // JEP - called new method instead of recreating
  484.                         //        playerOnePowerup.Reset(type, 5.0f, 1.0f, 0);
  485.                         //        break;
  486.                         //    }
  487.                         //case PowerupType.Heal:
  488.                         //    {
  489.                         //        // JEP - called new method instead of recreating
  490.                         //        playerOnePowerup.Reset(type, 1.0f, 1.0f, 0);
  491.                         //        break;
  492.                         //    }
  493.                         case PowerupType.SlowMo:
  494.                             {
  495.                                 playerOnePowerup.Reset(type, 1.0f, 1.0f, 0);
  496.                                 break;
  497.                             }
  498.                     }
  499.  
  500.                     // Without this set to true, the powerups stay on forever and you can continue to keep
  501.                     // activating them over and over without a timer
  502.                     powerupReady = true;
  503.                   }
  504.                 else if (input.ActivatePowerup(ControllingPlayer) & !playerOnePowerup.IsAlive)
  505.                 {
  506.                     // JEP 5/3 - added check to make sure powerup isn't still running
  507.                     playerOnePowerup.Activate();
  508.                 }
  509.                 // JEP - removed initialization of powerup and added check for IsAlive instead
  510.                 if (playerOnePowerup.IsAlive)
  511.                 {
  512.                     playerOnePowerup.Update((float)gameTime.ElapsedGameTime.TotalMilliseconds);
  513.                 }
  514.                 if (playerTwoPowerup.IsAlive)
  515.                 {
  516.                     playerTwoPowerup.Update((float)gameTime.ElapsedGameTime.TotalMilliseconds);
  517.                 }
  518.                 #endregion
  519.  
  520.                 #region what to do when the game is over
  521.                 // What to do when the game is over
  522.                 if (gamestate == GameStates.Running)
  523.                 {
  524.                     // if (hud.currentHealthP2 < 1)
  525.                     if (hud.currentHealthP2 < 90) // currently in to speed up debugging
  526.                     {
  527.                         menu.InfoText = "Game, blouses." + Environment.NewLine + "Prepare for the next game!";
  528.                         gamestate = GameStates.Won;
  529.                         // Reset(); // Resets the ball, bats, and all attributes between matches
  530.                     }
  531.                         // else if (hud.currentHealthP1 < 1)
  532.                     else if (hud.currentHealthP1 < 90) // currently in to speed up debugging
  533.                     {
  534.                         menu.InfoText = "You just let the AI beat you.";
  535.                         gamestate = GameStates.Lost;
  536.                         // Reset(); // Resets the ball, bats, and all attributes between matches
  537.                     }
  538.                 #endregion
  539.  
  540.                     // Updating the input
  541.                     input.Update();
  542.                     // Updating bat position
  543.                     leftBat.UpdatePosition(ball, gameTime);
  544.                     rightBat.UpdatePosition(ball, gameTime);
  545.  
  546.                     // Updating the ball position
  547.                     ball.UpdatePosition(gameTime);
  548.  
  549.                     // Updating the Particle Engine
  550.                     particleEngine.EmitterLocation = new Vector2(Mouse.GetState().X, Mouse.GetState().Y);
  551.                     particleEngine.Update();
  552.                     #region checks for collision of bats
  553.                     // Checking for collision of the bats
  554.                     if (ball.GetDirection() > 1.5f * Math.PI || ball.GetDirection() < 0.5f * Math.PI)
  555.                     {
  556.                         if (rightBat.GetSize().Intersects(ball.GetSize()))
  557.                         {
  558.                             ball.BatHit(CheckHitLocation(rightBat));
  559.                         }
  560.                     }
  561.                     else if (leftBat.GetSize().Intersects(ball.GetSize()))
  562.                     {
  563.                         ball.BatHit(CheckHitLocation(leftBat));
  564.                     }
  565.                     #endregion
  566.  
  567.  
  568.                     #region Turbo stuff
  569.                     // If spaceBar is down and the turbo bar is not empty, activate turbo. If not, turbo remains off
  570.                     if (input.ActivateTurbo(ControllingPlayer))
  571.                     {
  572.                         if (disableCooldown > 0)
  573.                         {
  574.                             leftBat.isTurbo = true;
  575.                             coolDown = powerEnableCooldown;
  576.                             leftBat.moveSpeed = 30.0f;
  577.                             disableCooldown -= gameTime.ElapsedGameTime.Milliseconds;
  578.                         }
  579.                         else
  580.                         {  
  581.                             leftBat.DisableTurbo();
  582.                         }
  583.                     }
  584.  
  585.                         // If spacebar is not down, begin to refill the turbo bar
  586.                     else
  587.                     {
  588.                         leftBat.DisableTurbo();
  589.                         coolDown -= gameTime.ElapsedGameTime.Milliseconds;
  590.                         // If the coolDown timer is not in effect, then the bat can use Turbo again
  591.                         if (coolDown < 0)
  592.                         {
  593.                             disableCooldown = powerDisableCooldown;
  594.                         }
  595.                     }
  596.  
  597.                     // Makes sure that if Turbo is on, it is killd it after () seconds
  598.                     if (leftBat.isTurbo)
  599.                     {
  600.                         disableCooldown -= gameTime.ElapsedGameTime.Milliseconds;
  601.                     }
  602.                     #endregion
  603.  
  604.  
  605.                     #region What happens when the match is reset
  606.                     if (!resetTimerInUse)
  607.                     {
  608.                         // checks out of bounds for right side
  609.                         if (ball.GetPosition().X > screenWidth)
  610.                         {
  611.                             // Timer used to reset the turbo speed
  612.                             resetTimerInUse = true;
  613.                             lastScored = true;
  614.                             hud.SubtractHealthP2();
  615.                             // Checks to see if ball went out of bounds, and triggers warp sfx
  616.                             ball.OutOfBounds();
  617.                             // Adds points to the current score
  618.                             leftBat.IncrementPoints();
  619.                         }
  620.                             // Checks out of bounds for left side
  621.                         else if (ball.GetPosition().X < 0)
  622.                         {
  623.                             // Timer used to reset the turbo speed
  624.                             resetTimerInUse = true;
  625.                             lastScored = false;
  626.                             hud.SubtractHealthP1();
  627.                             // Checks to see if ball went out of bounds, and triggers warp sfx
  628.                             ball.OutOfBounds();
  629.                             // Adds points to the current score    
  630.                             rightBat.IncrementPoints();
  631.                         }
  632.                     }
  633.  
  634.                     if (resetTimerInUse)
  635.                     {
  636.                         resetTimer++;
  637.                         ball.Stop();
  638.                     }
  639.                     if (resetTimer == 120)
  640.                     {
  641.                         resetTimerInUse = false;
  642.                         ball.Reset(lastScored);
  643.                         resetTimer = 0;
  644.                     }
  645. #endregion
  646.                 }
  647.  
  648.                     #region If game is won or lost
  649.                 // MainMenu screen appears after 6 seconds, or if player hits "Enter" or "A"
  650.                 if (gamestate == GameStates.Lost)
  651.                 {
  652.                     screenLoadDelay -= gameTime.ElapsedGameTime.TotalSeconds;
  653.  
  654.                     if (GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed
  655.                         || Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Enter) == true)
  656.                     {
  657.                         ScreenManager.AddScreen(new MainMenuScreen(), null); // Draws the MainMenuScreen
  658.                        // ScreenManager.AddScreen(new GamePlayScreen(), PlayerIndex.One); // PIndex 1 works fine and advances
  659.                        // the GameplayScreen, but if it is null the game crashes. Odd? DV 6/2
  660.                     }
  661.                     else if (screenLoadDelay < 0)
  662.                     {
  663.                         ScreenManager.AddScreen(new MainMenuScreen(), null); // Draws the MainMenuScreen      
  664.                     }
  665.                 }
  666.  
  667.                 // New GameplayScreen appears after 6 seconds, or if player hits "Enter" or "A"
  668.                 if (gamestate == GameStates.Won)
  669.                 {
  670.                     screenLoadDelay -= gameTime.ElapsedGameTime.TotalSeconds;
  671.  
  672.                         if(GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed ||
  673.                             Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Enter) == true)
  674.                     {
  675.                         ScreenManager.AddScreen(new StageCompleteScreen(), null); // Draws the StageSelectScreen  
  676.                     }
  677.                     else if (screenLoadDelay < 0)
  678.                     {
  679.                         ScreenManager.AddScreen(new StageCompleteScreen(), null); // Draws the StageSelectScreen        
  680.                     }
  681.                 }
  682.                 #endregion
  683.  
  684.                 GamePadState state = GamePad.GetState(PlayerIndex.One);
  685.             }
  686.         }
  687.  
  688.  
  689.         //////////////////////////////////////////////// METHODS /////////////////////////////////////////////////////////////////////
  690.  
  691.         #region check bat hit loc
  692.         /// <summary>
  693.         /// Checking for bat collision & instructs the ball where to deflect to
  694.         /// </summary>
  695.         private int CheckHitLocation(Bat bat)
  696.         {
  697.             int block = 0;
  698.             if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 20)
  699.             {
  700.                 block = 1;
  701.             }
  702.             else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 2)
  703.             {
  704.                 block = 2;
  705.             }
  706.             else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 3)
  707.             {
  708.                 block = 3;
  709.             }
  710.             else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 4)
  711.             {
  712.                 block = 4;
  713.             }
  714.             else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 5)
  715.             {
  716.                 block = 5;
  717.             }
  718.             else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 6)
  719.             {
  720.                 block = 6;
  721.             }
  722.             else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 7)
  723.             {
  724.                 block = 7;
  725.             }
  726.             else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 8)
  727.             {
  728.                 block = 8;
  729.             }
  730.             else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 20 * 19)
  731.             {
  732.                 block = 9;
  733.             }
  734.             else
  735.             {
  736.                 block = 10;
  737.             }
  738.  
  739.             return block;
  740.         }
  741.         #endregion
  742.  
  743.  
  744.         #region logic to trigger powerups
  745.         /// <summary>
  746.         /// Logic to trigger Powerups
  747.         /// </summary>
  748.         private void PowerupActivated(object sender, PowerupEventArgs e)
  749.         {
  750.             // TODO: Receive the error "Object reference not set to an instance of an object." if I
  751.             // try to activate a powerup before it is rolled.
  752.             // Setting  "if  (timer <= 0.0f)" stops the crash, but then powerups can no longer activated after they are rolled.
  753.             if  (timer <= 0.0f)
  754.             {
  755.                 switch (e.Type)
  756.                 {
  757.                     case PowerupType.BigBalls:
  758.                         {
  759.                             ball.GrowBall();
  760.                             break;
  761.                         }
  762.  
  763.                     //case PowerupType.ShrinkBalls:
  764.                     //    {
  765.                     //        ball.ShrinkBall();
  766.                     //        break;
  767.                     //    }
  768.  
  769.                     //case PowerupType.BigPaddle:
  770.                     //    {
  771.                     //        leftBat.GrowBat();
  772.                     //        break;
  773.                     //    }
  774.  
  775.                     //case PowerupType.ShrinkEnemy:
  776.                     //    {
  777.                     //        rightBat.ShrinkBat();
  778.                     //        break;
  779.                     //    }
  780.  
  781.                     //case PowerupType.SpeedBall:
  782.                     //    {
  783.                     //        ball.PowerupSpeed();
  784.                     //        break;
  785.                     //    }
  786.  
  787.                     //case PowerupType.Heal:
  788.                     //    {
  789.                     //        hud.AddHealthP1();
  790.                     //        break;
  791.                     //    }
  792.                     case PowerupType.SlowMo:
  793.                         {
  794.                             this.SlowMo(gScaleActivated);
  795.                             gScaleActivated = true;
  796.                             break;
  797.                         }
  798.                 }
  799.             }
  800.         }
  801.  
  802.         /// <summary>
  803.         /// JEP - new logic to deactivate powerup
  804.         /// </summary>
  805.         private void PowerupDeactivated(object sender, PowerupEventArgs e)
  806.         {
  807.             // Starts the powerup timer again after a powerup has expired
  808.              powerupReady = false;
  809.  
  810.              if (input.RollPowerup(ControllingPlayer) && !playerOnePowerup.IsAlive && timer <= 0.0f)
  811.             {
  812.                 switch (e.Type)
  813.                 {
  814.                     case PowerupType.BigBalls:
  815.                         {
  816.                             ball.NormalBallSize();
  817.                             break;
  818.                         }
  819.  
  820.                     //case PowerupType.ShrinkBalls:
  821.                     //    {
  822.                     //        ball.NormalBallSize();
  823.                     //        break;
  824.                     //    }
  825.  
  826.                     //case PowerupType.BigPaddle:
  827.                     //    {
  828.                     //        leftBat.NormalSize();
  829.                     //        break;
  830.                     //    }
  831.  
  832.                     //case PowerupType.ShrinkEnemy:
  833.                     //    {
  834.                     //        rightBat.NormalSize();
  835.                     //        break;
  836.                     //    }
  837.                     case PowerupType.SlowMo:
  838.                         {
  839.                             ball.ReturnToNormalSpeed();
  840.                             gScaleActivated = false;
  841.                             leftBat.moveSpeed = 7f;
  842.                             rightBat.moveSpeed = 7f;
  843.                              break;
  844.                         }
  845.  
  846.                     //case PowerupType.SpeedBall:
  847.                     //    {
  848.                     //        ball.NormalSpeed();
  849.                     //        break;
  850.                     //    }
  851.                 }
  852.             }
  853.         }
  854.         #endregion
  855.  
  856.         // Activates the SlowMo and grayscale effect for the powerup.
  857.         public void SlowMo(bool gScaleActivated)
  858.         {
  859.                         gScaleActivated = true;
  860.                         ball.SlowMoBall();
  861.                         leftBat.moveSpeed = 30.0f;
  862.                         rightBat.moveSpeed = 30.0f;
  863.                         AudioManager.Instance.PlaySoundEffect("SlowMotion1");
  864.         }
  865.  
  866.         /// <summary>
  867.         /// Lets the game respond to player input. Unlike the Update method,
  868.         /// this will only be called when the gameplay screen is active.
  869.         /// </summary>
  870.         public override void HandleInput(Input input)
  871.         {
  872.             if (input == null)
  873.             {
  874.                 throw new ArgumentNullException("input");
  875.             }
  876.  
  877.             // Look up inputs for the active player profile.
  878.             var playerIndex = (int)ControllingPlayer.Value;
  879.  
  880.             KeyboardState keyboardState = input.CurrentKeyboardState[playerIndex];
  881.             GamePadState gamePadState = input.CurrentGamePadState[playerIndex];
  882.  
  883.             // The game pauses either if the user presses the pause button, or if
  884.             // they unplug the active gamepad. This requires us to keep track of
  885.             // whether a gamepad was ever plugged in, because we don't want to pause
  886.             // on PC if they are playing with a keyboard and have no gamepad at all!
  887.             bool gamePadDisconnected = !gamePadState.IsConnected && input.GamePadWasConnected[playerIndex];
  888.  
  889.             if (input.IsPauseGame(ControllingPlayer) || gamePadDisconnected)
  890.             {
  891.                 ScreenManager.AddScreen(new PauseMenuScreen(), ControllingPlayer);
  892.             }
  893.         }
  894.  
  895.         /// <summary>
  896.         /// Resets all values and game data for a new game
  897.         /// </summary>
  898.         private void Reset()
  899.         {
  900.             hud.ResetHealth();
  901.             ball.Reset(true); // parameter passed could be random left or right
  902.             leftBat.ResetPosition();
  903.             leftBat.NormalSize();
  904.             rightBat.ResetPosition();
  905.             rightBat.NormalSize();
  906.         }
  907.  
  908.         /// <summary>
  909.         /// Sets up a 2 player game, specifically he bats. Currently not in use, as I've removed multiplayer functionality. 5/24
  910.         /// </summary>
  911.         private void SetUpMulti()
  912.         {
  913.             rightBat = new AIBat(contentManager, new Vector2(ScreenManager.Game.GraphicsDevice.Viewport.TitleSafeArea.Width,
  914.                  ScreenManager.Game.GraphicsDevice.Viewport.TitleSafeArea.Height), false);
  915.             leftBat = new Bat(contentManager, new Vector2(ScreenManager.Game.GraphicsDevice.Viewport.TitleSafeArea.Width,
  916.                  ScreenManager.Game.GraphicsDevice.Viewport.TitleSafeArea.Height), true);
  917.         }
  918.  
  919.         /// <summary>
  920.         /// Sets up a single player game, specifically the bats
  921.         /// </summary>
  922.         private void SetUpSingle()
  923.         {
  924.             rightBat = new AIBat(contentManager, new Vector2(ScreenManager.Game.GraphicsDevice.Viewport.TitleSafeArea.Width,
  925.                  ScreenManager.Game.GraphicsDevice.Viewport.TitleSafeArea.Height), false);
  926.             leftBat = new Bat(contentManager, new Vector2(ScreenManager.Game.GraphicsDevice.Viewport.TitleSafeArea.Width,
  927.                 ScreenManager.Game.GraphicsDevice.Viewport.TitleSafeArea.Height), true);
  928.         }
  929.  
  930.         #endregion
  931.     }
  932. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement