package com.jonathan.survivor.hud; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.GL10; import com.badlogic.gdx.scenes.scene2d.InputEvent; import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.ui.Image; import com.badlogic.gdx.scenes.scene2d.ui.Label; import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle; import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; import com.jonathan.survivor.World; /* * An instance of this class will display the HUD whilst in EXPLORATION mode. */ public class GameOverHud extends Hud { /** Holds the color which overlays the screen below the GameOverHud. */ public static final Color OVERLAY_COLOR = new Color(Color.BLACK); /** Stores how long the GameOverHud is displayed for before being brought back to the main menu. */ public static final float DISPLAY_TIME = 3; /** Stores the text which displays "Game Over". */ private Label gameOverLabel; /** Stores the listener used to listen for events from the arrow buttons. */ private ButtonListener buttonListener; /** Holds the amount of time the GameOverHud has been displayed. Once a certain time period elapses, the player is redirected to the MainMenu. */ private float playTime; /** Accepts the stage where 2d widgets will be drawn and placed, and the world, which will receive information about * button presses. */ public GameOverHud(Stage stage, World world) { super(stage, world); //Creates the "Game Over" label from a pre-defined LabelStyle. gameOverLabel = new Label("Game Over", assets.gameOverLabelStyle); //Creates a new listener for the buttons buttonListener = new ButtonListener(); } @Override public void draw(float deltaTime) { //Clears the screen with the overlay color so that the game is hidden behind the game over HUD. Gdx.gl.glClearColor(OVERLAY_COLOR.r, OVERLAY_COLOR.g, OVERLAY_COLOR.b, OVERLAY_COLOR.a); Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); //Draws the widgets to the screen. super.draw(deltaTime); //Increments the playTime of the GameOverHud to keep track of how long the HUD has been showing. playTime += deltaTime; //Checks if the GameOverHud has been displaying for long enough. If so, the player is brought back to the main menu. checkDone(); } /** Called to check if the GameOverHud has been displaying for long enough. If so, the player is brought back to the main menu. */ private void checkDone() { //If the GameOverHud has been showing for longer than DISPLAY_TIME if(playTime > DISPLAY_TIME) { //Informs the GameScreen that the GameOverHud has finished displaying, and the player is dead. hudListener.gameOverHudFinished(); } } class ButtonListener extends ClickListener { /** Delegates when a button is pressed. */ @Override public void clicked(InputEvent event, float x, float y) { } } /** Called when the stage must be reset to draw the widgets contained in this class. Used when the stage needs to be re-purposed. Also called when the * screen is resized to re-place the widgets. */ @Override public void reset(float guiWidth, float guiHeight) { //Clears the stage and all its widgets to re-purpose the stage to draw the exploration HUD. stage.clear(); //Sets the bottom-left position of the gameOverLabel so that the label is displayed at the center of the stage. gameOverLabel.setPosition(stage.getWidth()/2 - gameOverLabel.getWidth()/2, stage.getHeight()/2 - gameOverLabel.getHeight()/2); //Adds the "Game Over" label to the stage. stage.addActor(gameOverLabel); } }