Coverage Summary for Class: GUI (com.mozarellabytes.kroy.Utilities)

Class Class, % Method, % Line, %
GUI 0% (0/ 1) 0% (0/ 23) 0% (0/ 142)


1 package com.mozarellabytes.kroy.Utilities; 2  3 import com.badlogic.gdx.Gdx; 4 import com.badlogic.gdx.graphics.Color; 5 import com.badlogic.gdx.graphics.GL20; 6 import com.badlogic.gdx.graphics.OrthographicCamera; 7 import com.badlogic.gdx.graphics.Texture; 8 import com.badlogic.gdx.graphics.g2d.GlyphLayout; 9 import com.badlogic.gdx.graphics.glutils.ShapeRenderer; 10 import com.badlogic.gdx.math.Rectangle; 11 import com.mozarellabytes.kroy.Entities.FireTruck; 12 import com.mozarellabytes.kroy.Entities.Fortress; 13 import com.mozarellabytes.kroy.Kroy; 14 import com.mozarellabytes.kroy.Screens.GameScreen; 15  16 //import javax.jnlp.FileContents; 17  18 /** 19  * This Class is responsible for displaying the majority of the GUI that the 20  * user can see and interact with that are apart from the main function of 21  * the game i.e. drawing the FireTruck's path. The GUI renders the buttons 22  * visible in the top right corner whilst playing the game, along with 23  * rendering the stats area in the top left corner when an entity is selected 24  * by clicking on it on the map 25  */ 26 public class GUI { 27  28  /** LibGdx game */ 29  private final Kroy game; 30  31  /** Coordinates and dimensions of the stats box */ 32  private final int selectedX, selectedY, selectedH, selectedW; 33  34  /** The screen where the buttons are rendered */ 35  private final GameScreen gameScreen; 36  37  /** Rectangle containing the homeButton's coordinates, height and width */ 38  private final Rectangle homeButton; 39  /** Texture of the homeButton when it is not being clicked on */ 40  private final Texture homeButtonIdle; 41  /** Texture of the homeButton when it's being clicked */ 42  private final Texture homeButtonClicked; 43  /** Texture of the homeButton that is rendered to the screen */ 44  private Texture currentHomeTexture; 45  46  /** Rectangle containing the pauseButton's coordinates, height and width */ 47  private final Rectangle pauseButton; 48  /** Texture of the pausebutton when it is not being clicked on */ 49  private final Texture pauseButtonIdle; 50  /** Texture of the pauseButton when it's being clicked */ 51  private final Texture pauseButtonClicked; 52  /** Texture of the pauseButton that is rendered to the screen */ 53  private Texture currentPauseTexture; 54  55  /** Rectangle containing the infoButton's coordinates, height and width */ 56  private final Rectangle infoButton; 57  /** Texture of the infoButton when it is not being clicked on */ 58  private final Texture infoButtonIdle; 59  /** Texture of the infoButton when it's being clicked */ 60  private final Texture infoButtonClicked; 61  /** Texture of the infoButton that is rendered to the screen */ 62  private Texture currentInfoTexture; 63  64  /** Rectangle containing the soundButton's coordinates, height and width */ 65  private final Rectangle soundButton; 66  /** Texture of the soundButton when music is off to turn the music on 67  * when it is not being clicked */ 68  private final Texture soundOnIdleTexture; 69  /** Texture of the soundButton when music is on to turn the music off 70  * when it is not being clicked */ 71  private final Texture soundOffIdleTexture; 72  /** Texture of the soundButton when music is off and the soundButton is 73  * being clicked to turn the sound on*/ 74  private final Texture soundOnClickedTexture; 75  /** Texture of the soundButton when music is on and the soundButton is 76  * being clicked to turn the sound off */ 77  private final Texture soundOffClickedTexture; 78  /** Texture of the soundButton that is rendered to the screen */ 79  private Texture currentSoundTexture; 80  81  /** Camera to set the projection for the screen */ 82  private final OrthographicCamera pauseCamera; 83  84  /** Constructor for GUI 85  * 86  * @param game The Kroy game 87  * @param gameScreen Screen where these methods will be rendered 88  */ 89  public GUI(Kroy game, GameScreen gameScreen) { 90  this.game = game; 91  this.gameScreen = gameScreen; 92  this.selectedH = 275; 93  this.selectedW = 275; 94  this.selectedX = 10; 95  this.selectedY = Gdx.graphics.getHeight() - 10 - this.selectedH; 96  97  homeButtonIdle = new Texture(Gdx.files.internal("ui/home_idle.png"), true); 98  homeButtonIdle.setFilter(Texture.TextureFilter.MipMapLinearNearest, Texture.TextureFilter.MipMapLinearNearest); 99  homeButtonClicked = new Texture(Gdx.files.internal("ui/home_clicked.png"), true); 100  homeButtonClicked.setFilter(Texture.TextureFilter.MipMapLinearNearest, Texture.TextureFilter.MipMapLinearNearest); 101  102  pauseButtonIdle = new Texture(Gdx.files.internal("ui/pause_idle.png"), true); 103  pauseButtonIdle.setFilter(Texture.TextureFilter.MipMapLinearNearest, Texture.TextureFilter.MipMapLinearNearest); 104  pauseButtonClicked = new Texture(Gdx.files.internal("ui/pause_clicked.png"), true); 105  pauseButtonClicked.setFilter(Texture.TextureFilter.MipMapLinearNearest, Texture.TextureFilter.MipMapLinearNearest); 106  107  infoButtonIdle = new Texture(Gdx.files.internal("ui/info_idle.png"), true); 108  infoButtonIdle.setFilter(Texture.TextureFilter.MipMapLinearNearest, Texture.TextureFilter.MipMapLinearNearest); 109  infoButtonClicked = new Texture(Gdx.files.internal("ui/info_clicked.png"), true); 110  infoButtonClicked.setFilter(Texture.TextureFilter.MipMapLinearNearest, Texture.TextureFilter.MipMapLinearNearest); 111  112  soundOnIdleTexture = new Texture(Gdx.files.internal("ui/sound_on_idle.png"), true); 113  soundOnIdleTexture.setFilter(Texture.TextureFilter.MipMapLinearNearest, Texture.TextureFilter.MipMapLinearNearest); 114  soundOffIdleTexture = new Texture(Gdx.files.internal("ui/sound_off_idle.png"), true); 115  soundOffIdleTexture.setFilter(Texture.TextureFilter.MipMapLinearNearest, Texture.TextureFilter.MipMapLinearNearest); 116  soundOnClickedTexture = new Texture(Gdx.files.internal("ui/sound_on_clicked.png"), true); 117  soundOnClickedTexture.setFilter(Texture.TextureFilter.MipMapLinearNearest, Texture.TextureFilter.MipMapLinearNearest); 118  soundOffClickedTexture = new Texture(Gdx.files.internal("ui/sound_off_clicked.png"), true); 119  soundOffClickedTexture.setFilter(Texture.TextureFilter.MipMapLinearNearest, Texture.TextureFilter.MipMapLinearNearest); 120  121  currentHomeTexture = homeButtonIdle; 122  currentPauseTexture = pauseButtonIdle; 123  currentInfoTexture = infoButtonIdle; 124  125  if (SoundFX.music_enabled) { 126  currentSoundTexture = soundOffIdleTexture; 127  } else { 128  currentSoundTexture = soundOnIdleTexture; 129  } 130  131  homeButton = new Rectangle(Gdx.graphics.getWidth() - 33, Gdx.graphics.getHeight() - 33, 30, 30); 132  soundButton = new Rectangle(Gdx.graphics.getWidth() - 70, Gdx.graphics.getHeight() - 33, 30, 30); 133  pauseButton = new Rectangle(Gdx.graphics.getWidth() - 107, Gdx.graphics.getHeight() - 33, 30, 30); 134  infoButton = new Rectangle(Gdx.graphics.getWidth() - 144, Gdx.graphics.getHeight() - 33, 30, 30); 135  136  pauseCamera = new OrthographicCamera(); 137  pauseCamera.setToOrtho(false, Gdx.graphics.getDisplayMode().width, Gdx.graphics.getDisplayMode().height); 138  } 139  140  /** 141  * Renders the health and (when applicable) reserve bars 142  * along with the custom attributes that the entity 143  * possesses 144  * 145  * @param entity The entity that has been clicked on 146  */ 147  public void renderSelectedEntity(Object entity) { 148  if (entity != null) { 149  Gdx.graphics.getGL20().glEnable(GL20.GL_BLEND); 150  Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA); 151  game.shapeRenderer.begin(ShapeRenderer.ShapeType.Filled); 152  renderSelectedEntityBackground(); 153  game.shapeRenderer.end(); 154  game.shapeRenderer.begin(ShapeRenderer.ShapeType.Filled); 155  if (entity instanceof FireTruck) { 156  FireTruck truck = (FireTruck) entity; 157  renderSelectedTruck(truck); 158  } else if (entity instanceof Fortress) { 159  Fortress fortress = (Fortress) entity; 160  renderSelectedFortress(fortress); 161  } 162  game.shapeRenderer.end(); 163  } 164  } 165  166  /** 167  * Renders the dark background behind the stats area 168  */ 169  private void renderSelectedEntityBackground() { 170  game.shapeRenderer.setColor(0, 0, 0, 0.5f); 171  game.shapeRenderer.rect(selectedX, selectedY, selectedW, selectedH); 172  } 173  174  /** 175  * Calls the methods which render the attributes and 176  * health/reserve bars of a truck in the stats area 177  * 178  * @param truck the FireTruck that owns the stats 179  * that are being displayed 180  */ 181  private void renderSelectedTruck(FireTruck truck) { 182  renderSelectedEntityBar(truck.getHP(), truck.getType().getMaxHP(), Color.RED, Color.FIREBRICK, 1); 183  renderSelectedEntityBar(truck.getReserve(), truck.getType().getMaxReserve(), Color.CYAN, Color.BLUE, 2); 184  renderSelectedEntityText(truck); 185  } 186  187  /** 188  * Calls the methods which render the attributes and 189  * health bar of a fortress in the stats area 190  * 191  * @param fortress the Fortress that owns the stats 192  * thta are being displayed 193  */ 194  private void renderSelectedFortress(Fortress fortress) { 195  renderSelectedEntityBar(fortress.getHP(), fortress.getFortressType().getMaxHP(), Color.RED, Color.FIREBRICK, 1); 196  renderSelectedEntityText(fortress); 197  } 198  199  /** 200  * Renders the attributes in a vertical layout 201  * of the FireTruck 202  * 203  * @param truck the FireTruck that owns the stats 204  * that are being displayed 205  */ 206  private void renderSelectedEntityText(FireTruck truck) { 207  int newLine = 20; 208  game.batch.begin(); 209  game.font26.draw(game.batch, truck.getType().getName(), this.selectedX + 10, this.selectedY + this.selectedH - 10); 210  game.font19.draw(game.batch, "HP: ", this.selectedX + 15, this.selectedY + this.selectedH - 50); 211  game.font19.draw(game.batch, String.format("%.1f", truck.getHP()) + " / " + String.format("%.1f", truck.getType().getMaxHP()), this.selectedX + 20, this.selectedY + this.selectedH - 50 - newLine); 212  game.font19.draw(game.batch, "Reserve: ", this.selectedX + 15, this.selectedY + this.selectedH - 50 - newLine*2); 213  game.font19.draw(game.batch, String.format("%.1f", truck.getReserve()) + " / " + String.format("%.1f", truck.getType().getMaxReserve()), this.selectedX + 20, this.selectedY + this.selectedH - 50 - newLine*3); 214  game.font19.draw(game.batch, "Speed: ", this.selectedX + 15, this.selectedY + this.selectedH - 50 - newLine*4); 215  game.font19.draw(game.batch, String.format("%.1f", truck.getType().getSpeed()), this.selectedX + 20, this.selectedY + this.selectedH - 50 - newLine*5); 216  game.font19.draw(game.batch, "Range: ", this.selectedX + 15, this.selectedY + this.selectedH - 50 - newLine*6); 217  game.font19.draw(game.batch, String.format("%.1f", truck.getType().getRange()), this.selectedX + 20, this.selectedY + this.selectedH - 50 - newLine*7); 218  game.font19.draw(game.batch, "AP: ", this.selectedX + 15, this.selectedY + this.selectedH - 50 - newLine*8); 219  game.font19.draw(game.batch, String.format("%.2f", truck.getType().getAP()), this.selectedX + 20, this.selectedY + this.selectedH - 50 - newLine*9); 220  game.batch.end(); 221  } 222  223  /** 224  * Renders the attributes in a vertical layout 225  * of the Fortress 226  * 227  * @param fortress the Fortress that owns the stats 228  * that are being displayed 229  */ 230  private void renderSelectedEntityText(Fortress fortress) { 231  int newLine = 20; 232  game.batch.begin(); 233  game.font26.draw(game.batch, fortress.getFortressType().getName(), this.selectedX + 10, this.selectedY + this.selectedH - 10); 234  game.font19.draw(game.batch, "HP: ", this.selectedX + 15, this.selectedY + this.selectedH - 50); 235  game.font19.draw(game.batch, String.format("%.1f", fortress.getHP()) + " / " + String.format("%.1f", fortress.getFortressType().getMaxHP()), this.selectedX + 20, this.selectedY + this.selectedH - 50 - newLine); 236  game.font19.draw(game.batch, "Range: ", this.selectedX + 15, this.selectedY + this.selectedH - 50 - newLine*2); 237  game.font19.draw(game.batch, String.format("%.1f", fortress.getFortressType().getRange()), this.selectedX + 20, this.selectedY + this.selectedH - 50 - newLine*3); 238  game.font19.draw(game.batch, "AP: ", this.selectedX + 15, this.selectedY + this.selectedH - 50 - newLine*4); 239  game.font19.draw(game.batch, String.format("%.2f", fortress.getFortressType().getAP()), this.selectedX + 20, this.selectedY + this.selectedH - 50 - newLine*5); 240  game.batch.end(); 241  } 242  243  /** 244  * Renders the stat bars which are currently used to 245  * show the health/reserve of trucks and health of 246  * fortresses. The integers inside the method that 247  * have values set to them are customisable to get 248  * the desired layout/formatting of the bars 249  * 250  * @param value the value towards the goal 251  * @param maxValue the goal 252  * @param progressColour the colour of the value bar 253  * @param backgroundColour the colour behind the value bar 254  * @param position the 'bar number' to allow multiple 255  * bars along side each other 256  * (1 to infinity) 257  */ 258  private void renderSelectedEntityBar(float value, float maxValue, Color progressColour, Color backgroundColour, int position) { 259  int whiteW = 50; 260  int outerSpacing = 10; 261  int innerSpacing = 5; 262  int spaceForText = 35; 263  int barHeight = this.selectedH - outerSpacing*2 - innerSpacing*2 - spaceForText; 264  int positionSpacer = position * whiteW; 265  int barSpacer = 0; 266  if (position > 1) barSpacer = 5; 267  game.shapeRenderer.rect(this.selectedX + this.selectedW - positionSpacer - outerSpacing - barSpacer, this.selectedY + outerSpacing, whiteW, this.selectedH - outerSpacing*2 - spaceForText, Color.WHITE, Color.WHITE, Color.WHITE, Color.WHITE); 268  game.shapeRenderer.rect(this.selectedX + this.selectedW - positionSpacer - outerSpacing + innerSpacing - barSpacer, this.selectedY + outerSpacing + innerSpacing, whiteW - innerSpacing*2, barHeight, backgroundColour, backgroundColour, backgroundColour, backgroundColour); 269  game.shapeRenderer.rect(this.selectedX + this.selectedW - positionSpacer - outerSpacing + innerSpacing - barSpacer, this.selectedY + outerSpacing + innerSpacing, whiteW - innerSpacing*2, value/maxValue*barHeight, progressColour, progressColour, progressColour, progressColour); 270  } 271  272  /** Renders the buttons to the game screen */ 273  public void renderButtons() { 274  game.batch.begin(); 275  game.batch.draw(currentSoundTexture, soundButton.x, soundButton.y, soundButton.width, soundButton.height); 276  game.batch.draw(currentHomeTexture, homeButton.x, homeButton.y, homeButton.width, homeButton.height); 277  game.batch.draw(currentPauseTexture, pauseButton.x, pauseButton.y, pauseButton.width, pauseButton.height); 278  game.batch.draw(currentInfoTexture, infoButton.x, infoButton.y, infoButton.width, infoButton.height); 279  game.batch.end(); 280  } 281  282  /** Sets the homeButton texture to homeButtonClicked while the homeButton 283  * is being clicked on */ 284  public void clickedHomeButton() { 285  if (SoundFX.music_enabled){ 286  SoundFX.sfx_button_clicked.play(); 287  } 288  currentHomeTexture = homeButtonClicked; 289  } 290  291  /** Sets the infoButton texture to "Idle" if the previous was "Clicked", 292  * else it sets it to "Clicked" */ 293  public void clickedInfoButton() { 294  if (currentInfoTexture == infoButtonIdle) { 295  currentInfoTexture = infoButtonClicked; 296  } else { 297  currentInfoTexture = infoButtonIdle; 298  } 299  } 300  301  /** Sets the soundButton texture to either soundOffClickedTexture or 302  * soundOnClickedTexture while the soundButton is being clicked on */ 303  public void clickedSoundButton() { 304  if (SoundFX.music_enabled){ 305  currentSoundTexture = soundOffClickedTexture; 306  } else { 307  currentSoundTexture = soundOnClickedTexture; 308  } 309  } 310  311  /** Sets the pauseButton texture that is rendered to the screen and pauses 312  * and unpauses the game */ 313  public void clickedPauseButton() { 314  315  if (gameScreen.getState().equals(GameScreen.PlayState.PLAY)) { 316  currentPauseTexture = pauseButtonClicked; 317  if (SoundFX.music_enabled) { 318  SoundFX.sfx_pause.play(); 319  } 320  } else { 321  currentPauseTexture = pauseButtonIdle; 322  if (SoundFX.music_enabled) { 323  SoundFX.sfx_unpause.play(); 324  } 325  } 326  } 327  328  /** Sets the homeButton texture that is rendered to the screen */ 329  public void idleHomeButton() { 330  currentHomeTexture = homeButtonIdle; 331  } 332  333  /** Sets the pauseButton texture that is rendered to the screen */ 334  public void idlePauseButton() { 335  currentPauseTexture = pauseButtonIdle; 336  } 337  338  public void idleInfoButton() { 339  currentInfoTexture = infoButtonIdle; 340  } 341  342  /** Sets the soundButton texture that is rendered to the screen */ 343  public void idleSoundButton() { 344  if (SoundFX.music_enabled){ 345  currentSoundTexture = soundOffIdleTexture; 346  } else { 347  currentSoundTexture = soundOnIdleTexture; 348  } 349  } 350  351  /** Toggles the sound, called if 'S' key or the sound button 352  * is pressed */ 353  public void changeSound() { 354  if (SoundFX.music_enabled){ 355  currentSoundTexture = soundOnIdleTexture; 356  SoundFX.stopMusic(); 357  } else { 358  currentSoundTexture = soundOffIdleTexture; 359  SoundFX.playGameMusic(); 360  } 361  } 362  363  /** Renders the text to the screen when the game is paused */ 364  public void renderPauseScreenText() { 365  GlyphLayout layout = new GlyphLayout(); 366  String pauseText1 = "Game paused \n"; 367  String pauseText2 = "Press 'P' or the Pause button \n To return to game"; 368  layout.setText(game.font26, pauseText1); 369  layout.setText(game.font26, pauseText2); 370  371  game.batch.setProjectionMatrix(pauseCamera.combined); 372  game.batch.begin(); 373  game.font50.draw(game.batch, pauseText1, pauseCamera.viewportWidth/2 - layout.width/2.7f, pauseCamera.viewportHeight/1.8f - layout.height/2); 374  game.font26.draw(game.batch, pauseText2, pauseCamera.viewportWidth/2 - layout.width/2, pauseCamera.viewportHeight/2.3f - layout.height/2); 375  game.batch.end(); 376  } 377  378  public Rectangle getHomeButton() { return this.homeButton; } 379  380  public Rectangle getSoundButton() { return this.soundButton; } 381  382  public Rectangle getPauseButton() { return this.pauseButton; } 383  384  public Rectangle getInfoButton() { return this.infoButton; } 385  386 }