Coverage Summary for Class: PowerUp (com.mygdx.game.PowerUps)
Class |
Method, %
|
Line, %
|
PowerUp |
88.9%
(8/9)
|
97.6%
(40/41)
|
PowerUp$1 |
100%
(1/1)
|
100%
(1/1)
|
Total |
90%
(9/10)
|
97.6%
(41/42)
|
1 package com.mygdx.game.PowerUps;
2
3 import com.badlogic.gdx.utils.JsonValue;
4 import com.badlogic.gdx.utils.TimeUtils;
5 import com.mygdx.game.Components.Pirate;
6 import com.mygdx.game.Components.PowerUpAssigned;
7 import com.mygdx.game.Managers.GameManager;
8
9 /**
10 * // Assessment 2 requirement //
11 * A PowerUp that can modify the value of any Ship for a limited or permanent amount of time.
12 */
13 public class PowerUp {
14
15 private String name;
16 private final int cost;
17 private final String key;
18 private final PowerUpOperation oper;
19 private final float value;
20 private final long duration;
21 private long startTime;
22 private boolean done;
23
24 /**
25 * Create a PowerUp manually, used for testing.
26 * @param key The key of the value to affect
27 * @param operation The way the key is applied to the value
28 * @param value The value that is applied to the key
29 * @param cost The cost of the powerup
30 */
31 public PowerUp(String key, PowerUpOperation operation, float value, int cost, int duration) {
32 this.key = key;
33 this.oper = operation;
34 this.value = value;
35 this.duration = duration;
36 this.done = false;
37 this.cost = cost ;
38 }
39
40 /**
41 * Create a PowerUp from the settings file
42 * @param settings the json file containing the settings for the power up
43 */
44 public PowerUp(JsonValue settings) {
45 this.key = settings.getString("key");
46 this.oper = PowerUpOperation.values()[settings.getInt("operation")];
47 this.value = settings.getInt("value");
48 this.duration = settings.getInt("duration");
49 this.done = false;
50 this.cost = settings.getInt("cost") ;
51 this.name = settings.getString("name");
52 }
53
54
55 /**
56 * Apply PowerUp modifiers to the Pirate.
57 * @param pirate The Pirate to apply the PowerUp to
58 */
59 public void EnablePowerUp(Pirate pirate) {
60 switch (oper){
61 case replace:
62 pirate.setValue(key, value);
63 break;
64 case increment:
65 pirate.setValue(key, pirate.getValue(key) + value);
66 break;
67 case decrement:
68 pirate.setValue(key, pirate.getValue(key) - value);
69 break;
70 case multiply:
71 pirate.multValue(key, value);
72 break;
73 case divide:
74 pirate.multValue(key, 1f/value);
75 break;
76 }
77 startTime = TimeUtils.millis();
78 }
79
80 /**
81 * Check whether the PowerUp is permanent or temporary.
82 * @return True if the PowerUp is permanent
83 */
84 public boolean CheckPermanent() {
85 return (duration <= 0);
86 }
87
88 /**
89 * Check whether a PowerUp is done.
90 * @return Whether the PowerUp is done or not
91 */
92 public boolean CheckPowerUpDone() {
93 return CheckPowerUpDone(null);
94 }
95
96 /**
97 * Check whether a PowerUp is done, and Disable it if so.
98 * @param pirate The Pirate class to disable the PowerUp for
99 * @return Whether the PowerUp is done or not
100 */
101 public boolean CheckPowerUpDone(Pirate pirate) {
102 if (done) return true;
103 if (duration > 0) {
104 long timeSoFar = TimeUtils.timeSinceMillis(startTime) / 1000;
105 if (timeSoFar >= duration) {
106 if (pirate != null) DisablePowerUp(pirate);
107 return true;
108 }
109 } return false;
110 }
111
112 /**
113 * Remove PowerUp modifiers from a Pirate and reset to default.
114 * @param pirate The Pirate to return to normal
115 */
116 public void DisablePowerUp(Pirate pirate) {
117 if (duration > 0) pirate.resetToDefault(key);
118 done = true;
119 }
120
121 /**
122 * Removes plunder from the player in order to apply the power-up to themselves
123 */
124 public void buyPowerUp(){
125 if (GameManager.getPlayer().getPlunder() >= this.cost) {
126 GameManager.getPlayer().plunder((-this.cost));
127 GameManager.getPlayer().getComponent(PowerUpAssigned.class).AssignPowerUp(this);
128 }
129 }
130
131 /**
132 * @return the name of the power-up
133 */
134 public String getName() {
135 return name;
136 }
137 }