Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Improve States to unclog my game1 class

$
0
0
I have recently become enthralled with XNA Game Studio 4.0. For a college assignment we made a simple pirate game which functions perfect. However i want to clean up my game1 class and continue to use this project as a learning curve. My question is how can I implement a class that will control all my game states as my spriteManager manages all my sprites. Take a look at the code (its fairly basic as I'm new to XNA and C#) and any advice or a link to a tutorial would be brilliant
protected override void Update(GameTime gameTime)
        {
            switch (currentGameState)
            {
                case GameState.Menu:
                    
                       
                    if (Keyboard.GetState().IsKeyDown(Keys.Space) && !lastpressed)
                    {
                        lastpressed = true;
                        cue = soundBank.GetCue("MenuMusic");
                        cue.Play(); 
                   
                        currentGameState = GameState.Gameplay;
                        spriteManager.Enabled = true;
                        spriteManager.Visible = true;
                        this.IsMouseVisible = true;
                    }
                    else if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                    {
                        Exit();
                    }

                    break;

                case GameState.Gameplay:
                    this.IsMouseVisible = false;
                    if (Keyboard.GetState().IsKeyDown(Keys.P))
                    {
                        currentGameState = GameState.pause;
                        spriteManager.Enabled = false;
                        spriteManager.Visible = false;
                        this.IsMouseVisible = true;
                    }
                    if (spriteManager.health <= 0)
                    {
                        currentGameState = GameState.GameOver;
                        spriteManager.Dispose();
                        spriteManager.Visible = false;
                        this.IsMouseVisible = true;
                    }
                    break;

                case GameState.pause:
                    if (Keyboard.GetState().IsKeyDown(Keys.P))
                    {
                        currentGameState = GameState.Gameplay;
                        spriteManager.Enabled = true;
                        spriteManager.Visible = true;
                        this.IsMouseVisible = false;
                    }
                    break;

                case GameState.GameOver:
                    if (Keyboard.GetState().IsKeyDown(Keys.Space) && ! lastpressed)
                    {
                        lastpressed = true;
                        Components.Remove(spriteManager);
                        spriteManager = new SpriteManager(this);
                        Components.Add(spriteManager);
                        spriteManager.Initialize();
                        currentGameState = GameState.Menu;
                        //spriteManager.Initialize();
                        spriteManager.Enabled = false;
                        spriteManager.Visible = false;
                        this.IsMouseVisible = false;
                    }
                    break;
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Space))
            {
                lastpressed = false;
            }
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();


            audioEngine.Update();
            base.Update(gameTime);
        }



Looking forward to hearing your advice
Mark

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>