Coverage Summary for Class: GameManager (com.mygdx.game.Managers)
| Class | Class, % | Method, % | Line, % |
|---|---|---|---|
| GameManager | 100% (1/1) | 66.7% (14/21) | 79.5% (89/112) |
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.utils.QueueFIFO; 12 import com.mygdx.utils.Utilities; 13 14 import java.util.ArrayList; 15 16 /** 17 * Responsible for creating most entity's associated with the game. Also the cached chest and cannonballs 18 */ 19 public final class GameManager { 20 private static boolean initialized = false; 21 private static ArrayList<Faction> factions; 22 public static ArrayList<Ship> ships; 23 public static ArrayList<College> colleges; 24 public static ArrayList<Weather> weathers; 25 public static ArrayList<Monster> monsters; 26 27 private static final int cacheSize = 50; 28 private static ArrayList<CannonBall> ballCache; 29 private static ArrayList<CannonBallCollege> ballCache2; 30 private static int currentElement; 31 32 private static JsonValue settings; 33 34 private static TileMapGraph mapGraph; 35 36 /** 37 * facilitates creation of the game 38 */ 39 public static void Initialize() { 40 initialized = true; 41 currentElement = 0; 42 settings = new JsonReader(). 43 parse(Gdx.files.internal("GameSettings.json")); 44 45 factions = new ArrayList<>(); 46 ships = new ArrayList<>(); 47 ballCache = new ArrayList<>(cacheSize); 48 ballCache2 = new ArrayList<>(cacheSize); 49 colleges = new ArrayList<>(); 50 weathers = new ArrayList<>(); 51 monsters = new ArrayList<>(); 52 53 for (int i = 0; i < cacheSize; i++) { 54 ballCache.add(new CannonBall()); 55 } 56 57 for (int i = 0; i < cacheSize; i++) { 58 ballCache2.add(new CannonBallCollege()); 59 } 60 61 for (JsonValue v : settings.get("factions")) { 62 String name = v.getString("name"); 63 String col = v.getString("colour"); 64 Vector2 pos = new Vector2(v.get("position").getFloat("x"), v.get("position").getFloat("y")); 65 pos = Utilities.tilesToDistance(pos); 66 Vector2 spawn = new Vector2(v.get("shipSpawn").getFloat("x"), v.get("shipSpawn").getFloat("y")); 67 spawn = Utilities.tilesToDistance(spawn); 68 factions.add(new Faction(name, col, pos, spawn, factions.size() + 1)); 69 } 70 } 71 72 /** 73 * called every frame checks id the quests are completed 74 */ 75 public static void update() { 76 QuestManager.checkCompleted(); 77 } 78 79 /** 80 * Player is always in ships at index 0 81 * 82 * @return the ship 83 */ 84 public static Player getPlayer() { 85 return (Player) ships.get(0); 86 } 87 88 public static ArrayList<Ship> getShips(){ 89 return ships; 90 } 91 92 /** 93 * Creates the game with player maps, NPCs, colleges 94 * 95 * @param mapId the resource id of the tilemap 96 */ 97 public static void SpawnGame(int mapId) { 98 CreateWorldMap(mapId); 99 CreatePlayer(); 100 CreateWeather(); 101 CreateMonster(); 102 103 final int cnt = settings.get("factionDefaults").getInt("shipCount"); 104 for (int i = 0; i < factions.size(); i++) { 105 CreateCollege(i + 1); 106 for (int j = 0; j < cnt; j++) { 107 // prevents halifax from having shipcount + player 108 if (i == 0 && j > cnt - 2) { 109 break; 110 } 111 NPCShip s = CreateNPCShip(i + 1); 112 s.getComponent(Transform.class).setPosition(getFaction(i + 1).getSpawnPos()); 113 } 114 } 115 116 } 117 118 /** 119 * Creates player that belongs the faction with id 1 120 */ 121 public static void CreatePlayer() { 122 tryInit(); 123 Player p = new Player(); 124 p.setFaction(1); 125 ships.add(p); 126 } 127 128 /** 129 * Creates monster 130 */ 131 public static void CreateMonster() { 132 tryInit(); 133 Monster m = new Monster(); 134 monsters.add(m); 135 } 136 137 /** 138 * Creates weather 139 */ 140 public static Weather CreateWeather() { 141 tryInit(); 142 Weather e = new Weather(); 143 weathers.add(e); 144 return e; 145 } 146 147 /** 148 * Creates an NPC ship with the given faction 149 * 150 * @param factionId desired faction 151 * @return the created ship 152 */ 153 public static NPCShip CreateNPCShip(int factionId) { 154 tryInit(); 155 NPCShip e = new NPCShip(); 156 e.setFaction(factionId); 157 ships.add(e); 158 return e; 159 } 160 161 162 public static void addNPCMyShip(){ 163 NPCShip e = new NPCShip(); 164 e.setFaction(1); 165 ships.add(e); 166 } 167 168 /** 169 * Creates the world map 170 * 171 * @param mapId resource id 172 */ 173 public static void CreateWorldMap(int mapId) { 174 tryInit(); 175 WorldMap map = new WorldMap(mapId); 176 mapGraph = new TileMapGraph(map.getTileMap()); 177 } 178 179 /** 180 * Creates the college with it's building for the desired college 181 * 182 * @param factionId desired faction 183 */ 184 public static void CreateCollege(int factionId) { 185 tryInit(); 186 College c = new College(factionId); 187 colleges.add(c); 188 } 189 190 private static void tryInit() { 191 if (!initialized) { 192 Initialize(); 193 } 194 } 195 196 public static Faction getFaction(int factionId) { 197 tryInit(); 198 return factions.get(factionId - 1); 199 } 200 201 /** 202 * Gets the setting object from the GameSetting.json 203 * 204 * @return the JSON representation fo settings 205 */ 206 public static JsonValue getSettings() { 207 tryInit(); 208 return settings; 209 } 210 211 public static College getCollege(int factionId) { 212 tryInit(); 213 return colleges.get(factionId - 1); 214 } 215 216 /** 217 * Utilises the cached cannonballs to fire one 218 * 219 * @param p parent 220 * @param dir shoot direction 221 */ 222 public static void shoot(Ship p, Vector2 dir) { 223 Vector2 pos = p.getComponent(Transform.class).getPosition().cpy(); 224 //pos.add(dir.x * TILE_SIZE * 0.5f, dir.y * TILE_SIZE * 0.5f); 225 ballCache.get(currentElement++).fire(pos, dir, p); 226 currentElement %= cacheSize; 227 } 228 //new 229 /** 230 * Utilises the cached cannonballs to fire one 231 * 232 * @param c College that fires 233 * @param dir shoot direction 234 */ 235 public static void shoot2(College c, Vector2 dir) { 236 Vector2 pos = c.getComponent(Transform.class).getPosition().cpy(); 237 //pos.add(dir.x * TILE_SIZE * 0.5f, dir.y * TILE_SIZE * 0.5f); 238 ballCache2.get(currentElement++).fire(pos, dir, c); 239 currentElement %= cacheSize; 240 } 241 242 /** 243 * uses a* not sure if it works but i think it does 244 * 245 * @param loc src 246 * @param dst dst 247 * @return queue of delta postions 248 */ 249 public static QueueFIFO<Vector2> getPath(Vector2 loc, Vector2 dst) { 250 return mapGraph.findOptimisedPath(loc, dst); 251 } 252 }