Guest User

Untitled

a guest
Aug 7th, 2012
44
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     /// <summary>
  2.     /// This screen implements the actual game logic.
  3.     /// </summary>
  4.     public class GameplayScreen : GameScreen
  5.     {
  6.  
  7.     /// <summary>
  8.     /// This screen implements the actual game logic.
  9.     /// </summary>
  10.     public class GameplayScreen : GameScreen
  11.     {
  12.         #region Fields
  13.  
  14.  
  15.         Renderer myRenderer;                        // Renderer that draws particles to screen
  16.         ParticleEffect myEffect;                    // Particle effect object to store the info about particle
  17.     //  GraphicsDevice graphicsDevice;
  18.     //  GraphicsDeviceManager graphics;
  19.  
  20.         private int _selectedStage;                 // Stage select
  21.  
  22.         private PowerupControl _powerUpControl;
  23.         private KeyboardState _oldState;
  24.  
  25.         //// Turbo Stuff
  26.         public float disableTurboCooldown;
  27.         public int twoSecondBurst = 2000;           // Two Seconds
  28.         public int fiveSecondCoolDown = 5000;       // Five seconds
  29.         public int coolDown;                        // Cooldown Factor
  30.  
  31.         public int coolDownSpeed = 10;
  32.  
  33.         DebuggingTools debuggingTools;              // Debugging tools screen
  34.  
  35.         // For shaders
  36.         Effect gscaleEffect;
  37.         KeyboardState current, previous;
  38.         public bool gScaleActivated;
  39.         public float ElapsedTime, percentscale, percent;
  40.         private bool resetTimerInUse;
  41.         private int resetTimer;
  42.  
  43.         // Graphics stuff
  44.         private ContentManager contentManager;
  45.         private SpriteFont arial;
  46.         private HUD hud;
  47.         private Menu menu;
  48.         private Input input;
  49.         private float pauseAlpha;
  50.         private Random random;
  51.         private SpriteBatch spriteBatch;
  52.         private Texture2D backgroundTexture;
  53.  
  54.         private double screenLoadDelay;                 // The timer for when the menu will appear after a game resets
  55.  
  56.        // private static Game1 game;
  57.         private Game game;
  58.         public static GameplayScreen Instance;
  59.         public static GameStates gamestate;
  60.         public int screenHeight, screenWidth;
  61.         public Ball ball;
  62.         public Bat leftBat;
  63.         public AIBat rightBat;
  64.         public bool side, lastScored;
  65.  
  66.  
  67.         /// <summary>
  68.         /// Constructor.
  69.         /// </summary>
  70.         public GameplayScreen(Game game, int selectedStage):base(game)
  71.         {
  72.             TransitionOnTime = TimeSpan.FromSeconds(1.5);
  73.             TransitionOffTime = TimeSpan.FromSeconds(0.5);
  74.             this._selectedStage = selectedStage;
  75.  
  76.         }
  77.  
  78.         protected void Initialize(int selectedStage)
  79.         {
  80.             // Start the score off at 0
  81.             lastScored = false;
  82.             menu = new Menu(); // Draws text on the screen after the game to say who won
  83.             resetTimerInUse = true;
  84.             resetTimer = 0;
  85.             SetUpSingle(); // Sets up a single player game
  86.             ball = new Ball(contentManager,
  87.                             new Vector2(ScreenManager.Game.GraphicsDevice.Viewport.TitleSafeArea.Width,
  88.                             ScreenManager.Game.GraphicsDevice.Viewport.TitleSafeArea.Height),
  89.                             leftBat, rightBat);
  90.  
  91.  
  92.             ParticleEmitter.AddEmitter(EmitterType.BlueParticle, ball.Position, ball);
  93.  
  94.             input = new Input();
  95.             hud = new HUD(this);
  96.             debuggingTools = new DebuggingTools(this,Game);
  97.             gamestate = GameStates.Running;
  98.             // Initializes the previously chosen stage
  99.             this._selectedStage = selectedStage;
  100.  
  101.             // JEP 5/17 - draw code uses screenwidth/height
  102.             screenWidth = ScreenManager.Game.GraphicsDevice.Viewport.TitleSafeArea.Width;
  103.             screenHeight = ScreenManager.Game.GraphicsDevice.Viewport.TitleSafeArea.Height;
  104.  
  105.             // Sets the default percent scale for the black and white SlowMo effect
  106.             percentscale = 0.01f;
  107.             gScaleActivated = false;
  108.  
  109.             // Create new renderer and set its graphics devide to "this" device
  110.             myRenderer = new SpriteBatchRenderer
  111.             {
  112.                 GraphicsDeviceService = Game1.graphics
  113.             };
  114.  
  115.             myEffect = new ParticleEffect();
  116.            
  117.         }
  118.  
  119.         /// <summary>
  120.         /// Load graphics content for the game.
  121.         /// </summary>
  122.         public override void LoadContent()
  123.         {
  124.             if (contentManager == null)
  125.             {
  126.                 contentManager = new ContentManager(ScreenManager.Game.Services, "Content");
  127.             }
  128.             this.Initialize(_selectedStage);
  129.             arial = contentManager.Load<SpriteFont>(@"gfx/fonts/Arial"); // for game scores
  130.  
  131.             if (_selectedStage == 0)
  132.             {
  133.                 //Load the 1st stage selected  
  134.                 backgroundTexture = contentManager.Load<Texture2D>(@"gfx/Backgrounds/StageBackgroundScreen1");
  135.                 AudioManager.Instance.PlayStage1Music(); // Plays the music from AudioManager class
  136.             }
  137.             if (_selectedStage == 1)
  138.             {
  139.                 backgroundTexture = contentManager.Load<Texture2D>(@"gfx/Backgrounds/StageBackgroundScreen2");
  140.                 AudioManager.Instance.PlayStage2Music(); // Plays the music from AudioManager class
  141.             }
  142.             if (_selectedStage == 2)
  143.             {
  144.                 backgroundTexture = contentManager.Load<Texture2D>(@"gfx/Backgrounds/StageBackgroundScreen3");
  145.                 AudioManager.Instance.PlayStage3Music(); // Plays the music from AudioManager class
  146.             }
  147.  
  148.             hud.LoadContent(contentManager);
  149.             ScreenManager.Game.ResetElapsedTime();
  150.  
  151.             // For black and white shader. Starts off by default.
  152.             gscaleEffect = contentManager.Load<Effect>(@"gfx/effects/hit");
  153.             //percent = 1;
  154.             debuggingTools.LoadContent(contentManager);
  155.  
  156.             spriteBatch = new SpriteBatch(ScreenManager.Game.GraphicsDevice);
  157.             // Content for Mercury
  158.             myEffect = contentManager.Load<ParticleEffect>("BasicExplosion");
  159.             myEffect.LoadContent(contentManager);
  160.             myEffect.Initialise();
  161.             myRenderer.LoadContent(contentManager);
  162.         }
  163.  
  164.         //////////////////////////////////// DRAW ///////////////////////////////////////////////////////
  165.         /// <summary>
  166.         /// Draws the gameplay screen.
  167.         /// </summary>
  168.         public override void Draw(GameTime gameTime)
  169.         {
  170.             ScreenManager.GraphicsDevice.Clear(ClearOptions.Target, Color.CornflowerBlue, 0, 0);
  171.             SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  172.  
  173.             if (gamestate == GameStates.Running)
  174.             {
  175.  
  176.                 spriteBatch.Begin();
  177.                 // Draws background
  178.                 spriteBatch.Draw(backgroundTexture, new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
  179.                 //// Slow mo stuff from Michael Harts
  180.                 //spriteBatch.Draw(rect, new Rectangle((int)position.X, (int)position.Y, 300, 300), Color.Red);
  181.                 //Draws the HUD
  182.                 hud.Draw(gameTime, spriteBatch);
  183.                 // Draws the bats and ball
  184.                 leftBat.Draw(spriteBatch);
  185.                 rightBat.Draw(spriteBatch);
  186.                 // Draw the ball
  187.                 ball.Draw(spriteBatch);
  188.                 // Draws the debugging tools
  189.                 debuggingTools.Draw(spriteBatch);
  190.                 // Draws Mercury
  191.                 myRenderer.RenderEffect(myEffect);
  192.                 base.Draw(gameTime);
  193.                 spriteBatch.End();
  194.             }
  195.  
  196.  
  197.  
  198.         //////////////////////////////////////////// UPDATE ////////////////////////////////////////
  199.         /// <summary>
  200.         /// Lets the game respond to player input. Unlike the Update method,
  201.         /// this will only be called when the gameplay screen is active.
  202.         /// </summary>
  203.         public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
  204.         {
  205.             base.Update(gameTime, otherScreenHasFocus, false);
  206.  
  207.             // Draws the debugging tools on sreen
  208.  
  209.             // get the latest mouse state
  210.             MouseState ms = Mouse.GetState();
  211.             if (ms.LeftButton == ButtonState.Pressed)
  212.             {      
  213.                 myEffect.Trigger(new Vector2(500,500));
  214.             }
  215.             // "Deltatime" ie, time since last update call
  216.             float SecondsPassed = (float)gameTime.ElapsedGameTime.TotalSeconds;
  217.             myEffect.Update(SecondsPassed);
  218.  
  219.             // Draws the debugging tols
  220.             debuggingTools.Update(gameTime);
  221.  
  222.  
  223.  
  224.             //////////////////// If the game is *not* paused, then update the following: /////////////////////
  225.  
  226.             if (IsActive)
  227.             {
  228.  
  229.                 KeyboardState thisstate = Keyboard.GetState();
  230.                 _oldState = thisstate;
  231.                 if (thisstate.IsKeyDown(Keys.Space) && _oldState.IsKeyUp(Keys.Space))
  232.                     _powerUpControl.AddPower(10);
  233.  
  234.  
  235.  
  236.  
  237.                 // Updating the input
  238.                 input.Update();
  239.                 // Updating bat position
  240.                 leftBat.UpdatePosition(ball, gameTime);
  241.                 rightBat.UpdatePosition(ball, gameTime);
  242.  
  243.                 // Updating the ball position
  244.                 ball.UpdatePosition(gameTime);
  245.  
  246.  
  247.             }
  248.         }
  249.  
  250.  
  251.  
  252.     }
  253. }
RAW Paste Data