Exemplo n.º 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)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                this.Exit();
            }

            // TODO: Add your update logic here
            previousKbState = kbState;
            kbState         = Keyboard.GetState();

            // Esc to close game
            if (kbState.IsKeyDown(Keys.Escape))
            {
                this.Exit();
            }

            dt = (float)gameTime.ElapsedGameTime.TotalSeconds;

            switch (state)
            {
            case State.MENU:
                score    = 0;
                topText  = "Enter to begin. A to autoplay";
                autoplay = false;

                if (GetSongFilePath("song.txt")[0] != songFile[0])
                {
                    audioAnalysis.DisposeAudioAnalysis();
                    songFile = GetSongFilePath("song.txt");
                    AnalyseSong(songFile[0]);
                }

                PlaceBeats();

                if (kbState.IsKeyDown(Keys.Enter))
                {
                    state = State.START_GAME;
                }
                else if (kbState.IsKeyDown(Keys.A))
                {
                    autoplay = true;
                    state    = State.START_GAME;
                }

                break;

            case State.START_GAME:
                audioAnalysis.PlayAudio();

                topText = "";
                state   = State.PLAYING;
                break;

            case State.PLAYING:

                // If the Quit button is pressed, end game
                if (kbState.IsKeyDown(Keys.R))
                {
                    state = State.END;
                }
                // If the playback has reached the end of the audio track, end game
                if (audioAnalysis.PCMStream.Position >= audioAnalysis.PCMStream.Length)
                {
                    state = State.END;
                }

                // Check if the Hit button is pressed
                drumDown = false;
                if (kbState.IsKeyDown(Keys.Left) ||
                    kbState.IsKeyDown(Keys.Right) ||
                    autoplay)
                {
                    drumDown = true;
                }

                for (int i = 0; i < gameObjects.Count; i++)
                {
                    gameObjects[i].update(dt);
                }

                for (int i = 0; i < notes.Count; i++)
                {
                    notes[i].update(dt);

                    /*
                     * // Remove notes that go off screen
                     * if (notes[i].Position.X + notes[i].Origin.X < 0)
                     * {
                     *  notes[i].Active = false;
                     * }
                     * else if (notes[i].Position.Y + notes[i].Origin.Y < 0)
                     * {
                     *  notes[i].Active = false;
                     * }
                     * else if (notes[i].Alpha <= 0)
                     * {
                     *  notes[i].Active = false;
                     * }*/

                    // Check for hit collision
                    if (drumDown)
                    {
                        float notePos = notes[i].Position.X;

                        // If note is on top of the marker
                        if (Math.Abs(markerPosX - notePos) <= noteSize &&
                            marker.Position.Y == notes[i].Position.Y)
                        {
                            // Increase the score
                            score += 100;
                            // Play a hit and fade away animation
                            notes[i].AlphaVelocity = -5.0f;
                            notes[i].Velocity      = new Vector2(0, -1000);
                            notes[i].TintColor     = Color.GhostWhite;
                        }
                    }
                }
                break;

            case State.END:
                audioAnalysis.StopAudio();

                topText = "Game Over. R to restart";
                if (previousKbState.IsKeyUp(Keys.R) && kbState.IsKeyDown(Keys.R))
                {
                    state = State.MENU;
                }
                break;
            }

            base.Update(gameTime);
        }