public override void Update(ContentManager content, GameTime gameTime)
        {
            if (isLoaded == false)
            {
                isLoaded = true;
                middle   = new Vector2(Game1.Instance.ScreenWidth / 2, Game1.Instance.ScreenHeight / 2);
                back     = content.Load <Texture2D>("back");
                star.Load(content, "starSelector");
                arial         = content.Load <SpriteFont>("Arial");
                titleMeasure  = (arial.MeasureString("A5"));
                title         = new Vector2(middle.X - titleMeasure.X / 2, middle.Y - titleMeasure.Y / 2);
                soloMeasure   = (arial.MeasureString("Solo"));
                solo          = new Vector2(middle.X - soloMeasure.X / 2, middle.Y - soloMeasure.Y / 2) - menuSpacing;
                versusMeasure = (arial.MeasureString("Versus"));
                versus        = new Vector2(middle.X - versusMeasure.X / 2, middle.Y - versusMeasure.Y / 2);
                quitMeasure   = (arial.MeasureString("Quit"));
                quit          = new Vector2(middle.X - quitMeasure.X / 2, middle.Y - quitMeasure.Y / 2) + menuSpacing;
                menuMusic.Load(content, "A5_Menu_Music");
                menuMusic.soundInstance.IsLooped = true;
                menuMusic.soundInstance.Play();
                previousState = Keyboard.GetState();
                menuCursor    = 1;
                titleAlpha    = 0f;
                alpha         = 0f;
            }

            float deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds;

            titleAlpha  = MathHelper.Clamp(titleAlpha, 0f, 1f);
            alpha       = MathHelper.Clamp(alpha, 0f, 1f);
            title.Y     = MathHelper.Clamp(title.Y, Game1.Instance.ScreenHeight * 0.2f, Game1.Instance.ScreenHeight);
            title.Y    -= 50 * deltaTime;
            titleAlpha += 0.25f * deltaTime;

            if (title.Y <= Game1.Instance.ScreenHeight * 0.2f)
            {
                alpha += 0.2f * deltaTime;
            }

            currentState = Keyboard.GetState();
            if (currentState.IsKeyDown(Keys.Up) && previousState.IsKeyUp(Keys.Up) || currentState.IsKeyDown(Keys.W) && previousState.IsKeyUp(Keys.W))
            {
                menuCursor -= 1;
            }
            if (currentState.IsKeyDown(Keys.Down) && previousState.IsKeyUp(Keys.Down) || currentState.IsKeyDown(Keys.S) && previousState.IsKeyUp(Keys.S))
            {
                menuCursor += 1;
            }
            if (alpha >= 0.25f)
            {
                switch (menuCursor)
                {
                case 1:
                    starPos = solo - star.offset + new Vector2(-star.texture.Width, soloMeasure.Y / 2);
                    if (currentState.IsKeyDown(Keys.Enter) && previousState.IsKeyDown(Keys.Enter))
                    {
                        isLoaded = false;
                        StateManager.ChangeState("Solo Game");
                        menuMusic.soundInstance.Stop();
                    }
                    break;

                case 2:
                    starPos = versus - star.offset + new Vector2(-star.texture.Width, versusMeasure.Y / 2);
                    if (currentState.IsKeyDown(Keys.Enter) && previousState.IsKeyDown(Keys.Enter))
                    {
                        isLoaded = false;
                        StateManager.ChangeState("Versus Game");
                        menuMusic.soundInstance.Stop();
                    }
                    break;

                default:
                    starPos = quit - star.offset + new Vector2(-star.texture.Width, quitMeasure.Y / 2);
                    if (currentState.IsKeyDown(Keys.Enter))
                    {
                        Game1.Instance.Exit();
                    }
                    break;
                }
            }
            previousState = currentState;
            if (menuCursor > 3)
            {
                menuCursor = 1;
            }
            if (menuCursor < 1)
            {
                menuCursor = 3;
            }
        }
Esempio n. 2
0
        public override void Update(ContentManager Content, GameTime gameTime)
        {
            if (isLoaded == false)
            {
                isLoaded   = true;
                arial      = Content.Load <SpriteFont>("Arial");
                background = Content.Load <Texture2D>("back");
                star.Load(Content, "starSelector");
                music.Load(Content, "A5_End_Music");
                music.soundInstance.IsLooped = true;
                music.soundInstance.Play();
                oldState     = Keyboard.GetState();
                middle       = new Vector2(Game1.Instance.ScreenWidth / 2, Game1.Instance.ScreenHeight / 2);
                titleMeasure = arial.MeasureString("GAME OVER");
                title        = new Vector2(middle.X - titleMeasure.X / 2, middle.Y - titleMeasure.Y / 2) - (MenuState.Instance.menuSpacing * 3);
                retryMeasure = arial.MeasureString("Retry");
                retry        = new Vector2(middle.X - retryMeasure.X / 2, middle.Y - retryMeasure.Y / 2);
                menuMeasure  = arial.MeasureString("Menu");
                menu         = new Vector2(middle.X - menuMeasure.X / 2, middle.Y - menuMeasure.Y / 2) + MenuState.Instance.menuSpacing;
                scoreMeasure = arial.MeasureString("You Survived " + SoloGameState.Instance.gameTimer.ToString("##0") + " Seconds");
                score        = new Vector2(middle.X - scoreMeasure.X / 2, middle.Y - scoreMeasure.Y / 2) - (MenuState.Instance.menuSpacing * 2);
            }
            float deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds;

            KeyboardState newState = Keyboard.GetState();

            if (newState.IsKeyDown(Keys.Up) && oldState.IsKeyUp(Keys.Up) || newState.IsKeyDown(Keys.W) && oldState.IsKeyUp(Keys.W))
            {
                menuCursor -= 1;
            }
            if (newState.IsKeyDown(Keys.Down) && oldState.IsKeyUp(Keys.Down) || newState.IsKeyDown(Keys.S) && oldState.IsKeyUp(Keys.S))
            {
                menuCursor += 1;
            }
            switch (menuCursor)
            {
            case 1:
                starPos = retry - star.offset + new Vector2(-star.texture.Width, retryMeasure.Y / 2);
                if (newState.IsKeyDown(Keys.Enter) && oldState.IsKeyUp(Keys.Enter))
                {
                    isLoaded = false;
                    StateManager.ChangeState("Solo Game");
                    music.soundInstance.Stop();
                    menuCursor = 1;
                    SoloGameState.Instance.gameTimer = 0;
                }
                break;

            default:
                starPos = menu - star.offset + new Vector2(-star.texture.Width, menuMeasure.Y / 2);
                if (newState.IsKeyDown(Keys.Enter) && oldState.IsKeyUp(Keys.Enter))
                {
                    isLoaded = false;
                    StateManager.ChangeState("Menu");
                    music.soundInstance.Stop();
                    menuCursor = 1;
                    SoloGameState.Instance.gameTimer = 0;
                }
                break;
            }
            oldState = newState;
            if (menuCursor > 2)
            {
                menuCursor = 1;
            }
            if (menuCursor < 1)
            {
                menuCursor = 2;
            }
        }
        public override void Update(ContentManager Content, GameTime gameTime)
        {
            if (isLoaded == false)
            {
                isLoaded = true;
                player1.Load(Content);
                player2.Load(Content);
                projectiles.Load(Content);
                bgMusic.Load(Content, "versusMusic");
                bgMusic.soundInstance.IsLooped = true;
                bgMusic.soundInstance.Play();
                arial        = Content.Load <SpriteFont>("Arial");
                background   = Content.Load <Texture2D>("VersusBackground");
                startTimer   = 0f;
                reboundTimer = 0f;
                rebound      = false;
                pong.Load(Content, "shieldUp");
                gameOver.Load(Content, "bigExplosion");
                projectiles.velocity = projectiles.direction * 4;
            }

            float deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds;

            startTimer += deltaTime;

            player1.Update(deltaTime);

            player2.Update(deltaTime);
            if (startTimer >= 3)
            {
                projectiles.Update(deltaTime);
            }

            Vector2 pos  = new Vector2(-player1.playerSprite.texture.Width / 2, player1.playerSprite.texture.Height / 2);
            Vector2 pos2 = new Vector2(player1.playerSprite.texture.Width / 2, player1.playerSprite.texture.Height / 2);
            Vector2 bet  = pos2 - pos;

            bet.Normalize();
            Vector2 nor = Vector2.Zero;

            nor.X = bet.Y;
            nor.Y = -bet.X;
            Vector2 newVel = projectiles.GetVelocity() - 2 * Vector2.Dot(projectiles.GetVelocity(), nor) * nor;

            if (projectiles.b_projectileSphere.Intersects(player1.b_playerBox) && rebound == false)
            {
                rebound = true;
                projectiles.hitPlayer1 = true;
                projectiles.SetVelocity(newVel);
                p1Wins = true;
                pong.sound.Play();
                projectiles.velocity *= 1.1f;
            }
            Vector2 p2pos1 = new Vector2(player2.playerSprite.origin.X, player2.playerSprite.origin.Y);
            Vector2 p2Pos2 = new Vector2(player2.playerSprite.origin.X + player2.playerSprite.texture.Width, player2.playerSprite.texture.Height + player2.playerSprite.origin.Y);
            Vector2 p2bet  = p2Pos2 - p2pos1;

            p2bet.Normalize();
            Vector2 p2Normal = Vector2.Zero;

            p2Normal.X = p2bet.Y;
            p2Normal.Y = -p2bet.X;
            Vector2 p2newVel = projectiles.GetVelocity() - 2 * Vector2.Dot(projectiles.GetVelocity(), p2Normal) * p2Normal;

            if (player2.b_playerBox.Intersects(projectiles.b_projectileSphere) && rebound == false)
            {
                rebound = true;
                projectiles.hitPlayer1 = true;
                projectiles.SetVelocity(p2newVel);
                p1Wins = false;
                pong.sound.Play();
                projectiles.velocity *= 1.05f;
            }

            if (projectiles.projSprite.origin.X >= Game1.Instance.ScreenWidth)
            {
                projectiles.hitPlayer1 = true;

                Vector2 position1 = new Vector2(Game1.Instance.ScreenWidth, 0);
                Vector2 position2 = new Vector2(Game1.Instance.ScreenWidth, Game1.Instance.ScreenHeight);
                Vector2 between   = position2 - position1;
                between.Normalize();
                Vector2 normal = Vector2.Zero;
                normal.X = between.Y;
                normal.Y = -between.X;
                Vector2 newVelocity = projectiles.GetVelocity() - 2 * Vector2.Dot(projectiles.GetVelocity(), normal) * normal;
                projectiles.SetVelocity(newVelocity);
                projectiles.velocity *= 1.05f;
            }

            if (projectiles.projSprite.origin.X <= 0)
            {
                projectiles.hitPlayer2 = true;

                Vector2 position1 = new Vector2(0, 0);
                Vector2 position2 = new Vector2(0, Game1.Instance.ScreenHeight);
                Vector2 between   = position2 - position1;
                between.Normalize();
                Vector2 normal = Vector2.Zero;
                normal.X = between.Y;
                normal.Y = -between.X;
                Vector2 newVelocity = projectiles.GetVelocity() - 2 * Vector2.Dot(projectiles.GetVelocity(), normal) * normal;
                projectiles.SetVelocity(newVelocity);
                projectiles.velocity *= 1.05f;
            }

            if (projectiles.projSprite.origin.Y - projectiles.projSprite.offset.Y <= 0 || projectiles.projSprite.origin.Y + projectiles.projSprite.offset.Y >= Game1.Instance.ScreenHeight)
            {
                StateManager.ChangeState("Versus GameOver");
                bgMusic.soundInstance.Stop();
                isLoaded = false;
                gameOver.soundInstance.Play();
            }

            if (rebound == true)
            {
                reboundTimer += deltaTime;
                if (reboundTimer >= 0.3f)
                {
                    reboundTimer = 0f;
                    rebound      = false;
                }
            }
        }