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