Coverage Summary for Class: Ship (com.mygdx.game.Entitys)
| Class | Class, % | Method, % | Line, % |
|---|---|---|---|
| Ship | 100% (1/1) | 87.5% (21/24) | 88.3% (53/60) |
1 package com.mygdx.game.Entitys; 2 3 import com.badlogic.gdx.graphics.g2d.Sprite; 4 import com.badlogic.gdx.math.Vector2; 5 import com.badlogic.gdx.utils.ObjectMap; 6 import com.mygdx.game.Components.*; 7 import com.mygdx.game.Faction; 8 import com.mygdx.game.Managers.GameManager; 9 import com.mygdx.game.Managers.RenderLayer; 10 import com.mygdx.game.Managers.ResourceManager; 11 import com.mygdx.game.Physics.CollisionCallBack; 12 import com.mygdx.game.Physics.CollisionInfo; 13 import com.mygdx.game.Physics.PhysicsBodyType; 14 import com.mygdx.utils.Utilities; 15 16 import java.util.Objects; 17 18 /** 19 * Base class for game ships, Player & NPC. 20 */ 21 public class Ship extends Entity implements CollisionCallBack { 22 private static int shipCount = 0; 23 public static ObjectMap<Vector2, String> shipDirections; 24 25 private final Vector2 currentDir; 26 27 /** 28 * Creates a ship entity, containing Transform, Renderable, RigidBody, and Pirate components. 29 */ 30 public Ship() { 31 super(5); 32 currentDir = new Vector2(); 33 setName("Ship (" + shipCount++ + ")"); // each ship has a unique name 34 35 if (shipDirections == null) { 36 shipDirections = new ObjectMap<>(); 37 shipDirections.put(new Vector2(0, 1), "-up"); 38 shipDirections.put(new Vector2(0, -1), "-down"); 39 shipDirections.put(new Vector2(1, 0), "-right"); 40 shipDirections.put(new Vector2(-1, 0), "-left"); 41 shipDirections.put(new Vector2(1, 1), "-ur"); 42 shipDirections.put(new Vector2(-1, 1), "-ul"); 43 shipDirections.put(new Vector2(1, -1), "-dr"); 44 shipDirections.put(new Vector2(-1, -1), "-dl"); 45 } 46 47 Transform t = new Transform(); 48 t.setPosition(800, 800); 49 Renderable r = new Renderable(3, "white-up", RenderLayer.Transparent); 50 RigidBody rb = new RigidBody(PhysicsBodyType.Dynamic, r, t); 51 rb.setCallback(this); 52 53 Pirate p = new Pirate(); 54 PowerUpAssigned pow = new PowerUpAssigned(); 55 56 // rb.setCallback(this); 57 58 addComponents(t, r, rb, p, pow); 59 } 60 61 /** 62 * // New for assessment 2 // 63 * Get a Pirate value. 64 * @param key The value to get 65 * @return The value 66 */ 67 public float getValue(String key) { 68 return getComponent(Pirate.class).getValue(key); 69 } 70 71 /** 72 * // New for assessment 2 // 73 * Reset a Pirate value to what it originally was. 74 * @param key The value to reset 75 */ 76 public void resetToDefault(String key) { 77 getComponent(Pirate.class).resetToDefault(key); 78 } 79 80 /** 81 * @return the boolean value of life attached to the Pirate Component 82 */ 83 public boolean isAlive() { 84 return getComponent(Pirate.class).getHealth() > 0; 85 } 86 87 /** 88 * @return the range at which this ship can attack 89 */ 90 public static float getAttackRange() { 91 return Utilities.tilesToDistance(GameManager.getSettings().get("starting").getFloat("attackRange_tiles")); 92 } 93 94 /** 95 * @param money the integer to be added to the money value attached to the Pirate Component 96 */ 97 public void plunder(int money) { 98 getComponent(Pirate.class).addPlunder(money); 99 } 100 101 /** 102 * @param increment the integer to be added to the points value attached to the Pirate Component 103 */ 104 public void points(int increment) { 105 getComponent(Pirate.class).addPoints(increment); 106 } 107 108 /** 109 * Associates ship with faction and orients it to the default northern direction. 110 * @param factionId the desired faction id 111 */ 112 public void setFaction(int factionId) { 113 getComponent(Pirate.class).setFactionId(factionId); 114 setShipDirection("-up"); 115 } 116 117 /** 118 * gets the string representation of the direction the ship is facing 119 * @param dir the vector dir the ship is facing 120 * @return the string representation of the direction 121 */ 122 private String getShipDirection(Vector2 dir) { 123 if (!currentDir.equals(dir) && shipDirections.containsKey(dir)) { 124 currentDir.set(dir); 125 return shipDirections.get(dir); 126 } 127 return ""; 128 } 129 130 /** 131 * gets the faction colour 132 * @return the faction colour 133 */ 134 private String getColour() { 135 return getComponent(Pirate.class).getFaction().getColour(); 136 } 137 138 /** 139 * will rotate the ship to face the direction (just changes the sprite doesn't actually rotate) 140 * @param dir the dir to face (used to get the correct sprite from the texture atlas 141 */ 142 public void setShipDirection(Vector2 dir) { 143 setShipDirection(getShipDirection(dir)); 144 } 145 146 /** 147 * will rotate the ship to face the direction (just changes the sprite doesn't actually rotate) 148 * @param direction the dir to face (used to get the correct sprite from the texture atlas 149 */ 150 public void setShipDirection(String direction) { 151 if (Objects.equals(direction, "")) { 152 return; 153 } 154 Renderable r = getComponent(Renderable.class); 155 Sprite s = ResourceManager.getSprite(3, getColour() + direction); 156 157 try { 158 r.setTexture(s); 159 } catch (Exception ignored) { 160 161 } 162 } 163 164 /** 165 * @return the health attached to the Pirate Component 166 */ 167 public int getHealth() { 168 return getComponent(Pirate.class).getHealth(); 169 } 170 171 /** 172 * @return the plunder attached to the Pirate Component 173 */ 174 public int getPlunder() { 175 return getComponent(Pirate.class).getPlunder(); 176 } 177 178 /** 179 * @return the points attached to the Pirate Component 180 */ 181 public int getPoints() { 182 return getComponent(Pirate.class).getPoints(); 183 } 184 185 /** 186 * Calls shoot method with the current direction as parameter 187 */ 188 public void shoot() { 189 shoot(currentDir); 190 } 191 192 /** 193 * Calls shoot method of internal component 194 */ 195 public void shoot(Vector2 dir) { 196 getComponent(Pirate.class).shoot(dir); 197 } 198 199 /** 200 * @return copy of the transform's position 201 */ 202 public Vector2 getPosition() { 203 return getComponent(Transform.class).getPosition().cpy(); 204 } 205 206 /** 207 * Added for Assessment 2 208 * @return The Faction of the Pirate Component attached to this entity 209 */ 210 public Faction getFaction() { 211 return getComponent(Pirate.class).getFaction(); 212 } 213 214 /** 215 * Amended for Assessment 2 (added functionality for when attacked by cannonball) 216 * if called on a Player against anything else call it on the other thing 217 */ 218 @Override 219 public void EnterTrigger(CollisionInfo info) { 220 if (info.a instanceof CannonBall) { 221 CannonBall a = (CannonBall) info.a; 222 if(a.getFaction() != getFaction()){ 223 getComponent(Pirate.class).takeDamage( a.getAttackDmg() ); 224 a.kill(); 225 } 226 }else if (this instanceof Player && !(info.b instanceof Player)) { 227 ((CollisionCallBack) info.b).EnterTrigger(info); 228 } 229 } 230 231 /** 232 * if called on a Player against anything else call it on the other thing 233 */ 234 @Override 235 public void ExitTrigger(CollisionInfo info) { 236 if (this instanceof Player && !(info.b instanceof Player)) { 237 ((CollisionCallBack) info.b).ExitTrigger(info); 238 } 239 } 240 241 /** 242 * `unused` 243 */ 244 @Override 245 public void BeginContact(CollisionInfo info) { 246 247 } 248 249 /** 250 * `unused` 251 */ 252 @Override 253 public void EndContact(CollisionInfo info) { 254 255 } 256 }