This would be a small tutorial regarding the basic structure of the Gameplay screen. We would start by creating the Gameplay class which would extend the abstract class Screen which we made in Part I of this series. The structure would be similar to the MainMenu class.
public Gameplay(int backScreenid,OrthographicCamera cam,TapResponse tapresponse,int levelNumber){ BackScreenID=backScreenid; camera=cam; tapResponse=tapresponse; spriteBatch=new spriteBatch(); spriteBatch.setProjectionMatrix(camera.combined); CurrentLevel=levelNumber; //Store which level to load Init(); //Initialize textures, variables and game state } void Init(){ Assets.Load(); //in Assets class we would be loading the textures and settings //initialize the buttons in the menu } @Override public void update (Application app){ //Check which button is clicked. //Set BackScreenID to the screen which should be shown next //depending on the button click //and then set IsDone=true } @Override public void render (Application app){ Gdx.graphics.getGL10().glClear(GL10.GL_COLOR_BUFFER_BIT); //Clear the screen spritebatch.begin(); //Render solid backgrounds without alpha channel spritebatch.disableBlending(); //Render those textures which dont have any alpha/transparent component //As it is faster to draw them while blending is disabled spritebatch.enableBlending(); spritebatch.end(); } @Override public void dispose (){ //Dispose of the textures,fonts //dispose of spritebatch } @Override public void OnPause(){ } @Override public void OnResume(){ } @Override public boolean isDone() { return IsDone; } }
The above code is mostly same as the MainMenu class described in the last tutorial. We just added one more argument in the constructor for the level which is to be loaded when the screen changes from menu screen to this screen.
Over the next tutorials we would be making a simplfied version of our game Polar.
Let’s see what would be the different high level states in the gameplay cycle.
GAME_PRE_INITIALIZE: The initial state of the game when the constructor is called.
GAME_INITIALIZE: After loading the textures and setting the initial values of different objects. After initialization either the game can start automatically or it can wait for a event from the player.
GAME_RUNNING: In this state the player is in the active state.
GAME_PAUSE: This is the state when player pauses the game or the level finishes.
GAME_RESUME: This is the state when the player resumes a paused game or starts/restarts the level.
GAME_OVER: When the player dies or the level is finished.
So we would be making a finite state machine or simply an integer variable holding these different states.
public class Gameplay{ //STATE CONSTANTS final int GAME_PRE_INITIALIZE=0; final int GAME_INITIALIZE=1; final int GAME_RUNNING=2; final int GAME_PAUSE=3; final int GAME_RESUME=4; final int GAME_OVER=5; int currentState; int lastState; public Gameplay(...){ //Constructor //...... lastState=GAME_PRE_INITIALIZE; currentState=GAME_PRE_INITIALIZE; Init(); //...... } void Init(){ //After initializing we can set the state. Now if we want to load textures using a different thread // then we can check the states to see if the textures are loaded and show a loading bar in the //meantime currentState=GAME_INITIALIZE; } void Update(float dt){ //...... UpdateGameState() //...... } void UpdateGameState(){ if(currentState==GAME_PRE_INITIALIZE) // Show loading if(currentState==GAME_INITIALIZE) //Wait for user input to start the game if(currentState == GAME_RUNNING) //Update Game states for enemies collectibles etc. if(currentState==GAME_PAUSED) //Check for user input for either resume or exit from game if(currentState==GAME_RESUME){ //For a simple case we will consider game is paused at the init state or in between the level or after game is over if(lastState==GAME_INITIALIZE) //Start the level if(lastState==GAME_RUNNING) //Continue the game if(lastState==GAME_OVER) //Restart the level or load a new level } if(currentState==GAME_OVER){ //Stop all the game objects updates. Set the state to Pause. Wait for user input. } } }
So we have a simple base on which we can add our functions for initializing, updating, rendering etc our level.
Next Tutorial:
In our texture tutorial we would discuss more about the initialization part of the game.
Let us know any of your doubts/suggestions; please put them in the comments section and we would try to answer them.
Thank you for the patience.