/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { screenManager = new StateMachine(); menuScreen = new MenuScreen(); screenManager.States.Add(menuScreen); screenManager.States.InitialState = menuScreen; base.Initialize(); }
private void CreateStateMachine() { // Let's create a state machine that manages all states and transitions. _stateMachine = new StateMachine(); // ----- First we need to define all possible states of the game component: // The initial state is the "Loading" state. In this state a "Loading..." // text is rendered and all required assets are loaded in the background. var loadingState = new State { Name = "Loading" }; loadingState.Enter += OnEnterLoadingScreen; loadingState.Exit += OnExitLoadingScreen; // The second state is the "Start" state. Once loading is finished the text // "Press Start button" is shown until the users presses the Start button // on the gamepad. var startState = new State { Name = "Start" }; startState.Enter += OnEnterStartScreen; startState.Update += OnUpdateStartScreen; startState.Exit += OnExitStartScreen; // The "Menu" state represents the main menu. It provides buttons to start // the game, show sub menus, and exit the game. var menuState = new State { Name = "Menu" }; menuState.Enter += OnEnterMenuScreen; menuState.Exit += OnExitMenuScreen; // The "SubMenu" is just a dummy menu containing a few buttons. It is shown // whenever a sub menu is selected in the main menu. var subMenuState = new State { Name = "SubMenu" }; subMenuState.Enter += OnEnterSubMenuScreen; subMenuState.Update += OnUpdateSubMenuScreen; subMenuState.Exit += OnExitSubMenuScreen; // The "Game" state is a placeholder for the actual game content. var gameState = new State { Name = "Game" }; gameState.Enter += OnEnterGameScreen; gameState.Update += OnUpdateGameScreen; gameState.Exit += OnExitGameScreen; // Register the states in the state machine. _stateMachine.States.Add(loadingState); _stateMachine.States.Add(startState); _stateMachine.States.Add(menuState); _stateMachine.States.Add(subMenuState); _stateMachine.States.Add(gameState); // Optional: Define the initially selected state. // (If not set explicitly then the first state in the list is used as the // initial state.) _stateMachine.States.InitialState = loadingState; // ----- Next we can define the allowed transitions between states. // The "Loading" screen will transition to the "Start" screen once all assets // are loaded. The assets are loaded in the background. The background worker // sets the flag _allAssetsLoaded when it has finished. // The transition should fire automatically. To achieve this we can set FireAlways // to true and define a Guard. A Guard is a condition that needs to be fulfilled // to enable the transition. This way the game component automatically switches // from the "Loading" state to the "Start" state once the loading is complete. var loadToStartTransition = new Transition { Name = "LoadingToStart", TargetState = startState, FireAlways = true, // Always trigger the transition, if the guard allows it. Guard = () => _allAssetsLoaded, // Enable the transition when _allAssetsLoaded is true. }; loadingState.Transitions.Add(loadToStartTransition); // The remaining transition need to be triggered manually. // The "Start" screen will transition to the "Menu" screen. var startToMenuTransition = new Transition { Name = "StartToMenu", TargetState = menuState, }; startState.Transitions.Add(startToMenuTransition); // The "Menu" screen can transition to the "Game" screen or to the "SubMenu". var menuToGameTransition = new Transition { Name = "MenuToGame", TargetState = gameState, }; var menuToSubMenuTransition = new Transition { Name = "MenuToSubMenu", TargetState = subMenuState, }; menuState.Transitions.Add(menuToGameTransition); menuState.Transitions.Add(menuToSubMenuTransition); // The "Game" screen will transition back to the "Menu" screen. var gameToMenuTransition = new Transition { Name = "GameToMenu", TargetState = menuState, }; gameState.Transitions.Add(gameToMenuTransition); // The "SubMenu" can transition to back to the "Menu" screen. var subMenuToMenuTransition = new Transition { Name = "SubMenuToMenu", TargetState = menuState, }; subMenuState.Transitions.Add(subMenuToMenuTransition); }