Question

So I have this code: Variables:

    enum gameState
    {
        gameLoading,
        mainMenu,
        gameOptions,
        levelSelect,
        gamePlaying,
        gameOver
    }

In the Update() method:

        if (CurrentGameState == gameState.gameLoading)
        {
            if (Keyboard.GetState().IsKeyDown(Keys.E))
            {
                graphics.ToggleFullScreen(); //?
            }
            graphics.ApplyChanges();
        }
        ...
        //gameState.gameLoading logic
        if (Keyboard.GetState().IsKeyDown(Keys.Enter))
        CurrentGameState = gameState.mainMenu;

So what I want is to have Enter pressed in gameState.gameLoading and both resolution is fullscreen and the gameState is equal to mainMenu. When in gameState.mainMenu the resolution can't be changed from fullscreen to windowed by pressing enter. How to achieve this? Maybe using list?

Was it helpful?

Solution

I think you should have different classes for any gameState if they need to have different behaviours. This will ensure you that each Update affect only its own gameState.

OTHER TIPS

Since you've already decided what the states of your game are, why don't use the State Machine design pattern to control your input handling behaviour? This pattern's agenda is to delegate the work from the actual object to its states. What you do is create a class with an Update() method for each state like pinckerman suggested, and enter all the input handling + state transition logic there. You can find an example here: http://sourcemaking.com/design_patterns/state. When I use it, I detach the context from it's states and transitions completly by using an abstract state class and inherit from it. This makes it easier to change the state machine if needed. Here is a quick example:

public class Context
{
    private GameState state;

    public void Update()
    {
         state.Update();
    }

    public void ChangeState(GameState nextState)
    {
         state = nextState;
    }
}

public abstract class GameState
{
    protected Context context;

    public virtual void Update()
    {
        // some basic implementation if you want.
    }
}

public class GameLoadingState : GameState
{
    public override void Update()
    {
        // Handle key presses.
        context.ChaneState(new MainMenuState(context));
    }
}

public class MainMenuState : GameState
{
    public override void Update()
    {
        // Handle key presses in some other way.
        // Change state if needed.
    }
}

Again, if you don't like the implementation of the passive context, you can change it to be more active. Hope this helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top