Exemple #1
0
        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.BlueViolet);
            spriteBatch.Begin();
            //case statement for gamestates in draw
            switch (gamestate)
            {
            case GameState.Main:
            {
                spriteBatch.Draw(background, new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), Color.BlueViolet);
                string welcome = "Welcome to Breaking the Fourth \n         Press Enter to Begin";
                spriteBatch.DrawString(font, welcome, fontPosition, Color.White);
                menus.Draw(spriteBatch, mouseState, Config.Buttons.Start);
                menus.Draw(spriteBatch, mouseState, Config.Buttons.Load);
                menus.Draw(spriteBatch, mouseState, Config.Buttons.Exit);
            }
            break;

            case GameState.Controls:
                spriteBatch.Draw(background, new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), Color.BlueViolet);
                spriteBatch.Draw(controls, new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), Color.White);
                break;

            case GameState.Game:
            {
                //drawing methods in here
                DrawBackground(spriteBatch);
                player.Draw(spriteBatch);
                gun.Draw(spriteBatch, player, gun.Rotation, new Vector2(325, 325));
                for (int x = 0; x < terrain.Count; x++)
                {
                    if (terrain[x] is LevelGoal)
                    {
                        // make a Level Goal Object
                        LevelGoal goal = (LevelGoal)terrain[x];
                        goal.Image = levelGoal;
                        goal.Draw(spriteBatch);
                    }
                    else if (terrain[x] is DeathObject)
                    {
                        DeathObject spike = (DeathObject)terrain[x];
                        spike.Image     = spikes;
                        spike.SideImage = sideSpikes;
                        spike.Draw(spriteBatch);
                    }
                    else if (terrain[x] is DisappearingPlatforms)
                    {
                        DisappearingPlatforms disPlat = (DisappearingPlatforms)terrain[x];
                        disPlat.Image = terrainBlock;
                        disPlat.Draw(spriteBatch);
                    }
                    else
                    {
                        terrain[x].Draw(spriteBatch);
                    }
                }
                //THIS SHOULD BE TRACKING THE MOUSE POSITION BUT IT ISN'T AND I HATE IT! For some reason the mouseState is never changing...
                string mouse = ("Mouse X: " + mouseState.X + " Mouse Y: " + mouseState.Y + " Rotation: " + gun.Rotation);
                //spriteBatch.DrawString(font, mouse, fontPosition, Color.Red);
                //UI - Lives left
                spriteBatch.Draw(heart, new Rectangle(10, 5, 30, 30), Color.White);
                spriteBatch.DrawString(font, "X " + player.PlayerLives, new Vector2(50, 5), Color.Black);
                //UI-level #
                spriteBatch.DrawString(font, "Level: " + levelCounter, new Vector2(GraphicsDevice.Viewport.Width - 100, 5), Color.Black);
                //UI- bullets left for puzzle
                spriteBatch.Draw(bullet.BulletTexture, new Rectangle(GraphicsDevice.Viewport.Width - 130, GraphicsDevice.Viewport.Height - 25, 20, 20), Color.White);
                spriteBatch.DrawString(font, "X " + bullet.Bullets, new Vector2(GraphicsDevice.Viewport.Width - 100, GraphicsDevice.Viewport.Height - 25), Color.Black);
                //draw bullet if it has been fired
                if (bullet.BState == Bullet.BulletState.justFired || bullet.BState == Bullet.BulletState.airborne)
                {
                    bullet.Draw(spriteBatch, player);
                }
            }
            break;

            case GameState.Paused:
            {
                //draw game method again
                //draw bg
                spriteBatch.Draw(background, new Rectangle(0, 0, GraphicsDevice.Viewport.Width,
                                                           GraphicsDevice.Viewport.Height), bg);
                //draw player
                player.Draw(spriteBatch);
                gun.Draw(spriteBatch, player, gun.Rotation, new Vector2(325, 325));
                for (int x = 0; x < terrain.Count; x++)
                {
                    terrain[x].Draw(spriteBatch);
                    if (terrain[x] is LevelGoal)
                    {
                        terrain[x].Draw(spriteBatch);
                    }
                }
                //UI - Lives left
                spriteBatch.Draw(heart, new Rectangle(10, 5, 30, 30), Color.White);
                spriteBatch.DrawString(font, "X " + player.PlayerLives, new Vector2(50, 5), Color.Black);
                //UI-level #
                spriteBatch.DrawString(font, "Level: " + levelCounter, new Vector2(GraphicsDevice.Viewport.Width - 100, 5), Color.Black);
                //UI- bullets left for puzzle
                spriteBatch.Draw(bullet.BulletTexture, new Rectangle(GraphicsDevice.Viewport.Width - 130, GraphicsDevice.Viewport.Height - 25, 20, 20), Color.White);
                spriteBatch.DrawString(font, "X " + bullet.Bullets, new Vector2(GraphicsDevice.Viewport.Width - 100, GraphicsDevice.Viewport.Height - 25), Color.Black);
                //draw bullet if it has been fired
                if (bullet.BState == Bullet.BulletState.justFired || bullet.BState == Bullet.BulletState.airborne)
                {
                    bullet.Draw(spriteBatch, player);
                }
                ///////////////////////////////////////////////////////////
                //draw paused menu
                spriteBatch.Draw(paused, new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), Color.White);
                menus.Draw(spriteBatch, mouseState, Config.Buttons.Resume);
                menus.Draw(spriteBatch, mouseState, Config.Buttons.Restart);
                menus.Draw(spriteBatch, mouseState, Config.Buttons.Menu);
                menus.Draw(spriteBatch, mouseState, Config.Buttons.Save);
            }
            break;

            case GameState.LevelClear:
            {
                spriteBatch.Draw(background, new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), Color.DarkGreen);
                string cleared = "Level Cleared! \n Press Enter to go to next level";
                spriteBatch.DrawString(font, cleared, fontPosition, Color.White);
            }
            break;

            case GameState.GameOver:
            {
                spriteBatch.Draw(background, new Rectangle(0, 0, GraphicsDevice.Viewport.Width,
                                                           GraphicsDevice.Viewport.Height), Color.Maroon);
                string death = "GAME OVER\n Press Esc to go to the main menu \n Press Enter to start over";
                spriteBatch.DrawString(font, death, fontPosition, Color.Red);
                menus.Draw(spriteBatch, mouseState, Config.Buttons.Restart);
                menus.Draw(spriteBatch, mouseState, Config.Buttons.Menu);
            }
            break;

            case GameState.GameClear:
            {
                spriteBatch.Draw(background, new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), Color.ForestGreen);
                string cleared = "Game Cleared! \n Press Enter to return to the main menu";
                spriteBatch.DrawString(font, cleared, fontPosition, Color.White);
            }
            break;
            }//end of switch statement
            //draw mouse
            spriteBatch.Draw(crosshare, mouse, Color.White);
            //end spritebatch
            spriteBatch.End();

            base.Draw(gameTime);
        }//end of draw method
        public void Update(List <Terrain> terrain, Gun gun, Player player, MouseState mouseState, MouseState previousMState,
                           float rot, KeyboardState kbState, GraphicsDevice GraphicsDevice, Game1 game)
        {
            //assigns amount of movement to the y direction
            double movementY = (Math.Round(Math.Sin(rotation) * movement.BulletSpeed));//rotation determines where bullet goes
            //assigns amount of movement to the x direction
            double movementX = (Math.Round(Math.Cos(rotation) * movement.BulletSpeed));

            switch (bState)
            {
            case Bullet.BulletState.airborne:
                if (facingLeft == true)
                {
                    //position.X -= movement.BulletSpeed;
                    position.X -= Convert.ToInt32(movementX);
                    position.Y -= Convert.ToInt32(movementY);
                }
                else if (facingLeft == false)
                {
                    movementX   = -movementX;   //shifts bullet direction to resemble where the player is facing
                    position.X += Convert.ToInt32(movementX);
                    position.Y -= Convert.ToInt32(movementY);
                }
                for (int i = 0; i < terrain.Count; i++)
                {
                    if (terrain[i] is DisappearingPlatforms)
                    {
                        DisappearingPlatforms plat = (DisappearingPlatforms)terrain[i];
                        //makes sure you can't teleport to a platform that isn't supposed to be there
                        if ((plat.Type == DisappearingPlatforms.Disappear.Blinking && plat.Tint == Color.Transparent) ||
                            plat.Type == DisappearingPlatforms.Disappear.Intangible)
                        {
                            continue;
                        }
                        if (plat.CollisionDetected(position) == true)
                        {
                            // teleporting
                            IsTeleporting = true;
                            bState        = BulletState.ready;
                            //actual teleporting
                            player.X = position.X;
                            player.Y = position.Y - 40;
                            //stops no clip
                            player.OffsetTele(terrain, i, this);
                        }
                    }

                    if (terrain[i].CollisionDetected(position) == true)     //collision detection causes the bullet to disappear
                    {
                        // teleporting
                        IsTeleporting = true;
                        if (terrain[i] is DeathObject)
                        {
                            game.Death();
                            break;
                        }
                        if (terrain[i] is LevelGoal)
                        {
                            if (terrain[i].CollisionDetected(Position) == true)     ////////////////////
                            {
                                game.PreGamestate = game.Gamestate;
                                game.Gamestate    = GameState.LevelClear;
                            }
                        }
                        bState = BulletState.ready;
                        //actual teleporting
                        player.X = position.X;
                        player.Y = position.Y - 40;
                        //stops no clip
                        player.OffsetTele(terrain, i, this);
                    }
                    if (terrain[i].CollisionDetected(player.Position) == true) //not working
                    {
                        player.OffsetTele(terrain, i, this);
                    }
                }    //end of for loop
                //handles if the bullet leaves the screen in x direction
                if (position.X > GraphicsDevice.Viewport.Width || position.Right < GraphicsDevice.Viewport.X)
                {
                    bState = BulletState.ready;
                }
                //handles if billet leaves screen in y direction
                if (position.Y > GraphicsDevice.Viewport.Height || position.Y < GraphicsDevice.Viewport.Y)
                {
                    bState = BulletState.ready;
                }
                //handles if player changes screen
                if (player.X > GraphicsDevice.Viewport.Width || player.Position.Right < GraphicsDevice.Viewport.X)
                {
                    bState = BulletState.ready;
                }
                break;

            case Bullet.BulletState.empty:
            {
                //allows firing after changing screens
                if (player.X > GraphicsDevice.Viewport.Width || player.Position.Right < GraphicsDevice.Viewport.X)
                {
                    bState = BulletState.ready;
                }
            }
            break;

            case Bullet.BulletState.justFired:
            {
                //sets up bullet position to be right before gun
                position.Y = gun.GunPosition.Top;        //can't fix it with a simple hardcoded offset, due to rot
                if (rotation > 0)
                {
                    position.Y -= Math.Abs(Convert.ToInt32(movementY / movement.BulletSpeed * 28));
                }
                else
                {
                    position.Y += Math.Abs(Convert.ToInt32(movementY / movement.BulletSpeed * 28));
                }
                //shift bulletstate
                bState = BulletState.airborne;
                //bool that says whether it is left or right
                if (player.PState == Player.PlayerState.faceLeft || player.PState == Player.PlayerState.walkLeft)
                {
                    facingLeft = true;
                    position.X = gun.GunPosition.Left;        // -30;
                    //position.X = Convert.ToInt32(gun.Origin.X) - 95;
                    position.X -= Math.Abs(Convert.ToInt32(movementX / movement.BulletSpeed * 28));
                }
                else
                {
                    facingLeft  = false;
                    position.X  = gun.GunPosition.Right;
                    position.X -= Math.Abs(Convert.ToInt32(movementY / movement.BulletSpeed * 28));
                }
            }
            break;

            case Bullet.BulletState.ready:     //fires bullet when clicking left mouse button
            {
                // reset teleporting bool
                IsTeleporting = false;
                if (mouseState.LeftButton == ButtonState.Pressed && previousMState.LeftButton == ButtonState.Released)
                {
                    Fire(rot);
                }
            }
            break;
            } //end of switch statement
        }     //end of update method
        public void Update(KeyboardState kbState, KeyboardState previousKbState, List <Terrain> terrain, Gun gun,
                           GameState gamestate, MouseState mState, Game1 game1, Bullet bullet)
        {
            // determining movement and player orientation
            switch (pState)
            {
            case PlayerState.faceRight:
                if (kbState.IsKeyDown(Keys.D))
                {
                    pState = PlayerState.walkRight;
                }
                if (kbState.IsKeyDown(Keys.A))
                {
                    pState = PlayerState.faceRightWalkLeft;
                }
                if (mState.X <= X + 25)
                {
                    pState = PlayerState.faceLeft;
                }
                break;

            case PlayerState.faceLeft:
                if (kbState.IsKeyDown(Keys.A))
                {
                    pState = PlayerState.walkLeft;
                }
                if (kbState.IsKeyDown(Keys.D))
                {
                    pState = PlayerState.faceLeftWalkRight;
                }
                if (mState.X > X + 25)
                {
                    pState = PlayerState.faceRight;
                }
                break;

            case PlayerState.walkRight:
                if (kbState.IsKeyDown(Keys.D))
                {
                    if (!kbState.IsKeyDown(Keys.A))
                    {
                        X += movement.PlayerSpeed;
                    }
                }
                if (kbState.IsKeyUp(Keys.D))
                {
                    pState = PlayerState.faceRight;
                }
                if (mState.X <= X + 25)
                {
                    pState = PlayerState.faceLeftWalkRight;
                }
                break;

            case PlayerState.walkLeft:
                if (kbState.IsKeyDown(Keys.A))
                {
                    if (!kbState.IsKeyDown(Keys.D))
                    {
                        X -= movement.PlayerSpeed;
                    }
                }

                if (kbState.IsKeyUp(Keys.A))
                {
                    pState = PlayerState.faceLeft;
                }
                if (mState.X > X + 25)
                {
                    pState = PlayerState.faceRightWalkLeft;
                }
                break;

            case PlayerState.faceLeftWalkRight:
                if (kbState.IsKeyDown(Keys.D))
                {
                    if (!kbState.IsKeyDown(Keys.A))
                    {
                        X += movement.PlayerSpeed;
                    }
                }
                if (kbState.IsKeyUp(Keys.D))
                {
                    pState = PlayerState.faceLeft;
                }
                break;

            case PlayerState.faceRightWalkLeft:
                if (kbState.IsKeyDown(Keys.A))
                {
                    if (!kbState.IsKeyDown(Keys.D))
                    {
                        X -= movement.PlayerSpeed;
                    }
                }
                if (kbState.IsKeyUp(Keys.A))
                {
                    pState = PlayerState.faceRight;
                }
                break;
            }
            bool collided = false;

            //collision detection
            for (int i = 0; i < terrain.Count; i++)
            {
                //doesn't allow collision if platform isn't there/has disappeared
                if (terrain[i] is DisappearingPlatforms)
                {
                    justTeleported = false;
                    DisappearingPlatforms plat = (DisappearingPlatforms)terrain[i];
                    if ((plat.Type == DisappearingPlatforms.Disappear.Blinking && plat.Tint == Color.Transparent) ||
                        plat.Type == DisappearingPlatforms.Disappear.Intangible)
                    {
                        continue;
                    }
                    if (plat.CollisionDetected(position) == true)
                    {
                        canJump = false;
                        //stops no clip issues
                        Offset(terrain, kbState, i, bullet, game1);
                        //halts jumping after colliding
                        isJumping = false;
                        collided  = true;
                    }
                }
                if (terrain[i].CollisionDetected(position) == true) /////special terrain is causing issue still when going down-resolved
                {
                    if (terrain[i] is DeathObject)
                    {
                        game1.Death();
                    }
                    if (terrain[i] is LevelGoal)
                    {
                        if (terrain[i].CollisionDetected(Position) == true)
                        {
                            justTeleported     = false;
                            game1.PreGamestate = gamestate;
                            game1.Gamestate    = GameState.LevelClear;
                        }
                    }
                    canJump = false;
                    //stops no clip issues
                    Offset(terrain, kbState, i, bullet, game1);
                    //halts jumping after colliding
                    isJumping = false;
                    collided  = true;
                }
                //fixes stuttering when moving down on a moving platform
                if (!(position.Left > terrain[i].Position.Right) && !(position.Right < terrain[i].Position.Left))
                {
                    if (terrain[i] is SpecialTerrain)
                    {
                        if (!(terrain[i] is DeathObject || terrain[i] is LevelGoal) &&
                            (Y <= terrain[i].Y - position.Height && Y >= terrain[i].Y - position.Height - movement.Gravity))//limits y range of activation
                        {
                            SpecialTerrain st = (SpecialTerrain)terrain[i];
                            if (st.MinX == -1 && isJumping == false)
                            {
                                onVST          = true;//set standing on vertical st true
                                onST           = false;
                                Y              = st.Position.Top - position.Height;
                                justTeleported = false;
                                isFalling      = false;
                                isJumping      = false;
                                startingY      = position.Y;
                                collided       = true;
                                canJump        = true;
                            }
                            //should keep you stuck to the platform
                            else if (st.MaxY == -1)
                            {
                                if (kbState.IsKeyUp(Keys.A) && kbState.IsKeyUp(Keys.D) && kbState.IsKeyUp(Keys.Space))
                                {
                                    onST           = true;
                                    onVST          = false; //set standing on st true
                                    Y              = st.Position.Top - position.Height;
                                    justTeleported = false;
                                    isFalling      = false;
                                    isJumping      = false;
                                    collided       = true;
                                    canJump        = true;
                                    if (st.MovingLeft == true)
                                    {
                                        X--;
                                    }
                                    else
                                    {
                                        X++;
                                    }
                                }
                            }
                        }
                        else
                        {
                            onST  = false;
                            onVST = false;
                        }
                    }
                }
                //checks if player is standing on terrain & counts that as colliding
                if (!(position.Left > terrain[i].Position.Right) && !(position.Right < terrain[i].Position.Left))
                {
                    if (position.Bottom == terrain[i].Position.Top)
                    {
                        //fixed issue where you could land on spikes
                        if (terrain[i] is DeathObject)
                        {
                            game1.Death();
                        }
                        if (terrain[i] is LevelGoal)
                        {
                            game1.PreGamestate = gamestate;
                            game1.Gamestate    = GameState.LevelClear;
                        }
                        if (position.Bottom <= 0)
                        {
                            position.Y = terrain[i].Position.Top + terrain[i].Position.Height;
                        }
                        justTeleported = false;
                        isFalling      = false;
                        isJumping      = false;
                        startingY      = position.Y;
                        collided       = true;
                        canJump        = true;
                    }
                }
            }//end of loop for collision detection
            if (isJumping == false && collided == false)
            {
                isFalling = true;
            }
            if (isFalling == true)
            {
                //go down-gravity
                Gravity();
                isJumping      = false; //stops player from jumping while falling to slow descend
                canJump        = false;
                justTeleported = false; //reset bool to false so it is only true for the frame exactly after teleporting
                onST           = false;
                onVST          = false;
            }
            if (isJumping == true)
            {
                position.Y -= 4;
                if (position.Y <= (startingY - (.75 * position.Height))) //keeps jumps to being equal to 3/4ths of the player's height
                {
                    //starts the player falling after jump is complete
                    isFalling = true;
                    onST      = false;
                    onVST     = false;
                }
            }
            //jump start
            else if (kbState.IsKeyDown(Keys.Space) && previousKbState.IsKeyUp(Keys.Space) && canJump == true)
            {
                //jump logic
                //go up
                position.Y -= 4;
                isJumping   = true;
            }
        }//end of update method