DaveVoyles

SlowMo 2

Jun 23rd, 2012
12
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. namespace Pong
  2. {
  3.     using Microsoft.Xna.Framework;
  4.     using Microsoft.Xna.Framework.Content;
  5.     using Microsoft.Xna.Framework.Graphics;
  6.     using System;
  7.  
  8.     public class Bat
  9.     {
  10.         public Vector2 Position;
  11.         public float moveSpeed;
  12.         public Rectangle size;
  13.         private int points;
  14.         private int yHeight;
  15.         private Texture2D leftBat;
  16.         public float turbo;
  17.         public float recharge;
  18.         public float interval;
  19.         public bool isTurbo;
  20.  
  21.         // SlowMo stuff
  22.         private float Elapsed = 0f;
  23.         public float MoveTimer = 0f;
  24.         public bool Slowmo = false;
  25.        
  26.         /// <summary>
  27.         /// Constructor for the bat
  28.         /// </summary>
  29.         public Bat(ContentManager contentManager, Vector2 screenSize, bool side)
  30.         {
  31.             moveSpeed = 7f;
  32.             turbo = 15f;
  33.             recharge = 100f;
  34.             points = 0;
  35.             interval = 5f;
  36.             leftBat = contentManager.Load<Texture2D>(@"gfx/bats/batGrey");
  37.             size = new Rectangle(0, 0, leftBat.Width, leftBat.Height);
  38.  
  39.             // True means left bat, false means right bat.
  40.             if (side) Position = new Vector2(30, screenSize.Y / 2 - size.Height / 2);
  41.             else Position = new Vector2(screenSize.X - 30, screenSize.Y / 2 - size.Height / 2);
  42.             yHeight = (int)screenSize.Y;
  43.         }
  44.  
  45.         public void IncreaseSpeed()
  46.         {
  47.             moveSpeed += .5f;      
  48.         }
  49.  
  50.         /// <summary>
  51.         /// The speed of the bat when Turbo is activated
  52.         /// </summary>
  53.         public void Turbo()
  54.         {
  55.             moveSpeed += 8.0f;
  56.         }
  57.  
  58.         /// <summary>
  59.         /// Returns the speed of the bat back to normal after Turbo is deactivated
  60.         /// </summary>
  61.         public void DisableTurbo()
  62.         {
  63.             moveSpeed = 7.0f;
  64.             isTurbo = false;
  65.         }
  66.  
  67.         /// <summary>
  68.         /// Returns the bat to the nrmal size after the Grow/Shrink powerup has expired
  69.         /// </summary>
  70.         public void NormalSize()
  71.         {
  72.             size = new Rectangle(0, 0, leftBat.Width, leftBat.Height);
  73.         }
  74.  
  75.         /// <summary>
  76.         /// Checks for the size of the bat
  77.         /// </summary>
  78.         public Rectangle GetSize()
  79.         {
  80.             return size;
  81.         }
  82.  
  83.         /// <summary>
  84.         /// Adds point to the player or the AI after scoring. Currently Disabled.
  85.         /// </summary>
  86.         public void IncrementPoints()
  87.         {
  88.             points++;
  89.         }
  90.  
  91.         /// <summary>
  92.         /// Checks for the number of points at the moment
  93.         /// </summary>
  94.         public int GetPoints()
  95.         {
  96.             return points;
  97.         }
  98.  
  99.         /// <summary>
  100.         /// Sets thedefault starting position for the bats
  101.         /// </summary>
  102.         /// <param name="position"></param>
  103.         public void SetPosition(Vector2 position)
  104.         {
  105.             if (position.Y < 0)
  106.             {
  107.                 position.Y = 0;
  108.             }
  109.             if (position.Y > yHeight - size.Height)
  110.             {
  111.                 position.Y = yHeight - size.Height;
  112.             }
  113.             this.Position = position;
  114.         }
  115.  
  116.  
  117.         /// <summary>
  118.         /// Checks for the current position of the bat
  119.         /// </summary>
  120.         public Vector2 GetPosition()
  121.         {
  122.             return Position;
  123.         }
  124.  
  125.         /// <summary>
  126.         /// Controls the bat moving up the screen
  127.         /// </summary>
  128.         public void MoveUp()
  129.         {
  130.             SetPosition(Position + new Vector2(0, -moveSpeed * MoveTimer));
  131.         }
  132.  
  133.         /// <summary>
  134.         /// Controls the bat moving down the screen
  135.         /// </summary>
  136.         public void MoveDown()
  137.         {
  138.             SetPosition(Position + new Vector2(0, moveSpeed * MoveTimer));
  139.         }
  140.  
  141.         /// <summary>
  142.         /// Updates the position of the AI bat, in order to track the ball
  143.         /// </summary>
  144.         /// <param name="ball"></param>
  145.         public virtual void UpdatePosition(Ball ball)
  146.         {
  147.             size.X = (int)Position.X;
  148.             size.Y = (int)Position.Y;
  149.         }
  150.  
  151.         /// <summary>
  152.         /// Resets the bat to the center location after a new game starts
  153.         /// </summary>
  154.         public void ResetPosition()
  155.         {
  156.             SetPosition(new Vector2(GetPosition().X, yHeight / 2 - size.Height));
  157.         }
  158.  
  159.        /// <summary>
  160.        /// Used for the Growbat powerup
  161.        /// </summary>
  162.         public void GrowBat()
  163.         {
  164.             // Doubles the size of the bat collision
  165.             size = new Rectangle(0, 0, leftBat.Width * 2, leftBat.Height * 2);      
  166.         }
  167.  
  168.         /// <summary>
  169.         /// Used for the Shrinkbat powerup
  170.         /// </summary>
  171.         public void ShrinkBat()
  172.         {
  173.             // 1/2 the size of the bat collision
  174.             size = new Rectangle(0, 0, leftBat.Width / 2, leftBat.Height / 2);
  175.         }
  176.  
  177.         /// <summary>
  178.         /// Draws the bats
  179.         /// </summary>
  180.         public virtual void Draw(SpriteBatch batch)
  181.         {
  182.             batch.Draw(leftBat, size, Color.White);
  183.         }
  184.  
  185.    
  186.     }
  187. }
  188.  
  189.  
  190.  
  191.  
  192.  
  193. *******************************************
  194.  
  195.  
  196.  /// <summary>
  197.     /// This screen implements the actual game logic. It is just a
  198.     /// placeholder to get the idea across: you'll probably want to
  199.     /// put some more interesting gameplay in here!
  200.     /// </summary>
  201.     public class GameplayScreen : GameScreen
  202.     {
  203.  
  204.         private  float Elapsed = 0f;
  205.         public float MoveTimer = 0f;
  206.         public bool Slowmo = false;
  207.  
  208.       /// <summary>
  209.         /// Lets the game respond to player input. Unlike the Update method,
  210.         /// this will only be called when the gameplay screen is active.
  211.         /// </summary>
  212.         public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
  213.         {
  214.             base.Update(gameTime, otherScreenHasFocus, false);
  215.  
  216.             // Gradually fade in or out depending on whether we are covered by the pause screen.
  217.             if (coveredByOtherScreen)
  218.             {
  219.                 pauseAlpha = Math.Min(pauseAlpha + 1f / 32, 1);
  220.                 MediaPlayer.Pause(); // Pause gameplay music
  221.             }
  222.             else
  223.             {
  224.                 pauseAlpha = Math.Max(pauseAlpha - 1f / 32, 0);
  225.                 MediaPlayer.Resume(); // Resum gameplay music
  226.             }
  227.  
  228.  
  229.  ////////////////////// If the game is *not* paused, then update the following: ///////////////////////////
  230.             //frameTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
  231.             //if (slowTime > 0f)
  232.             //{
  233.             //    slowTime -= frameTime;
  234.             //    frameTime /= 10f;
  235.             //}
  236.  
  237.             if (IsActive)
  238.             {
  239.                 // SlowMo Stuff
  240.                 Elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
  241.                 if (Slowmo) Elapsed *= .8f;
  242.  
  243.                 MoveTimer += Elapsed;
  244.  
  245.                 GamePadState state = GamePad.GetState(PlayerIndex.One);
  246.                            // Updates the input
  247.                     input.Update();
  248.  
  249.                     #region Movement of bat update
  250.                     // Controls movement of the bats              
  251.                     if (rightBat.GetType() != typeof(AIBat))
  252.                     {
  253.                         if (input.IsLeftPaddleMovingDown(ControllingPlayer))
  254.                         {
  255.                             leftBat.MoveDown();
  256.                         }
  257.                         else if (input.IsLeftPaddleMovingUp(ControllingPlayer))
  258.                         {
  259.                             leftBat.MoveUp();
  260.                         }
  261.  
  262.                         if (input.IsRightPaddleMovingDown(ControllingPlayer))
  263.                         {
  264.                             rightBat.MoveDown();
  265.                         }
  266.                         else if (input.IsRightPaddleMovingUp(ControllingPlayer))
  267.                         {
  268.                             rightBat.MoveUp();
  269.                         }
  270.                     }
  271.                     else if (rightBat.GetType() == typeof(AIBat))
  272.                     {
  273.                         if (input.IsLeftPaddleMovingDown(ControllingPlayer))
  274.                         {
  275.                             leftBat.MoveDown();
  276.                         }
  277.                         else if (input.IsLeftPaddleMovingUp(ControllingPlayer))
  278.                         {
  279.                             leftBat.MoveUp();
  280.                         }
  281.  
  282.                         if (input.IsRightPaddleMovingDown(ControllingPlayer))
  283.                         {
  284.                             leftBat.MoveDown();
  285.                         }
  286.                         else if (input.IsRightPaddleMovingUp(ControllingPlayer))
  287.                         {
  288.                             leftBat.MoveUp();
  289.                         }
  290.                     }
  291.                     #endregion
  292.  
  293.                     // Updating ball and bat position
  294.                     leftBat.UpdatePosition(ball);
  295.                     rightBat.UpdatePosition(ball);
  296.                     ball.UpdatePosition();
  297.  
  298.                     // Checking for collision of the bats
  299.                     if (ball.GetDirection() > 1.5f * Math.PI || ball.GetDirection() < 0.5f * Math.PI)
  300.                     {
  301.                         if (rightBat.GetSize().Intersects(ball.GetSize()))
  302.                         {
  303.                             ball.BatHit(CheckHitLocation(rightBat));
  304.                         }
  305.                     }
  306.                     else if (leftBat.GetSize().Intersects(ball.GetSize()))
  307.                     {
  308.                         ball.BatHit(CheckHitLocation(leftBat));
  309.                         }
  310.                 }
  311. }
RAW Paste Data