Example #1
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (gameState == GameState.TitleScreen)
            {
                previousState = GameState.TitleScreen;
                startScreen.Update(gameTime, 1);

                if (startScreen.SinglePlayer || startScreen.MultiPlayer)
                {
                    if (fadeScreen.Opacity <= 255)
                    {
                        if (fadeScreen.Opacity != 255)
                        {
                            MediaPlayer.Volume -= .07f;
                            fadeScreen.Fade(gameTime, 15);
                        }
                        else
                        {
                            if (startScreen.SinglePlayer)
                            {
                                gameState = GameState.CharacterSelection;
                                characterScreen = new CharacterSelectionScreen(graphics, Content);
                            }
                            else if (startScreen.MultiPlayer)
                            {
                                gameState = GameState.MultiplayerLobby;
                                multiplayerLobby = new Lobby(graphics, Content, startScreen.ControlScheme);
                            }
                            MediaPlayer.Volume = 1f;
                        }
                    }
                    else
                    {
                        if (fadeScreen != null)
                        {
                            if (fadeScreen.Opacity > 0)
                            {
                                fadeScreen.Fade(gameTime, -1);
                            }
                        }
                    }
                }
            }
            else if (gameState == GameState.CharacterSelection)
            {
                previousState = GameState.CharacterSelection;
                characterScreen.Update(gameTime);

                if (characterScreen.IsDone)
                {
                    if (fadeScreen.Opacity <= 255)
                    {
                        if (fadeScreen.Opacity != 255)
                        {
                            fadeScreen.Fade(gameTime, 15);
                            MediaPlayer.Volume -= .06f;
                        }
                        else
                        {
                            LoadCharacter();
                            gameState = GameState.StartLevel; // .Credits; //
                            characterScreen = null;
                            startScreen = null;
                        }
                    }
                }
                else
                {
                    if (fadeScreen.Opacity > 0)
                    {
                        fadeScreen.Fade(gameTime, -5);
                    }
                }
            }
            else if (gameState == GameState.MultiplayerLobby)
            {
                previousState = GameState.MultiplayerLobby;
                multiplayerLobby.Update(gameTime);

                if (multiplayerLobby.IsDone)
                {
                    if (fadeScreen.Opacity <= 255)
                    {
                        if (fadeScreen.Opacity != 255)
                        {
                            fadeScreen.Fade(gameTime, 15);
                            MediaPlayer.Volume -= .06f;
                        }
                        else
                        {
                            LoadCharacter();
                            gameState = GameState.StartLevel;
                            characterScreen = null;
                            startScreen = null;
                        }
                    }
                }
                else
                {
                    if (fadeScreen.Opacity > 0)
                    {
                        fadeScreen.Fade(gameTime, -5);
                    }
                }
            }
            else if (gameState == GameState.StartLevel)
            {
                previousState = GameState.StartLevel;
                if (fadeScreen.Opacity > 0)
                {
                    MediaPlayer.Volume += .07f;
                    fadeScreen.Fade(gameTime, -5);
                }
                if (fadeScreen.Opacity == 0)
                {
                    MediaPlayer.Volume = 1f;
                }

                float scrollSpeed = gameTime.ElapsedGameTime.Milliseconds / 6f;
                currentLevel.background.Update(gameTime, scrollSpeed);

                levelBeginning.Update(gameTime);
                if (levelIncrement == 1)
                    currentLevel.border.Update(gameTime, scrollSpeed);

                playerOneSprite.Update(gameTime);
                if (playerTwoSprite != null)
                {
                    playerTwoSprite.Update(gameTime);
                }

                if (levelBeginning.IsDone)
                {
                    gameState = GameState.PlayingLevel;
                }
            }
            else if (gameState == GameState.PlayingLevel)
            {
                previousState = GameState.PlayingLevel;

                if (dinosaurOne._currentHealth > 0)
                {
                    if (dinosaurOne.ControlScheme == ChosenControl.Keyboard)
                    {
                        input.Update(Keyboard.GetState(), playerOneSprite, dinosaurOne, playerShots, graphics, gameTime, gameState);
                    }
                    else if (dinosaurOne.ControlScheme == ChosenControl.Gamepad)
                    {
                        padInput.Update(GamePad.GetState(PlayerIndex.One, GamePadDeadZone.None), playerOneSprite, dinosaurOne, playerShots, graphics, gameTime, gameState);
                    }
                }

                if (dinosaurTwo != null)
                {
                    if (dinosaurTwo._currentHealth > 0)
                    {
                        if (dinosaurTwo.ControlScheme == ChosenControl.Keyboard)
                        {
                            input.Update(Keyboard.GetState(), playerTwoSprite, dinosaurTwo, playerShots, graphics, gameTime, gameState);
                        }
                        else if (dinosaurTwo.ControlScheme == ChosenControl.Gamepad)
                        {
                            if (dinosaurOne.ControlScheme != ChosenControl.Gamepad)
                            {
                                padInput.Update(GamePad.GetState(PlayerIndex.One, GamePadDeadZone.None), playerTwoSprite, dinosaurTwo, playerShots, graphics, gameTime, gameState);
                            }
                            else
                            {
                                padInput.Update(GamePad.GetState(PlayerIndex.Two, GamePadDeadZone.None), playerTwoSprite, dinosaurTwo, playerShots, graphics, gameTime, gameState);
                            }
                        }
                    }
                }

                hbOne.Update(dinosaurOne.Health);
                cbOne.Update(dinosaurOne.LaserCharge);
                if (playerTwoSprite != null)
                {
                    hbTwo.Update(dinosaurTwo.Health);
                    cbTwo.Update(dinosaurTwo.LaserCharge);
                }

                int counter = 0;
                foreach (Sprite s in enemies)
                {
                    currentLevel.healthBars[counter++].Update(s.Health, s.Position);

                    if (s.enemyType.type == Enemies.EnemyType.Etype.Level1Boss ||
                        s.enemyType.type == Enemies.EnemyType.Etype.Level2Boss ||
                        s.enemyType.type == Enemies.EnemyType.Etype.Level3Boss)
                    {
                        bossLifeBar.IsVisible = true;
                        bossLifeBar.Update(s.Health);
                    }
                    else
                    {
                        bossLifeBar.IsVisible = false;
                    }
                }

                enemyShots.Update(gameTime, graphics, playerOneSprite, playerTwoSprite);
                playerShots.Update(gameTime, graphics);

                gameState = currentLevel.Update(this, gameTime, spriteBatch, graphics, this.enemies);

                foreach (Sprite enemy in enemies)
                {
                    if (enemy.GetType() == (typeof(AnimatedSprite)))
                    {
                        (enemy as AnimatedSprite).Update(gameTime);
                    }
                    else
                    {
                        enemy.Update(gameTime, graphics, playerOneSprite, playerTwoSprite, dinosaurOne, dinosaurTwo);
                    }

                    if ((enemy.Movement.X < 0 && enemy.Position.X <= 100) || (enemy.Movement.X > 0 && enemy.Position.X >= graphics.PreferredBackBufferWidth - 100))
                    {
                        enemy.Movement = new Vector2(enemy.Movement.X * -1, enemy.Movement.Y);
                    }

                    if ((enemy.Movement.Y < 0 && enemy.Position.Y <= 50) || (enemy.Movement.Y > 0 && enemy.Position.Y >= graphics.PreferredBackBufferHeight / 1.5))
                    {
                        enemy.Movement = new Vector2(enemy.Movement.X, enemy.Movement.Y * -1);
                    }

                    shotBehavior.Update(graphics, enemy, enemyShots);
                }

                AnimatedSprite removeMe = null;
                foreach (AnimatedSprite ex in explosions)
                {
                    ex.Update(gameTime);

                    if (ex.frames >= ex.frameTotal)
                    {
                        removeMe = ex;
                    }
                }
                explosions.Remove(removeMe);

                foreach (Powerup powerUp in collisions.powerups)
                {
                    if (powerUp != null)
                    {
                        powerUp.Update(gameTime);
                    }
                }

                collisions.Update(this, Content, spriteBatch, font, dinosaurOne, enemies, enemyShots, playerShots, currentLevel.randomObjects, currentLevel.healthBars, playerOneSprite, graphics, dinosaurTwo, playerTwoSprite);

                if (dinosaurOne._currentCharge < dinosaurOne.MaxLaserCharge)
                    playerOneCounter += 1;
                if (playerOneCounter >= 9)
                {
                    dinosaurOne._currentCharge++;
                    playerOneCounter = 0;
                }

                if (dinosaurTwo != null)
                {
                    if (dinosaurTwo._currentCharge < dinosaurTwo.MaxLaserCharge)
                        playerTwoCounter += 1;
                    if (playerTwoCounter >= 9)
                    {
                        dinosaurTwo._currentCharge++;
                        playerTwoCounter = 0;
                    }
                }

                if (singlePlayer)
                {
                    if (dinosaurOne._currentHealth <= 0)
                    {
                        hbOne.Update(dinosaurOne.Health);

                        gameState = GameState.Death;
                    }
                }
                else
                {
                    if (dinosaurOne._currentHealth <= 0 && dinosaurTwo._currentHealth <= 0)
                    {
                        hbOne.Update(dinosaurOne.Health);
                        hbTwo.Update(dinosaurTwo.Health);

                        gameState = GameState.Death;
                    }
                    else if (dinosaurOne._currentHealth <= 0 || dinosaurTwo._currentHealth <= 0)
                    {
                        if (dinosaurOne._currentHealth <= 0)
                        {
                            hbOne.Update(0);
                            if (playerOneSprite.Position.Y < graphics.PreferredBackBufferHeight + 200)
                            {
                                if (!explodedOne)
                                {
                                    explodedOne = true;
                                    collisions.explosionSound.Play();
                                }
                                AnimatedSprite explode = new AnimatedSprite(12, 0, 0, 134, 134, this, collisions.explodeImage,
                                playerOneSprite.Position,
                                spriteBatch, new Enemies.EnemyType(Content, Enemies.EnemyType.Etype.Simple));
                                explode.Name = "Explode";
                                explosions.Add(explode);

                                Vector2 movement = Vector2.Zero;
                                movement += Vector2.UnitY * .05f;
                                playerOneSprite.Movement += movement;

                                playerOneSprite.Update(gameTime);
                                movement.X = 0f;
                                movement.Y = 0f;
                                playerOneSprite.Movement = movement;
                                playerOneSprite.Update(gameTime);
                            }
                        }
                        else if (dinosaurTwo._currentHealth <= 0)
                        {
                            hbTwo.Update(0);
                            if (playerTwoSprite.Position.Y < graphics.PreferredBackBufferHeight + 200)
                            {
                                if (!explodedTwo)
                                {
                                    explodedTwo = true;
                                    collisions.explosionSound.Play();
                                }
                                AnimatedSprite explode = new AnimatedSprite(12, 0, 0, 134, 134, this, collisions.explodeImage,
                                playerTwoSprite.Position,
                                spriteBatch, new Enemies.EnemyType(Content, Enemies.EnemyType.Etype.Simple));
                                explode.Name = "Explode";
                                explosions.Add(explode);

                                Vector2 movement = Vector2.Zero;
                                movement += Vector2.UnitY * .05f;
                                playerTwoSprite.Movement += movement;

                                playerTwoSprite.Update(gameTime);
                                movement.X = 0f;
                                movement.Y = 0f;
                                playerTwoSprite.Movement = movement;
                                playerTwoSprite.Update(gameTime);
                            }
                        }
                    }
                }

            }

            else if (gameState == GameState.Death)
            {
                previousState = GameState.Death;

                if (playerTwoSprite != null)
                {
                    AnimatedSprite explode = new AnimatedSprite(12, 0, 0, 134, 134, this, collisions.explodeImage,
                                playerTwoSprite.Position,
                                spriteBatch, new Enemies.EnemyType(Content, Enemies.EnemyType.Etype.Simple));
                    explode.Name = "Explode";

                    if (!explodedOne)
                    {
                        collisions.explosionSound.Play();
                        explodedOne = true;
                    }
                    explosions.Add(explode);

                    AnimatedSprite explodeOne = new AnimatedSprite(12, 0, 0, 134, 134, this, collisions.explodeImage,
                                playerOneSprite.Position,
                                spriteBatch, new Enemies.EnemyType(Content, Enemies.EnemyType.Etype.Simple));
                    explode.Name = "Explode";
                    if (!explodedTwo)
                    {
                        collisions.explosionSound.Play();
                        explodedTwo = true;
                    }
                    explosions.Add(explodeOne);
                }
                else
                {
                    AnimatedSprite explode = new AnimatedSprite(12, 0, 0, 134, 134, this, collisions.explodeImage,
                                playerOneSprite.Position,
                                spriteBatch, new Enemies.EnemyType(Content, Enemies.EnemyType.Etype.Simple));
                    explode.Name = "Explode";
                    if (!explodedOne)
                    {
                        collisions.explosionSound.Play();
                        explodedOne = true;
                    }
                    explosions.Add(explode);
                }

                AnimatedSprite removeMe = null;
                foreach (AnimatedSprite ex in explosions)
                {
                    ex.Update(gameTime);

                    if (ex.frames >= ex.frameTotal)
                    {
                        removeMe = ex;
                    }
                }
                explosions.Remove(removeMe);

                if (fadeScreen.Opacity <= 180)
                {
                    fadeScreen.Fade(gameTime, 1);
                }

                deathScreen.Update(gameTime);

                enemyShots.Update(gameTime, graphics, playerOneSprite, playerTwoSprite);
                playerShots.Update(gameTime, graphics);
                foreach (Powerup powerUp in collisions.powerups)
                {
                    if (powerUp != null)
                    {
                        powerUp.Update(gameTime);
                    }
                }
                collisions.Update(this, Content, spriteBatch, font, dinosaurOne, enemies, enemyShots, playerShots, currentLevel.randomObjects, currentLevel.healthBars, playerOneSprite, graphics, dinosaurTwo, playerTwoSprite);

                if (deathScreen.TitleSelected)
                {
                    if (fadeScreen.Opacity < 255)
                    {
                        fadeScreen.Fade(gameTime, 5);
                        MediaPlayer.Volume -= .06f;
                    }
                    else if (fadeScreen.Opacity >= 255)
                    {
                        UnloadContent();
                        LoadContent();
                    }
                }
            }
            else if (gameState == GameState.EndOfLevel)
            {
                previousState = GameState.EndOfLevel;

                enemyShots.list.Clear();

                playerOneSprite.Update(gameTime);
                if (playerTwoSprite != null)
                {
                    playerTwoSprite.Update(gameTime);
                }

                currentLevel.Update(this, gameTime, spriteBatch, graphics, enemies);
                //enemyShots.Update(gameTime, graphics, playerOneSprite, playerTwoSprite);
                playerShots.Update(gameTime, graphics);
                upgradeScreen.Update(gameTime);

                foreach (Powerup powerUp in collisions.powerups)
                {
                    if (powerUp != null)
                    {
                        powerUp.Update(gameTime);
                    }
                }

                AnimatedSprite removeMe = null;
                foreach (AnimatedSprite ex in explosions)
                {
                    ex.Update(gameTime);

                    if (ex.frames >= ex.frameTotal)
                    {
                        removeMe = ex;
                    }
                }
                explosions.Remove(removeMe);

                collisions.Update(this, Content, spriteBatch, font, dinosaurOne, enemies, enemyShots, playerShots, currentLevel.randomObjects, currentLevel.healthBars, playerOneSprite, graphics, dinosaurTwo, playerTwoSprite);

                if (upgradeScreen.IsComplete)
                {
                    if (fadeScreen.Opacity <= 255)
                    {
                        if (fadeScreen.Opacity != 255)
                        {
                            fadeScreen.Fade(gameTime, 15);
                        }
                        else
                        {
                            if (playerTwoSprite == null)
                            {
                                playerOneSprite.Position = new Vector2(graphics.PreferredBackBufferWidth / 2, graphics.PreferredBackBufferHeight - 250);
                                dinosaurOne.Replenish();
                            }
                            else
                            {
                                playerOneSprite.Position = new Vector2(graphics.PreferredBackBufferWidth / 3, graphics.PreferredBackBufferHeight - 250);
                                playerTwoSprite.Position = new Vector2(graphics.PreferredBackBufferWidth - (graphics.PreferredBackBufferWidth / 3), graphics.PreferredBackBufferHeight - 250);
                                dinosaurOne.Replenish();
                                hbOne.Update(dinosaurOne.Health);
                                dinosaurTwo.Replenish();
                                hbTwo.Update(dinosaurTwo.Health);
                            }

                            if (currentLevel.levelSelected != Level.LevelSelected.Level5)
                            {
                                gameState = GameState.LoadingScreen;
                            }
                        }
                    }
                }
            }
            else if (gameState == GameState.LoadingScreen)
            {
                previousState = GameState.LoadingScreen;
                loadingScreen.Update(gameTime);

                if (loadingScreen.IsDone)
                {
                    if (fadeScreen.Opacity <= 255)
                    {
                        if (fadeScreen.Opacity != 255)
                        {
                            fadeScreen.Fade(gameTime, 5);
                            MediaPlayer.Volume -= .07f;
                        }
                        else
                        {
                            gameState = GameState.StartLevel;
                            CreateScreens();
                            GenerateLevel();
                            MediaPlayer.Volume = 1f;
                        }
                    }
                }
                else
                {
                    if (fadeScreen.Opacity > 0)
                    {
                        fadeScreen.Fade(gameTime, -5);
                    }
                }
            }
            else if (gameState == GameState.Pause)
            {
                pauseScreen.Update(gameTime);
                MediaPlayer.Volume = .25f;
                if (pauseScreen.QuitSelected)
                {
                    this.Exit();
                }
                else if (pauseScreen.ResumeSelected)
                {
                    pauseScreen.ResumeSelected = false;
                    gameState = previousState;
                    MediaPlayer.Volume = 1f;
                }
                else if (pauseScreen.TitleSelected)
                {
                    if (fadeScreen.Opacity <= 255)
                    {
                        if (fadeScreen.Opacity != 255)
                        {
                            fadeScreen.Fade(gameTime, 15);
                            MediaPlayer.Volume -= .06f;
                        }
                        else
                        {
                            UnloadContent();
                            LoadContent();
                        }
                    }
                }
            }
            else if (gameState == GameState.Credits)
            {
                previousState = GameState.Credits;

                creditScreen.Update(gameTime);

                if (creditScreen.TextDone && !creditScreen.BeginBackgroundCreditScroll)
                {
                    if (fadeScreen.Opacity < 255)
                    {
                        fadeScreen.Fade(gameTime, 5);
                    }
                    else if (fadeScreen.Opacity == 255)
                    {
                        creditScreen.BeginBackgroundCreditScroll = true;
                        levelBeginning.LevelCount = 1;
                        GenerateLevel();
                    }
                }
                else if (creditScreen.BeginBackgroundCreditScroll)
                {
                    if (fadeScreen.Opacity > 0)
                    {
                        fadeScreen.Fade(gameTime, -5);
                    }
                    currentLevel.background.Update(gameTime, 2);
                    currentLevel.border.Update(gameTime, 3);
                }
                else
                {
                    if (fadeScreen.Opacity > 0)
                    {
                        fadeScreen.Fade(gameTime, -5);
                    }
                }
            }

            // Allows the game to exit
            // Gamepad option to exit
            //if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            //{
            //    this.Exit();
            //}

            // allows keyboard exit on esc keypress
            if (Keyboard.GetState().IsKeyDown(Keys.Escape) || GamePad.GetState(PlayerIndex.One, GamePadDeadZone.None).IsButtonDown(Buttons.Start))
            {
            gameState = GameState.Pause;
            }
            base.Update(gameTime);
        }
Example #2
0
        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            gameState = GameState.TitleScreen;
            previousState = gameState;

            upgradeScreen = null;
            startScreen = null;
            characterScreen = null;
            multiplayerLobby = null;
            deathScreen = null;
            levelBeginning = null;
            pauseScreen = null;
            loadingScreen = null;

            levelCreator = null;

            Random rand = new Random();
            currentLevel = null;

            enemies = null;
            explosions = null;
            shotBehavior = null;
            enemyShots = null;
            playerShots = null;
            hbOne = null;
            hbTwo = null;
            bossLifeBar = null;
            cbOne = null;
            cbTwo = null;
            backGroundMusic = null;
            playerOneMeterTitle = null;
            playerTwoMeterTitle = null;
            meterLabel = null;
            playerOneMarker = null;
            playerTwoMarker = null;

            dinosaurOne = null;
            dinosaurTwo = null;
            playerOneSprite = null;
            playerTwoSprite = null;
            collisions = null;
            font = null;

            playerOneCounter = 0;
            playerTwoCounter = 0;
            levelIncrement = 0;

            singlePlayer = false;
            explodedOne = false;
            explodedTwo = false;

            input = null;
            padInput = null;
        }