Coverage Summary for Class: PirateGame (com.mygdx.game)
| Class | Class, % | Method, % | Line, % |
|---|---|---|---|
| PirateGame | 100% (1/1) | 30% (3/10) | 38.5% (20/52) |
1 package com.mygdx.game; 2 3 import com.badlogic.gdx.Game; 4 import com.badlogic.gdx.Gdx; 5 import com.badlogic.gdx.Screen; 6 import com.badlogic.gdx.scenes.scene2d.Stage; 7 import com.badlogic.gdx.scenes.scene2d.ui.Skin; 8 import com.badlogic.gdx.utils.viewport.ScreenViewport; 9 import com.mygdx.game.Managers.*; 10 import com.mygdx.game.UI.EndScreen; 11 import com.mygdx.game.UI.GameScreen; 12 import com.mygdx.game.UI.MenuScreen; 13 import com.mygdx.game.UI.PauseScreen; 14 15 import java.util.Objects; 16 17 /** 18 * Contains class instances of game UI screens. 19 */ 20 public class PirateGame extends Game { 21 public MenuScreen menu; 22 public GameScreen game; 23 public EndScreen end; 24 public Stage stage; 25 public Skin skin; 26 public PauseScreen pause; 27 public static int id_map; 28 public GameDifficulty difficulty = GameDifficulty.Regular; 29 30 /** 31 * Create instances of game stage and UI screens. 32 */ 33 @Override 34 public void create() { 35 RenderingManager.Initialize(); // added for assessment 2 due to rendering refactoring for testing 36 loadResources(); 37 stage = new Stage(new ScreenViewport()); 38 createSkin(); 39 40 menu = new MenuScreen(this); 41 //game = new GameScreen(this, id_map); moved to game screen for assessment 2 42 end = new EndScreen(this); 43 pause = new PauseScreen(this); 44 45 setScreen(menu); 46 } 47 48 /** 49 * Added for Assessment 2 50 * Modularized resource loading, so it can be called from tests and for code clarity 51 */ 52 public static void loadResources() { 53 ResourceManager.addTexture("ship.png"); 54 id_map = ResourceManager.addTileMap("Map.tmx"); 55 56 ResourceManager.addTextureAtlas("Boats.txt"); 57 ResourceManager.addTextureAtlas("UISkin/skin.atlas"); 58 ResourceManager.addTextureAtlas("Buildings.txt"); 59 ResourceManager.addTextureAtlas("powerups.txt"); 60 ResourceManager.addTextureAtlas("obstacles.txt"); 61 62 63 ResourceManager.addTexture("menuBG.jpg"); 64 ResourceManager.addTexture("Chest.png"); 65 ResourceManager.addTexture("questArrow.png"); 66 67 ResourceManager.loadAssets(); 68 } 69 70 /** 71 * Clean up prevent memory leeks 72 */ 73 @Override 74 public void dispose() { 75 menu.dispose(); 76 game.dispose(); 77 stage.dispose(); 78 skin.dispose(); 79 } 80 81 /** 82 * load ui skin from assets 83 */ 84 private void createSkin() { 85 skin = new Skin(Gdx.files.internal("UISkin/skin.json")); 86 } 87 88 /** 89 * New for assessment 2 90 * new version of setScreen to stop the game screen being deleted before a new screen is set if the new screen is pause. 91 * @param screen the screen to be changed to 92 */ 93 @Override 94 public void setScreen (Screen screen) { 95 if (this.screen != null && screen != pause) this.screen.hide(); 96 this.screen = screen; 97 if (this.screen != null) { 98 this.screen.show(); 99 this.screen.resize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); 100 } 101 } 102 103 /** 104 * New for assessment 2 105 * Restarts the game by reinitialising the Managers and creating a new instance of GameScreen 106 */ 107 public void restartGame(){ 108 // can't load any more resources after this point (just functionally I choose not to implement) 109 RenderingManager.Initialize(); 110 EntityManager.Initialize(); 111 GameManager.Initialize(difficulty); 112 setScreen(menu); 113 } 114 115 /** 116 * New for assessment 2 117 * Changes the difficulty for the game by changing the enum and calling GameManager.getSettings() 118 */ 119 public void setDifficulty(String selected){ 120 if (Objects.equals(selected, "Easy")){ 121 difficulty = GameDifficulty.Easy; 122 } 123 else if (Objects.equals(selected, "Regular")) { 124 difficulty = GameDifficulty.Regular; 125 } 126 else if (Objects.equals(selected, "Hard")) { 127 difficulty = GameDifficulty.Hard; 128 } 129 else{ 130 difficulty = GameDifficulty.Regular; 131 132 } 133 } 134 /** 135 Added for assessment 2 so that the game doesn't start until after the difficulty has been chosen by the player 136 */ 137 public void StartGame(){ 138 PhysicsManager.Initialize(); 139 GameManager.Initialize(difficulty); 140 game = new GameScreen(this, id_map); 141 setScreen(game); 142 } 143 144 /** 145 * New for assessment 2 146 * Loads the game from the saved data 147 */ 148 public void LoadGame(){ 149 PhysicsManager.Initialize(); 150 SaveManager.LoadGame(); 151 152 GameManager.Initialize(difficulty); 153 game = new GameScreen(this, id_map); 154 155 SaveManager.SpawnGame(); 156 157 setScreen(game); 158 } 159 } 160 161