Coverage Summary for Class: QuestManager (com.mygdx.game.Managers)
| Class | Class, % | Method, % | Line, % |
|---|---|---|---|
| QuestManager | 100% (1/1) | 100% (12/12) | 92.3% (60/65) |
1 package com.mygdx.game.Managers; 2 3 import com.badlogic.gdx.math.Vector2; 4 import com.mygdx.game.Entitys.Chest; 5 import com.mygdx.game.Entitys.College; 6 import com.mygdx.game.Entitys.Player; 7 import com.mygdx.game.Quests.KillQuest; 8 import com.mygdx.game.Quests.LocateQuest; 9 import com.mygdx.game.Quests.Quest; 10 import com.mygdx.utils.Utilities; 11 12 import java.util.ArrayList; 13 import java.util.Random; 14 15 import static com.mygdx.utils.Constants.TILE_SIZE; 16 17 /** 18 * Creates the quests and manages their completion and order 19 */ 20 public class QuestManager { 21 private static boolean initialized = false; 22 private static ArrayList<Quest> allQuests; 23 private static Chest chest; 24 public static int numCompleted; 25 public static void Initialize() { 26 initialized = true; 27 allQuests = new ArrayList<>(); 28 chest = new Chest(); 29 30 createRandomQuests(); 31 } 32 33 /** 34 * Creates a random kill quest on a random college 35 * 36 * @param exclude the id of factions to not kill 37 * @return the id of the faction targeted 38 */ 39 private static int rndKillQuest(ArrayList<Integer> exclude) { 40 int id; 41 College enemy; 42 int i = 0; 43 do { 44 id = new Random().nextInt(4) + 2; 45 enemy = GameManager.getCollege(id); 46 i++; 47 } 48 while (Utilities.contains(exclude, id) && i < 5); 49 if (i == 5) { 50 return 0; 51 } 52 addQuest(new KillQuest(enemy)); 53 return id; 54 } 55 56 /** 57 * Creates a locate quest for a random position sourced from game settings 58 */ 59 private static void rndLocateQuest() { 60 final ArrayList<Float> locations = new ArrayList<>(); 61 for (float f : GameManager.getSettings().get("quests").get("locations").asFloatArray()) { 62 locations.add(f); 63 } 64 // in game settings the positions are stored as ints with y following x it doesnt wrap 65 // eg. a, b, c, d 66 // v1: (a, b) v2: (c, d) 67 Integer choice = -1; 68 float x = Utilities.randomChoice(locations, choice); 69 float y; 70 //assert (choice > -1); 71 if (choice == locations.size() - 1) { 72 y = x; 73 x = locations.get(choice - 1); 74 } else { 75 y = locations.get(choice + 1); 76 } 77 x *= TILE_SIZE; 78 y *= TILE_SIZE; 79 addQuest(new LocateQuest(new Vector2(x, y), 5 * TILE_SIZE)); 80 } 81 82 /** 83 * 50/50 chance of kill quest or locate quest 84 * 85 * @param exclude list of factions to exclude from killing 86 */ 87 private static void rndQuest(ArrayList<Integer> exclude) { 88 if (new Random().nextFloat() > 0.5) { 89 rndLocateQuest(); 90 } else { 91 exclude.add(rndKillQuest(exclude)); 92 } 93 } 94 95 /** 96 * Creates the quest line with the final quest being to kill a college 97 */ 98 private static void createRandomQuests() { 99 // the last quest added is the final quest 100 int primaryEnemyId = new Random().nextInt(4) + 2; 101 ArrayList<Integer> exclude = new ArrayList<>(); 102 exclude.add(primaryEnemyId); 103 for (int i = 0; i < GameManager.getSettings().get("quests").getInt("count"); i++) { 104 rndQuest(exclude); 105 } 106 College enemy = GameManager.getCollege(primaryEnemyId); 107 addQuest(new KillQuest(enemy)); 108 } 109 110 public static void addQuest(Quest q) { 111 tryInit(); 112 allQuests.add(q); 113 } 114 115 /** 116 * checks quests for completion and gives rewards, teleports the chest when appropriate. 117 * Stops checking the quest after the first no completed quest (prevents quests being completed in any order) 118 */ 119 public static void checkCompleted() { 120 tryInit(); 121 Player p = GameManager.getPlayer(); 122 for (Quest q : allQuests) { 123 if (q.isCompleted()) { 124 continue; 125 } 126 boolean completed = q.checkCompleted(p); 127 if (completed) { 128 p.plunder(q.getReward()); 129 p.setReward_powerUp(true); 130 numCompleted ++; 131 } else if (q instanceof LocateQuest) { 132 chest.setPosition(((LocateQuest) q).getLocation()); 133 break; 134 } else { 135 chest.setPosition(new Vector2(-1000, -1000)); 136 break; 137 } 138 } 139 } 140 141 private static void tryInit() { 142 if (!initialized) { 143 Initialize(); 144 } 145 } 146 147 /** 148 * Returns the next un-completed quest 149 * 150 * @return the quest null if no un-completed quests found 151 */ 152 public static Quest currentQuest() { 153 tryInit(); 154 for (Quest q : allQuests) { 155 if (!q.isCompleted()) { 156 return q; 157 } 158 } 159 return null; 160 } 161 162 /** 163 * Are there any quests 164 * 165 * @return true if any non completed quest exirs 166 */ 167 public static boolean anyQuests() { 168 tryInit(); 169 return currentQuest() != null; 170 } 171 }