Game Development Reference
In-Depth Information
case GameState.SettingsMenu:
{
// Update the settings menu
} break;
case GameState.PlayingGame:
{
// Update the game
} break;
case GameState.TitleMenu:
{
// Update title menu
} break;
default:
{
// Error invalid state
} break;
}
}
There are a few things to notice here. The code is very long and prone to become
complicated and hard to follow. It also breaks the DRY principle. To add a new
state, let's say a credits screen, a new entry must be added to the game state enum
and then added to the switch statement. When extending the code, the fewer
places that require change the better.
This big switch statement can be replaced with a much better system using the
IGameObject interface, where every game state is a game object.
It's probably best to start a new project with a game loop to follow this example.
Once this
is done,
it's
time to create a new class
implementing the
IGameObject interface for each game state.
namespace GameStructure
{
class SplashScreenState : IGameObject
{
}
}
Here a splash screen class has been created that inherits from the IGameObject
interface. The above code is invalid because it doesn't implement any of the
IGameObject methods. Instead of typing the methods out by hand, one of the
 
Search WWH ::




Custom Search