Coverage Summary for Class: Pirate (com.mygdx.game.Components)
| Class | Class, % | Method, % | Line, % |
|---|---|---|---|
| Pirate | 100% (1/1) | 100% (27/27) | 100% (56/56) |
1 package com.mygdx.game.Components; 2 3 import com.badlogic.gdx.math.Vector2; 4 import com.badlogic.gdx.utils.JsonValue; 5 import com.mygdx.game.Entitys.Ship; 6 import com.mygdx.game.Faction; 7 import com.mygdx.game.Managers.GameManager; 8 import com.mygdx.utils.QueueFIFO; 9 10 import java.util.HashMap; 11 12 /** 13 * Gives the concepts of health plunder, etc. Allows for firing of cannonballs, factions, death, targets 14 */ 15 public class Pirate extends Component { 16 /** 17 * // Assessment 2 Change: Refactored to use map for storing values which will be modified by power-ups. 18 * The enemy that is being targeted by the AI. 19 */ 20 private int factionId; 21 private final HashMap<String,Float> defaults; 22 private final HashMap<String,Float> values; 23 private int points; 24 protected boolean isAlive; 25 26 private boolean infiniteAmmo; // Added for Assessment 2 for power-ups and colleges 27 private final QueueFIFO<Ship> targets; 28 //private int plunder; 29 30 /** 31 * Creates the base Pirate Component, setting up its initial values 32 */ 33 public Pirate() { 34 super(); 35 targets = new QueueFIFO<>(); 36 type = ComponentType.Pirate; 37 factionId = 1; 38 isAlive = true; 39 40 values = new HashMap<>(); 41 JsonValue starting = GameManager.getSettings().get("starting"); 42 values.put("health", (float) starting.getInt("health")); 43 values.put("damage", (float) starting.getInt("damage")); 44 values.put("ammo", (float) starting.getInt("ammo")); 45 values.put("plunder", (float) starting.getInt("plunder")); 46 47 values.put("plunderRate", 1f); 48 values.put("defense", 1f); 49 50 defaults = new HashMap<>(values); 51 } 52 53 /** 54 * // New for assessment 2 // 55 * Get a Pirate value. 56 * @param key The value to get 57 * @return The value 58 */ 59 public float getValue(String key) { 60 return values.get(key); 61 } 62 63 /** 64 * // New for assessment 2 // 65 * Set a new value for Pirate while holding reference to what it was originally. 66 * @param key The value to set to 67 * @param value The float to apply 68 */ 69 public void setValue(String key, float value) { 70 values.replace(key, value); 71 } 72 73 /** 74 * // New for assessment 2 // 75 * Multiply a value for Pirate while holding reference to what it was originally. 76 * @param key The value to multiply 77 * @param mult The multiplication factor 78 */ 79 public void multValue(String key, float mult) { 80 values.replace(key, values.get(key) * mult); 81 } 82 83 /** 84 * // New for assessment 2 // 85 * Reset a Pirate value to what it originally was. 86 * @param key The value to reset 87 */ 88 public void resetToDefault(String key) { 89 values.replace(key, defaults.get(key)); 90 } 91 92 /** 93 * @param target The Ship to be added to the list of potential targets 94 */ 95 public void addTarget(Ship target) { 96 targets.add(target); 97 } 98 99 /** 100 * @return the plunder value attached to this pirate 101 */ 102 public int getPlunder() { 103 return values.get("plunder").intValue(); 104 } 105 106 /** 107 * @return the points value attached to this pirate 108 */ 109 public int getPoints() { 110 return points; 111 } 112 113 /** 114 * @param money The integer value to be added to the pirate's stored plunder amount 115 */ 116 public void addPlunder(int money) { 117 // Assessment 2 change: Plunder additions are now multiplied by plunderRate value 118 values.replace("plunder", values.get("plunder")+ Math.round(money * values.get("plunderRate"))); 119 } 120 121 /** 122 * @param increment The integer value to be added to the pirate's stored point amount 123 */ 124 public void addPoints(int increment) { 125 points += increment; 126 } 127 128 /** 129 * @return the Faction value attached to this pirate 130 */ 131 public Faction getFaction() { 132 return GameManager.getFaction(factionId); 133 } 134 135 /** 136 * @param factionId The ID of the faction to set this pirate to 137 */ 138 public void setFactionId(int factionId) { 139 this.factionId = factionId; 140 } 141 142 /** 143 * Added for Assessment 2, sets whether the pirate can ignore ammo costs for firing 144 * @param status boolean value to set infiniteAmmo to 145 */ 146 public void setInfiniteAmmo(boolean status) { 147 infiniteAmmo = status; 148 } 149 150 /** 151 * Added for Assessment 2 152 * @return the damage dealt by attacks from this unit 153 */ 154 public Float getAttackDmg() { 155 return values.get("damage"); 156 } 157 158 /** 159 * Deducts damage taken from Pirate's health and kills the pirate if that reduces health to less than or equal to 0 160 * @param dmg The float value to be removed from this pirate's health total 161 */ 162 public void takeDamage(float dmg) { 163 // Assessment 2 change: Refactored to include key for health instead of variable, as well as factor in new defense value 164 dmg *= (1f/values.get("defense")); 165 values.replace("health", getHealth() - dmg); 166 if (getHealth() <= 0) kill(); 167 } 168 169 /** 170 * Changed for Assessment 2: 171 * - Removed functionality and replaced with function call to a different function, 172 * to allow for differentiation between when a start position is or isn't provided. 173 * @param direction the direction to shoot in 174 */ 175 public void shoot(Vector2 direction) { 176 shoot(parent.getComponent(Transform.class).getPosition().cpy(),direction); 177 } 178 179 /** 180 * Added for Assessment 2 181 * Will shoot a cannonball from the startPos in the direction, assigning this.Parent as the cannonball's parent 182 * @param direction the direction to shoot in 183 */ 184 public void shoot(Vector2 startPos, Vector2 direction){ 185 if(!infiniteAmmo && (getAmmo() == 0)){ 186 return; 187 } 188 if(!infiniteAmmo){ 189 values.replace("ammo", getAmmo()-1f); 190 } 191 GameManager.shoot(parent, startPos, direction); 192 } 193 194 /** 195 * @return the health value attached to this pirate 196 */ 197 public int getHealth() { 198 // Assessment 2 change: Refactored to use key for health instead of variable 199 return Math.round(values.get("health")); 200 } 201 202 /** 203 * if dst to target is less than attack range 204 * target will be null if not in aggro range 205 */ 206 public boolean canAttack() { 207 final Ship p = (Ship) parent; 208 final Vector2 pos = p.getPosition(); 209 // within attack range 210 if (targets.peek() != null) { 211 final float dst = pos.dst(targets.peek() != null ? targets.peek().getPosition() : null); // Assessment 2 change: Fixed to not error on null 212 return dst < Ship.getAttackRange(); 213 }else{ 214 return false; 215 } 216 } 217 218 /** 219 * if dst to target is >= attack range 220 * target will be null if not in aggro range 221 */ 222 public boolean isAggro() { 223 final Ship p = (Ship) parent; 224 final Vector2 pos = p.getPosition(); 225 // out of attack range but in aggro range 226 if (targets.peek() != null) { 227 final float dst = pos.dst(targets.peek() != null ? targets.peek().getPosition() : null); 228 return dst >= Ship.getAttackRange(); 229 }else{ 230 return false; 231 } 232 } 233 234 /** 235 * @return the current target value attached to this pirate 236 */ 237 public Ship getTarget() { 238 return targets.peek(); 239 } 240 241 /** 242 * @return the boolean alive value attached to this pirate 243 */ 244 public boolean isAlive() { 245 return isAlive; 246 } 247 248 /** 249 * Kill its self 250 */ 251 public void kill() { 252 // Assessment 2 change: Refactored to use key for health instead of variable 253 values.replace("health", 0f); 254 isAlive = false; 255 } 256 257 /** 258 * @param ammo The integer to be added to the amount of ammo this pirate has access to 259 */ 260 public void setAmmo(int ammo) { 261 // Assessment 2 change: Refactored to use key for ammo instead of variable 262 values.replace("ammo", (float) ammo); 263 } 264 265 /** 266 * @return the ammo value attached to this pirate 267 */ 268 public int getAmmo() { 269 // Assessment 2 change: Refactored to use key for ammo instead of variable 270 return Math.round(values.get("ammo")); 271 } 272 273 public QueueFIFO<Ship> getTargets() { 274 return targets; 275 } 276 277 public void removeTarget() { 278 targets.pop(); 279 } 280 281 //public int targetCount() { 282 // return targets.size(); 283 // } 284 285 //public void reload(int ammo) { 286 // // Assessment 2 change: Refactored to use key for ammo instead of variable 287 // values.replace("ammo", (float) getAmmo()+ammo); 288 // } 289 }