-
Notifications
You must be signed in to change notification settings - Fork 19
Separating Game states
Suppose you have a simple game with some levels, and now you wanted to extend your game. Think of things like, adding an introduction screen, a tutorial screen for the game, and or the sub game screen (for games that have other embedded games inside) or even the highscore / gameover screens. The game will behave differently in each screen right? And if you go with the classic way of using enums for them, your code will become very messy, huge and unmaintainable very soon.
@Override
public void update(float delta)
{
switch (currentState)
{
case INTRO:
// Code for the introduction screen
case TUTORIAL:
// Code for the tutorial screen
case GAME:
// Code for the actual playing logic
case END:
// Code for the game over / highscore screens
}
}
And then, this simply goes on forever, you might add unnecessary variables to the game class that you only use in a certain state, and that will make the code of your game look very messy. The solution for this is to move the code for each screen into it's own class, that it now will be easier to read and write more code. Each state should be considered as a completely different game, and a game is a collection of different states. In SilenceEngine, that is done by extending the GameState
class.
Just like your main game class, the game state classes also have a specific skeleton code. Take this as an example game state that does nothing.
public class MyGameState extends GameState
{
@Override
public void onEnter() {}
@Override
public void update(float delta) {}
@Override
public void render(float delta) {}
@Override
public void resized() {}
@Override
public void onLeave() {}
}
This is the skeleton of the game state. You implement these methods to make your own state. Then all you have to do is to set the current game state, and the game will automatically call these methods on the current game state.
At anytime in the game, there will be only one current gamestate, which will recieve the game events. To set and or get the current GameState, you can use the methods of the Game
class.
public void setGameState(GameState gameState);
public GameState getGameState();
These methods allow to set the current game state and get the current game state. The events will automatically be passed to the current game state just after the event is recieved by the game.
Other than the default events update, render and resize, you have two new events called onEnter and onLeave in the GameStates that are called when the game state becomes the current and when a new one becomes the current.
@Override
public void onEnter()
{
// Called when this game state becomes the current
}
This is the onEnter event method. This method is called after gamestate is made current in the game. This is used to take actions once it is active. You can construct the game state once, and make them active at any time in any order you wish.
@Override
public void onLeave()
{
// Called when this game state is no longer the current
}
This is the onLeave event method. This method is called when the game state is no more current in the game.
Written by Sri Harsha Chilakapati | https://goharsha.com
- Home
- Introduction
- The Basics
- Getting Input
- Rendering Graphics
- Playing sounds
- The Scenes and Entities
- Going more low-level
- Examples/Demos