Coverage Summary for Class: GameManager (com.mygdx.game.Managers)

Class Class, % Method, % Line, %
GameManager 100% (1/1) 89.5% (17/19) 96.9% (93/96)


1 package com.mygdx.game.Managers; 2  3 import com.badlogic.gdx.Gdx; 4 import com.badlogic.gdx.math.Vector2; 5 import com.badlogic.gdx.utils.JsonReader; 6 import com.badlogic.gdx.utils.JsonValue; 7 import com.mygdx.game.AI.TileMapGraph; 8 import com.mygdx.game.Components.Transform; 9 import com.mygdx.game.Entitys.*; 10 import com.mygdx.game.Faction; 11 import com.mygdx.game.PowerUps.PowerUp; 12 import com.mygdx.utils.Utilities; 13  14 import java.util.ArrayList; 15 import java.util.Objects; 16 import java.util.Random; 17  18 /** 19  * Responsible for creating most entity's associated with the game. Also, the cached chest and cannonballs 20  */ 21 public final class GameManager { 22  private static ArrayList<Faction> factions; 23  private static ArrayList<Ship> ships; 24  private static ArrayList<College> colleges; 25  26  private static final int cacheSize = 20; 27  private static ArrayList<CannonBall> ballCache; 28  private static int currentElement; 29  30  private static JsonValue settings; 31  32  /** 33  * facilitates creation of the game 34  * @param difficulty contains the ENUM for the difficulty that has been selected 35  */ 36  public static void Initialize(GameDifficulty difficulty) { 37  currentElement = 0; 38  // start of change for assessment 2, adds functionality for changing difficulty 39  40  changeDifficulty(difficulty.toString()); 41  // end of change 42  43  factions = new ArrayList<>(); 44  ships = new ArrayList<>(); 45  ballCache = new ArrayList<>(cacheSize); 46  colleges = new ArrayList<>(); 47  48  for (int i = 0; i < cacheSize; i++) { 49  ballCache.add(new CannonBall()); 50  } 51  52  for (JsonValue v : settings.get("factions")) { 53  String name = v.getString("name"); 54  String col = v.getString("colour"); 55  Vector2 pos = new Vector2(v.get("position").getFloat("x"), v.get("position").getFloat("y")); 56  pos = Utilities.tilesToDistance(pos); 57  Vector2 spawn = new Vector2(v.get("shipSpawn").getFloat("x"), v.get("shipSpawn").getFloat("y")); 58  spawn = Utilities.tilesToDistance(spawn); 59  factions.add(new Faction(name, col, pos, spawn, factions.size() + 1)); 60  } 61  } 62  63  /** 64  * Added for assessment 2 65  * loads the part of the json file for the chosen difficulty and overwrites the settings values with these values 66  * @param difficulty the chosen difficulty as a string 67  */ 68  public static void changeDifficulty(String difficulty){ 69  JsonValue settingsAll = new JsonReader(). //change for assessment 2 for multiple difficulties 70  parse(Gdx.files.internal("GameSettings.json")); 71  settings = settingsAll.get("Regular"); 72  73  if (!Objects.equals(difficulty, "Regular")) { 74  JsonValue editSet = settingsAll.get(difficulty); 75  for (JsonValue x : editSet) { 76  for (JsonValue value : x) { 77  System.out.println(value); 78  settings.get(x.name).get(value.name).set(value.asDouble(), null); 79  } 80  } 81  } 82  } 83  84  /** 85  * called every frame checks id the quests are completed 86  */ 87  public static void update() { 88  QuestManager.checkCompleted(); 89  } 90  91  /** 92  * Player is always in ships at index 0 93  * 94  * @return the ship 95  */ 96  public static Player getPlayer() { 97  return (Player) ships.get(0); 98  } 99  100  /** 101  * Get an NPCShip in the current game 102  * @param id the ship's ID 103  * @return the NPCShip instance 104  */ 105  public static NPCShip getNPCShip(int id) { 106  107  return (NPCShip) ships.get(id); 108  } 109  110  /** 111  * Creates the game with player maps, NPCs, colleges 112  * // Change for Assessment 2 Start // 113  * Only creates world map if mapId is non-negative 114  * For test purposes 115  * // Change for Assessment 2 End // 116  * @param mapId the resource id of the tilemap 117  */ 118  public static void SpawnGame(int mapId) { 119  120  if (mapId >= 0) CreateWorldMap(mapId); 121  CreatePlayer(); 122  final int cnt = settings.get("factionDefaults").getInt("shipCount"); 123  for (int i = 0; i < factions.size(); i++) { 124  CreateCollege(i + 1); 125  for (int j = 0; j < cnt; j++) { 126  // prevents halifax from having ship count + player 127  if (i == 0 && j > cnt - 2) { 128  break; 129  } 130  NPCShip s = CreateNPCShip(i + 1); 131  s.getComponent(Transform.class).setPosition(getFaction(i + 1).getSpawnPos()); 132  } 133  } 134  QuestManager.Initialize(); // added for assessment 2 to stop tryInit being used (testing) 135  QuestManager.createRandomQuests(); // "" 136  137  // Assessment 2 change: spawns in powerups based on settings 138  for (JsonValue powData : settings.get("powerups")) { 139  String texName = powData.getString("sprite"); 140  int cooldown = powData.getInt("spawnCooldown"); 141  PowerUp pow = new PowerUp(powData); 142  for (JsonValue pos : powData.get("positions")) { 143  Vector2 position = new Vector2(pos.getFloat(0), pos.getFloat(1)); 144  new PowerUpPickup(pow, texName, Utilities.tilesToDistance(position), cooldown); 145  } 146  } 147  148  // Assessment 2 change: spawns in obstacles based on settings 149  JsonValue obstaclesData = settings.get("obstacles"); 150  JsonValue types = obstaclesData.get("types"); 151  152  for (JsonValue pos : obstaclesData.get("positions")) { 153  Vector2 position = new Vector2(pos.getFloat(0), pos.getFloat(1)); 154  JsonValue obstacle = types.get(new Random().nextInt(types.size)); 155  156  if (Objects.equals(obstacle.getString("name"), "storm")) { 157  new Weather( 158  obstacle.getFloat("damage"), 159  obstacle.getFloat("rate", -1) 160  ).getComponent(Transform.class).setPosition(Utilities.tilesToDistance(position)); 161  } else { 162  new Obstacle( 163  obstacle.getString("name"), 164  obstacle.getBoolean("trigger"), 165  obstacle.getFloat("damage"), 166  obstacle.getFloat("rate", -1), 167  obstacle.getInt("limit", -1) 168  ).getComponent(Transform.class).setPosition(Utilities.tilesToDistance(position)); 169  } 170  } 171  } 172  173  /** 174  * Creates player that belongs the faction with id 1 175  */ 176  public static void CreatePlayer() { 177  Player p = new Player(); 178  p.setFaction(1); 179  ships.add(p); 180  } 181  182  /** 183  * Creates an NPC ship with the given faction 184  * 185  * @param factionId desired faction 186  * @return the created ship 187  */ 188  public static NPCShip CreateNPCShip(int factionId) { 189  NPCShip e = new NPCShip(); 190  e.setFaction(factionId); 191  ships.add(e); 192  return e; 193  } 194  195  /** 196  * Creates the world map 197  * 198  * @param mapId resource id 199  */ 200  public static void CreateWorldMap(int mapId) { 201  WorldMap map = new WorldMap(mapId); 202  new TileMapGraph(map.getTileMap()); 203  } 204  205  /** 206  * Creates the college with it's building for the desired college 207  * 208  * @param factionId desired faction 209  */ 210  public static void CreateCollege(int factionId) { 211  College c = new College(factionId); 212  colleges.add(c); 213  } 214  215  216  public static Faction getFaction(int factionId) { 217  return factions.get(factionId - 1); 218  } 219  220  /** 221  * Gets the setting object from the GameSetting.json 222  * @return the JSON representation fo settings 223  */ 224  public static JsonValue getSettings() { 225  return settings; 226  } 227  228  public static College getCollege(int factionId) { 229  return colleges.get(factionId - 1); 230  } 231  232  /** 233  * Added for Assessment 2 234  * @return Returns the next cannonball that would be fired from the cache 235  */ 236  public static CannonBall getCurrentCannon() { 237  return ballCache.get(currentElement); 238  } 239  240  /** 241  * Utilises the cached cannonballs to fire one 242  * Changed for Assessment 2, separated incrementer for visual clarity and parameterised startPos 243  * @param p parent 244  * @param pos position projectile is spawned at 245  * @param dir shoot direction 246  */ 247  public static void shoot(Entity p, Vector2 pos, Vector2 dir) { // Changed for Assessment 2, type switched from Ship to Entity 248  ballCache.get(currentElement).fire(p,pos, dir); 249  currentElement++; 250  currentElement %= cacheSize; 251  } 252  253  public static ArrayList<Ship> getShips(){ 254  return ships; 255  } 256  public static ArrayList<College> getColleges(){ 257  return colleges; 258  } 259  260  public static void dispose(){ 261  currentElement = 0; 262  263  factions = new ArrayList<>(); 264  ships = new ArrayList<>(); 265  ballCache = new ArrayList<>(cacheSize); 266  colleges = new ArrayList<>(); 267  } 268  269  //public static QueueFIFO<Vector2> getPath(Vector2 loc, Vector2 dst) { 270  // return mapGraph.findOptimisedPath(loc, dst); 271  //} 272 }