Guest User

Untitled

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