NuclearWinter > Game flow

You can use the GameStateManager game component to handle different game states like menus, cut scenes, in-game, credits, etc.

Just create an instance of it in your main game class and add it to its Components list.

namespace YourGame
{
    public class YourGame
    {
        [...]
        protected override void Initialize()
        {
            GameStateMgr = new NuclearWinter.GameFlow.GameStateManager( this );
            Components.Add( GameStateMgr );
            
            // Instantiate all of your GameStates
            Intro       = new GameStateIntro( this );
            MainMenu    = new GameStateMainMenu( this );
            InGame      = new GameStateInGame( this );
            Credits     = new GameStateCredits( this );
            
            // Setup your initial GameState
            GameStateMgr.SwitchState( Intro );
            
            base.Initialize();
        }
        [...]
        public NuclearWinter.GameFlow.GameStateMgr      GameStateMgr        { get; private set; }
        
        // Here you'll put all of your GameStates. Make them public so each GameState can switch to them
        public GameStateIntro                           Intro               { get; private set; }
        public GameStateMainMenu                        MainMenu            { get; private set; }
        public GameStateInGame                          InGame              { get; private set; }
        public GameStateCredits                         Credits             { get; private set; }
    }
}


Making the GameStateMgr public will allow any GameState to access it and switch to another GameState.

You'll have to subclass NuclearWinter.GameFlow.GameState for each of your game's states.
At the very least, you'll have to override the following abstract methods:
  • Start() which gets called when your GameState becomes the current one
  • Stop() which gets called just before switching to another GameState
  • Update() which gets called every frame
  • Draw() which gets called every frame too, after Update()

Here's a simple GameState subclass for some random menu:

namespace YourGame
{
    public class GameStateSomeMenu: NuclearWinter.GameFlow.GameState
    {
        public GameStateSomeMenu( YourGame _yourGame )
        {
            // Keeping an instance of YourGame around will allow you to access all of its components, like the GameStateMgr
            mYourGame = _yourGame;
            
            // You'll probably to have a (XNA) ContentManager for each GameState so you can load resources and free them per GameState
            mContent = new ContentManager( mGame.Services, "Content" );
        }
        
        public override void Start()
        {
            // Load some resources (textures, sounds...)
        }
        
        public override void Stop()
        {
            // Do some stuff, release loaded content for instance
        }

        public override void Update( GameTime _time )
        {
            // Run the Update logic, check for input, do some computing
        }
        
        public override void Draw( GameTime _time )
        {
            // Render your menu, game or whatever it is this GameState is about
        }
        
        YourGame          mYourGame;
        ContentManager    mContent;
    }
}

Last edited May 21, 2011 at 8:46 PM by elisee, version 1

Comments

No comments yet.