Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // --------------------------------------------------------------------------------------------------------------------
- // <summary>
- // This screen implements the actual game logic. It is just a
- // placeholder to get the idea across: you'll probably want to
- // put some more interesting gameplay in here!
- // </summary>
- // --------------------------------------------------------------------------------------------------------------------
- namespace Pong
- {
- using System;
- using System.Collections.Generic;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Audio;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Media;
- /// <summary>
- /// This screen implements the actual game logic. It is just a
- /// placeholder to get the idea across: you'll probably want to
- /// put some more interesting gameplay in here!
- /// </summary>
- public class GameplayScreen : GameScreen
- {
- #region Fields
- // For shaders
- Effect gscaleEffect;
- float percent;
- KeyboardState current, previous;
- public bool gScaleActivated;
- public float ElapsedTime;
- public float percentscale;
- // Graphics stuff
- private Vector2 activatedVec;
- private SpriteFont arial;
- private Texture2D backgroundTexture;
- private ContentManager contentManager;
- private int coolDown;
- private Vector2 deactivatedVec;
- private int disableCooldown;
- private SpriteFont font;
- private HUD hud;
- private Menu menu;
- private Input input;
- private float pauseAlpha;
- private Animation powerupAnimate;
- // All things having to do with the powerup
- private Powerup playerOnePowerup;
- private Powerup playerTwoPowerup;
- private int powerDisableCooldown = 2000;
- private int powerEnableCooldown = 5000;
- private bool powerupReady;
- private Vector2 promptVec;
- private Vector2 leftBatVec;
- private Vector2 rightBatVec;
- private Vector2 ballSpeedVec;
- private Random random;
- private int resetTimer;
- private bool resetTimerInUse;
- private SpriteBatch spriteBatch;
- private double timer; // timer for the powerup countdown
- private SpriteFont timerFont;
- private Vector2 timerVector;
- private Vector2 vec;
- private Vector2 vec2;
- private double powerupResetDelay;
- // The timer for when the menu will appear after a game resets
- private double screenLoadDelay;
- public static GameplayScreen Instance;
- public static GameStates gamestate;
- public int screenHeight;
- public int screenWidth;
- public ParticleEngine particleEngine;
- public Ball ball;
- public Bat leftBat;
- public AIBat rightBat;
- public bool side;
- public bool lastScored;
- // public Song GamePlaySong { get; private set; }
- public PlayerIndex? controllingp { get; set; }
- public enum GameStates
- {
- Menu,
- Running,
- Paused,
- End,
- Won,
- Lost
- }
- #endregion
- #region Methods
- /// <summary>
- /// Constructor.
- /// </summary>
- public GameplayScreen()
- {
- TransitionOnTime = TimeSpan.FromSeconds(1.5);
- TransitionOffTime = TimeSpan.FromSeconds(0.5);
- }
- protected void Initialize()
- {
- // Start the score off at 0
- lastScored = false;
- resetTimer = 0;
- menu = new Menu(); // Draws text on the screen after the game to say who won
- resetTimerInUse = true;
- SetUpSingle(); // Sets up a single player game
- ball = new Ball(contentManager,
- new Vector2 (ScreenManager.Game.GraphicsDevice.Viewport.TitleSafeArea.Width,
- ScreenManager.Game.GraphicsDevice.Viewport.TitleSafeArea.Height),
- leftBat, rightBat);
- input = new Input();
- hud = new HUD(this);
- gamestate = GameStates.Running;
- // The timer for when the menu will appear after a game resets.
- // Screen will auto appear after 8 seconds
- screenLoadDelay = 8.0;
- powerupResetDelay = 6.0;
- // Places the powerup animation inside of the surrounding box
- // TODO Needs to be cleaned up, instead of using hard pixel values
- powerupAnimate = new Animation(contentManager.Load<Texture2D>(@"gfx/powerupSpriteSheet"),
- new Vector2(103, 44), 64, 64, 4, 5);
- // Rolls a random powerup
- random = new Random();
- vec = new Vector2(100, 50);
- vec2 = new Vector2(100, 100);
- promptVec = new Vector2(50, 25);
- leftBatVec = new Vector2(100, 200);
- rightBatVec = new Vector2(100, 250);
- ballSpeedVec = new Vector2(100, 300);
- // Starting value for the cooldown for the powerup timer
- timer = 3000.0f;
- // Where the timer appears on the screen
- timerVector = new Vector2(10, 10);
- // JEP - one time creation of powerup objects
- playerOnePowerup = new Powerup();
- playerOnePowerup.Activated += PowerupActivated;
- playerOnePowerup.Deactivated += PowerupDeactivated;
- playerTwoPowerup = new Powerup();
- playerTwoPowerup.Activated += PowerupActivated;
- playerTwoPowerup.Deactivated += PowerupDeactivated;
- // Location where activated / deactivated powerup notifications will appear
- activatedVec = new Vector2(100, 125);
- deactivatedVec = new Vector2(100, 150);
- powerupReady = false;
- // JEP 5/17 - draw code uses screenwidth/height
- screenWidth = ScreenManager.Game.GraphicsDevice.Viewport.TitleSafeArea.Width;
- screenHeight = ScreenManager.Game.GraphicsDevice.Viewport.TitleSafeArea.Height;
- // Sets the default percent scale for the black and white SlowMo effect
- percentscale = 0.01f;
- gScaleActivated = false;
- }
- /// <summary>
- /// Load graphics content for the game.
- /// </summary>
- public override void LoadContent()
- {
- if (contentManager == null)
- {
- contentManager = new ContentManager(ScreenManager.Game.Services, "Content");
- }
- this.Initialize();
- arial = contentManager.Load<SpriteFont>(@"gfx/fonts/Arial"); // for game scores
- spriteBatch = new SpriteBatch(ScreenManager.GraphicsDevice);
- backgroundTexture = contentManager.Load<Texture2D>(@"gfx/Backgrounds/StageBackgroundScreen1");
- hud.LoadContent(contentManager);
- AudioManager.Instance.PlaySoundTrack(); // Plays the music from AudioManager class
- font = contentManager.Load<SpriteFont>(@"gfx/fonts/Arial"); // Used by the Powerup
- timerFont = contentManager.Load<SpriteFont>(@"gfx/fonts/Arial"); // Used or the countdown time for
- ScreenManager.Game.ResetElapsedTime();
- // Everything to do with particles
- List<Texture2D> textures = new List<Texture2D>();
- textures.Add(contentManager.Load<Texture2D>(@"gfx/particle/circle"));
- textures.Add(contentManager.Load<Texture2D>(@"gfx/particle/star"));
- textures.Add(contentManager.Load<Texture2D>(@"gfx/particle/diamond"));
- particleEngine = new ParticleEngine(textures, new Vector2());
- // For black and white shader. Starts off by default.
- gscaleEffect = contentManager.Load<Effect>(@"gfx/effects/hit");
- //percent = 1;
- }
- //////////////////////////////////// DRAW ///////////////////////////////////////////////////////
- /// <summary>
- /// Draws the gameplay screen.
- /// </summary>
- public override void Draw(GameTime gameTime)
- {
- ScreenManager.GraphicsDevice.Clear(ClearOptions.Target, Color.CornflowerBlue, 0, 0);
- SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
- if (gamestate == GameStates.Running)
- {
- // For shader: If % is <= 0 then draw all objects normally.
- if (gScaleActivated == false)
- {
- spriteBatch.Begin();
- // Draws background
- spriteBatch.Draw(backgroundTexture, new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
- //// Slow mo stuff from Michael Harts
- //spriteBatch.Draw(rect, new Rectangle((int)position.X, (int)position.Y, 300, 300), Color.Red);
- // Draws Animation for Powerup
- powerupAnimate.Draw(spriteBatch);
- //Draws the HUD
- hud.Draw(gameTime, spriteBatch);
- // Draws the bats and ball
- leftBat.Draw(spriteBatch);
- rightBat.Draw(spriteBatch);
- // Draw the ball
- ball.Draw(spriteBatch);
- // Draws the particle engine
- particleEngine.Draw(spriteBatch);
- // Draws powerup timer countdown
- spriteBatch.DrawString(timerFont, ((int)timer / 1000).ToString(), timerVector, Color.White);
- #region powerup display (text for debugging)
- // Text that appears on the screen to notify the player of powerups. Currently there for debug.
- if (gamestate == GameStates.Running)
- {
- spriteBatch.DrawString(font, "Press A or Left Alt button to generate powerup", promptVec, Color.White);
- spriteBatch.DrawString(font, "LeftBat Speed = " + leftBat.moveSpeed, leftBatVec, Color.White);
- spriteBatch.DrawString(font, "RightBat Speed = " + rightBat.moveSpeed, rightBatVec, Color.White);
- spriteBatch.DrawString(font, "Ball Speed = " + ball.speed, ballSpeedVec, Color.White);
- if (powerupReady)
- {
- spriteBatch.DrawString(font, "Powerup Type: " + playerOnePowerup.Type, vec, Color.White);
- spriteBatch.DrawString(
- font, "Press Left Bumper or Left Ctrl to activate powerup", vec2, Color.White);
- }
- // JEP - changed check to use powerup status
- if (!playerOnePowerup.IsAlive)
- {
- spriteBatch.DrawString(font, "Powerup deactivated", deactivatedVec, Color.White);
- }
- if (playerOnePowerup.IsAlive)
- {
- spriteBatch.DrawString(font, "Powerup activated", activatedVec, Color.White);
- }
- }
- #endregion
- // Draws the GameScreen class, and all associated text during the game
- base.Draw(gameTime);
- spriteBatch.End();
- }
- else // Otherwise activate the grayscale effect
- {
- // The actual grayscale effect
- spriteBatch.Begin(0, null, null, null, null, gscaleEffect);
- // Draws background
- spriteBatch.Draw(backgroundTexture, new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
- //Draws the HUD
- hud.Draw(gameTime, spriteBatch);
- // Draws bats
- leftBat.Draw(spriteBatch);
- rightBat.Draw(spriteBatch);
- // Draw the ball
- ball.Draw(spriteBatch);
- // Draws the particle engine
- particleEngine.Draw(spriteBatch);
- // Draws the GameScreen class, and all associated text during the game
- // Draws powerup timer countdown
- spriteBatch.DrawString(timerFont, ((int)timer / 1000).ToString(), timerVector, Color.White);
- #region powerup display (text for debugging)
- // Text that appears on the screen to notify the player of powerups. Currently there for debug.
- if (gamestate == GameStates.Running)
- {
- spriteBatch.DrawString(font, "Press A or Left Alt button to generate powerup", promptVec, Color.White);
- spriteBatch.DrawString(font, "LeftBat Speed = " + leftBat.moveSpeed, leftBatVec, Color.White);
- spriteBatch.DrawString(font, "RightBat Speed = " + rightBat.moveSpeed, rightBatVec, Color.White);
- spriteBatch.DrawString(font, "Ball Speed = " + ball.speed, ballSpeedVec, Color.White);
- if (powerupReady)
- {
- spriteBatch.DrawString(font, "Powerup Type: " + playerOnePowerup.Type, vec, Color.White);
- spriteBatch.DrawString(
- font, "Press Left Bumper or Left Ctrl to activate powerup", vec2, Color.White);
- }
- // JEP - changed check to use powerup status
- if (!playerOnePowerup.IsAlive)
- {
- spriteBatch.DrawString(font, "Powerup deactivated", deactivatedVec, Color.White);
- }
- if (playerOnePowerup.IsAlive)
- {
- spriteBatch.DrawString(font, "Powerup activated", activatedVec, Color.White);
- }
- }
- #endregion
- base.Draw(gameTime);
- spriteBatch.End();
- }
- }
- else if (gamestate == GameStates.End || gamestate == GameStates.Won || gamestate == GameStates.Lost)
- {
- spriteBatch.Begin();
- spriteBatch.Draw(backgroundTexture, new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
- menu.DrawEndScreen(spriteBatch, screenWidth, arial);
- spriteBatch.End();
- }
- // If the game is transitioning on or off, fade it out to black.
- if (TransitionPosition > 0 || pauseAlpha > 0)
- {
- float alpha = MathHelper.Lerp(1f - TransitionAlpha, 1f, pauseAlpha / 2);
- ScreenManager.FadeBackBufferToBlack(alpha);
- }
- }
- //////////////////////////////////////////// UPDATE ////////////////////////////////////////
- /// <summary>
- /// Lets the game respond to player input. Unlike the Update method,
- /// this will only be called when the gameplay screen is active.
- /// </summary>
- public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
- {
- base.Update(gameTime, otherScreenHasFocus, false);
- // Gradually fade in or out depending on whether we are covered by the pause screen.
- if (coveredByOtherScreen)
- {
- pauseAlpha = Math.Min(pauseAlpha + 1f / 32, 1);
- MediaPlayer.Pause(); // Pause gameplay music
- }
- else
- {
- pauseAlpha = Math.Max(pauseAlpha - 1f / 32, 0);
- MediaPlayer.Resume(); // Resum gameplay music
- }
- //////////////////// If the game is *not* paused, then update the following: /////////////////////
- if (IsActive)
- {
- #region Movement of bat update
- // Controls movement of the bats
- if (rightBat.GetType() != typeof(AIBat))
- {
- if (input.IsLeftPaddleMovingDown(ControllingPlayer))
- {
- leftBat.MoveDown();
- }
- else if (input.IsLeftPaddleMovingUp(ControllingPlayer))
- {
- leftBat.MoveUp();
- }
- if (input.IsRightPaddleMovingDown(ControllingPlayer))
- {
- rightBat.MoveDown();
- }
- else if (input.IsRightPaddleMovingUp(ControllingPlayer))
- {
- rightBat.MoveUp();
- }
- }
- else if (rightBat.GetType() == typeof(AIBat))
- {
- if (input.IsLeftPaddleMovingDown(ControllingPlayer))
- {
- leftBat.MoveDown();
- }
- else if (input.IsLeftPaddleMovingUp(ControllingPlayer))
- {
- leftBat.MoveUp();
- }
- if (input.IsRightPaddleMovingDown(ControllingPlayer))
- {
- leftBat.MoveDown();
- }
- else if (input.IsRightPaddleMovingUp(ControllingPlayer))
- {
- leftBat.MoveUp();
- }
- }
- #endregion
- #region Powerup timer
- // updates timer for powerups so that it counts down
- if (gamestate == GameStates.Running)
- {
- // begins to count down for the powerup
- timer -= gameTime.ElapsedGameTime.TotalMilliseconds;
- if (this.powerupReady)
- {
- powerupResetDelay -= gameTime.ElapsedGameTime.TotalSeconds;
- if (powerupResetDelay < 0)
- {
- powerupResetDelay = 6;
- // if a powerup has just expired then....
- timer = 10000.0f; // start the timer at 10 seconds
- }
- }
- }
- #endregion
- #region Powerup Update
- // JEP 5/3 - don't allow a new powerup to be rolled until the current one is done
- if (input.RollPowerup(ControllingPlayer) && !playerOnePowerup.IsAlive && timer <= 0.0f)
- {
- // generate a random powerup
- var type = (PowerupType)random.Next(Enum.GetNames(typeof(PowerupType)).Length);
- // Animation for the powerups
- powerupAnimate.Update(gameTime);
- switch (type)
- {
- case PowerupType.BigBalls:
- {
- // JEP - called new method instead of recreating
- playerOnePowerup.Reset(type, 5.0f, 1.0f, 0);
- break;
- }
- //case PowerupType.ShrinkBalls:
- // {
- // // JEP - called new method instead of recreating
- // playerOnePowerup.Reset(type, 5.0f, 1.0f, 0);
- // break;
- // }
- //case PowerupType.BigPaddle:
- // {
- // // JEP - called new method instead of recreating
- // playerOnePowerup.Reset(type, 5.0f, 1.0f, 0);
- // break;
- // }
- //case PowerupType.ShrinkEnemy:
- // {
- // // JEP - called new method instead of recreating
- // playerOnePowerup.Reset(type, 5.0f, 1.0f, 0);
- // break;
- // }
- //case PowerupType.SpeedBall:
- // {
- // // JEP - called new method instead of recreating
- // playerOnePowerup.Reset(type, 5.0f, 1.0f, 0);
- // break;
- // }
- //case PowerupType.Heal:
- // {
- // // JEP - called new method instead of recreating
- // playerOnePowerup.Reset(type, 1.0f, 1.0f, 0);
- // break;
- // }
- case PowerupType.SlowMo:
- {
- playerOnePowerup.Reset(type, 1.0f, 1.0f, 0);
- break;
- }
- }
- // Without this set to true, the powerups stay on forever and you can continue to keep
- // activating them over and over without a timer
- powerupReady = true;
- }
- else if (input.ActivatePowerup(ControllingPlayer) & !playerOnePowerup.IsAlive)
- {
- // JEP 5/3 - added check to make sure powerup isn't still running
- playerOnePowerup.Activate();
- }
- // JEP - removed initialization of powerup and added check for IsAlive instead
- if (playerOnePowerup.IsAlive)
- {
- playerOnePowerup.Update((float)gameTime.ElapsedGameTime.TotalMilliseconds);
- }
- if (playerTwoPowerup.IsAlive)
- {
- playerTwoPowerup.Update((float)gameTime.ElapsedGameTime.TotalMilliseconds);
- }
- #endregion
- #region what to do when the game is over
- // What to do when the game is over
- if (gamestate == GameStates.Running)
- {
- // if (hud.currentHealthP2 < 1)
- if (hud.currentHealthP2 < 90) // currently in to speed up debugging
- {
- menu.InfoText = "Game, blouses." + Environment.NewLine + "Prepare for the next game!";
- gamestate = GameStates.Won;
- // Reset(); // Resets the ball, bats, and all attributes between matches
- }
- // else if (hud.currentHealthP1 < 1)
- else if (hud.currentHealthP1 < 90) // currently in to speed up debugging
- {
- menu.InfoText = "You just let the AI beat you.";
- gamestate = GameStates.Lost;
- // Reset(); // Resets the ball, bats, and all attributes between matches
- }
- #endregion
- // Updating the input
- input.Update();
- // Updating bat position
- leftBat.UpdatePosition(ball, gameTime);
- rightBat.UpdatePosition(ball, gameTime);
- // Updating the ball position
- ball.UpdatePosition(gameTime);
- // Updating the Particle Engine
- particleEngine.EmitterLocation = new Vector2(Mouse.GetState().X, Mouse.GetState().Y);
- particleEngine.Update();
- #region checks for collision of bats
- // Checking for collision of the bats
- if (ball.GetDirection() > 1.5f * Math.PI || ball.GetDirection() < 0.5f * Math.PI)
- {
- if (rightBat.GetSize().Intersects(ball.GetSize()))
- {
- ball.BatHit(CheckHitLocation(rightBat));
- }
- }
- else if (leftBat.GetSize().Intersects(ball.GetSize()))
- {
- ball.BatHit(CheckHitLocation(leftBat));
- }
- #endregion
- #region Turbo stuff
- // If spaceBar is down and the turbo bar is not empty, activate turbo. If not, turbo remains off
- if (input.ActivateTurbo(ControllingPlayer))
- {
- if (disableCooldown > 0)
- {
- leftBat.isTurbo = true;
- coolDown = powerEnableCooldown;
- leftBat.moveSpeed = 30.0f;
- disableCooldown -= gameTime.ElapsedGameTime.Milliseconds;
- }
- else
- {
- leftBat.DisableTurbo();
- }
- }
- // If spacebar is not down, begin to refill the turbo bar
- else
- {
- leftBat.DisableTurbo();
- coolDown -= gameTime.ElapsedGameTime.Milliseconds;
- // If the coolDown timer is not in effect, then the bat can use Turbo again
- if (coolDown < 0)
- {
- disableCooldown = powerDisableCooldown;
- }
- }
- // Makes sure that if Turbo is on, it is killd it after () seconds
- if (leftBat.isTurbo)
- {
- disableCooldown -= gameTime.ElapsedGameTime.Milliseconds;
- }
- #endregion
- #region What happens when the match is reset
- if (!resetTimerInUse)
- {
- // checks out of bounds for right side
- if (ball.GetPosition().X > screenWidth)
- {
- // Timer used to reset the turbo speed
- resetTimerInUse = true;
- lastScored = true;
- hud.SubtractHealthP2();
- // Checks to see if ball went out of bounds, and triggers warp sfx
- ball.OutOfBounds();
- // Adds points to the current score
- leftBat.IncrementPoints();
- }
- // Checks out of bounds for left side
- else if (ball.GetPosition().X < 0)
- {
- // Timer used to reset the turbo speed
- resetTimerInUse = true;
- lastScored = false;
- hud.SubtractHealthP1();
- // Checks to see if ball went out of bounds, and triggers warp sfx
- ball.OutOfBounds();
- // Adds points to the current score
- rightBat.IncrementPoints();
- }
- }
- if (resetTimerInUse)
- {
- resetTimer++;
- ball.Stop();
- }
- if (resetTimer == 120)
- {
- resetTimerInUse = false;
- ball.Reset(lastScored);
- resetTimer = 0;
- }
- #endregion
- }
- #region If game is won or lost
- // MainMenu screen appears after 6 seconds, or if player hits "Enter" or "A"
- if (gamestate == GameStates.Lost)
- {
- screenLoadDelay -= gameTime.ElapsedGameTime.TotalSeconds;
- if (GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed
- || Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Enter) == true)
- {
- ScreenManager.AddScreen(new MainMenuScreen(), null); // Draws the MainMenuScreen
- // ScreenManager.AddScreen(new GamePlayScreen(), PlayerIndex.One); // PIndex 1 works fine and advances
- // the GameplayScreen, but if it is null the game crashes. Odd? DV 6/2
- }
- else if (screenLoadDelay < 0)
- {
- ScreenManager.AddScreen(new MainMenuScreen(), null); // Draws the MainMenuScreen
- }
- }
- // New GameplayScreen appears after 6 seconds, or if player hits "Enter" or "A"
- if (gamestate == GameStates.Won)
- {
- screenLoadDelay -= gameTime.ElapsedGameTime.TotalSeconds;
- if(GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed ||
- Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Enter) == true)
- {
- ScreenManager.AddScreen(new StageCompleteScreen(), null); // Draws the StageSelectScreen
- }
- else if (screenLoadDelay < 0)
- {
- ScreenManager.AddScreen(new StageCompleteScreen(), null); // Draws the StageSelectScreen
- }
- }
- #endregion
- GamePadState state = GamePad.GetState(PlayerIndex.One);
- }
- }
- //////////////////////////////////////////////// METHODS /////////////////////////////////////////////////////////////////////
- #region check bat hit loc
- /// <summary>
- /// Checking for bat collision & instructs the ball where to deflect to
- /// </summary>
- private int CheckHitLocation(Bat bat)
- {
- int block = 0;
- if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 20)
- {
- block = 1;
- }
- else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 2)
- {
- block = 2;
- }
- else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 3)
- {
- block = 3;
- }
- else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 4)
- {
- block = 4;
- }
- else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 5)
- {
- block = 5;
- }
- else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 6)
- {
- block = 6;
- }
- else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 7)
- {
- block = 7;
- }
- else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 10 * 8)
- {
- block = 8;
- }
- else if (ball.GetPosition().Y < bat.GetPosition().Y + bat.GetSize().Height / 20 * 19)
- {
- block = 9;
- }
- else
- {
- block = 10;
- }
- return block;
- }
- #endregion
- #region logic to trigger powerups
- /// <summary>
- /// Logic to trigger Powerups
- /// </summary>
- private void PowerupActivated(object sender, PowerupEventArgs e)
- {
- // TODO: Receive the error "Object reference not set to an instance of an object." if I
- // try to activate a powerup before it is rolled.
- // Setting "if (timer <= 0.0f)" stops the crash, but then powerups can no longer activated after they are rolled.
- if (timer <= 0.0f)
- {
- switch (e.Type)
- {
- case PowerupType.BigBalls:
- {
- ball.GrowBall();
- break;
- }
- //case PowerupType.ShrinkBalls:
- // {
- // ball.ShrinkBall();
- // break;
- // }
- //case PowerupType.BigPaddle:
- // {
- // leftBat.GrowBat();
- // break;
- // }
- //case PowerupType.ShrinkEnemy:
- // {
- // rightBat.ShrinkBat();
- // break;
- // }
- //case PowerupType.SpeedBall:
- // {
- // ball.PowerupSpeed();
- // break;
- // }
- //case PowerupType.Heal:
- // {
- // hud.AddHealthP1();
- // break;
- // }
- case PowerupType.SlowMo:
- {
- this.SlowMo(gScaleActivated);
- gScaleActivated = true;
- break;
- }
- }
- }
- }
- /// <summary>
- /// JEP - new logic to deactivate powerup
- /// </summary>
- private void PowerupDeactivated(object sender, PowerupEventArgs e)
- {
- // Starts the powerup timer again after a powerup has expired
- powerupReady = false;
- if (input.RollPowerup(ControllingPlayer) && !playerOnePowerup.IsAlive && timer <= 0.0f)
- {
- switch (e.Type)
- {
- case PowerupType.BigBalls:
- {
- ball.NormalBallSize();
- break;
- }
- //case PowerupType.ShrinkBalls:
- // {
- // ball.NormalBallSize();
- // break;
- // }
- //case PowerupType.BigPaddle:
- // {
- // leftBat.NormalSize();
- // break;
- // }
- //case PowerupType.ShrinkEnemy:
- // {
- // rightBat.NormalSize();
- // break;
- // }
- case PowerupType.SlowMo:
- {
- ball.ReturnToNormalSpeed();
- gScaleActivated = false;
- leftBat.moveSpeed = 7f;
- rightBat.moveSpeed = 7f;
- break;
- }
- //case PowerupType.SpeedBall:
- // {
- // ball.NormalSpeed();
- // break;
- // }
- }
- }
- }
- #endregion
- // Activates the SlowMo and grayscale effect for the powerup.
- public void SlowMo(bool gScaleActivated)
- {
- gScaleActivated = true;
- ball.SlowMoBall();
- leftBat.moveSpeed = 30.0f;
- rightBat.moveSpeed = 30.0f;
- AudioManager.Instance.PlaySoundEffect("SlowMotion1");
- }
- /// <summary>
- /// Lets the game respond to player input. Unlike the Update method,
- /// this will only be called when the gameplay screen is active.
- /// </summary>
- public override void HandleInput(Input input)
- {
- if (input == null)
- {
- throw new ArgumentNullException("input");
- }
- // Look up inputs for the active player profile.
- var playerIndex = (int)ControllingPlayer.Value;
- KeyboardState keyboardState = input.CurrentKeyboardState[playerIndex];
- GamePadState gamePadState = input.CurrentGamePadState[playerIndex];
- // The game pauses either if the user presses the pause button, or if
- // they unplug the active gamepad. This requires us to keep track of
- // whether a gamepad was ever plugged in, because we don't want to pause
- // on PC if they are playing with a keyboard and have no gamepad at all!
- bool gamePadDisconnected = !gamePadState.IsConnected && input.GamePadWasConnected[playerIndex];
- if (input.IsPauseGame(ControllingPlayer) || gamePadDisconnected)
- {
- ScreenManager.AddScreen(new PauseMenuScreen(), ControllingPlayer);
- }
- }
- /// <summary>
- /// Resets all values and game data for a new game
- /// </summary>
- private void Reset()
- {
- hud.ResetHealth();
- ball.Reset(true); // parameter passed could be random left or right
- leftBat.ResetPosition();
- leftBat.NormalSize();
- rightBat.ResetPosition();
- rightBat.NormalSize();
- }
- /// <summary>
- /// Sets up a 2 player game, specifically he bats. Currently not in use, as I've removed multiplayer functionality. 5/24
- /// </summary>
- private void SetUpMulti()
- {
- rightBat = new AIBat(contentManager, new Vector2(ScreenManager.Game.GraphicsDevice.Viewport.TitleSafeArea.Width,
- ScreenManager.Game.GraphicsDevice.Viewport.TitleSafeArea.Height), false);
- leftBat = new Bat(contentManager, new Vector2(ScreenManager.Game.GraphicsDevice.Viewport.TitleSafeArea.Width,
- ScreenManager.Game.GraphicsDevice.Viewport.TitleSafeArea.Height), true);
- }
- /// <summary>
- /// Sets up a single player game, specifically the bats
- /// </summary>
- private void SetUpSingle()
- {
- rightBat = new AIBat(contentManager, new Vector2(ScreenManager.Game.GraphicsDevice.Viewport.TitleSafeArea.Width,
- ScreenManager.Game.GraphicsDevice.Viewport.TitleSafeArea.Height), false);
- leftBat = new Bat(contentManager, new Vector2(ScreenManager.Game.GraphicsDevice.Viewport.TitleSafeArea.Width,
- ScreenManager.Game.GraphicsDevice.Viewport.TitleSafeArea.Height), true);
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement