Coverage Summary for Class: Ship (com.mygdx.game.Entitys)
| Class | Class, % | Method, % | Line, % |
|---|---|---|---|
| Ship | 100% (1/1) | 64.3% (18/28) | 64.5% (91/141) |
1 package com.mygdx.game.Entitys; 2 3 import com.badlogic.gdx.Application; 4 import com.badlogic.gdx.Gdx; 5 import com.badlogic.gdx.graphics.g2d.Sprite; 6 import com.badlogic.gdx.math.Vector2; 7 import com.badlogic.gdx.utils.ObjectMap; 8 import com.mygdx.game.Components.Pirate; 9 import com.mygdx.game.Components.Renderable; 10 import com.mygdx.game.Components.RigidBody; 11 import com.mygdx.game.Components.Transform; 12 import com.mygdx.game.Managers.GameManager; 13 import com.mygdx.game.Managers.RenderLayer; 14 import com.mygdx.game.Managers.ResourceManager; 15 import com.mygdx.game.Physics.CollisionCallBack; 16 import com.mygdx.game.Physics.CollisionInfo; 17 import com.mygdx.game.Physics.PhysicsBodyType; 18 import com.mygdx.utils.Utilities; 19 20 import java.util.Objects; 21 22 /** 23 * Base class for game ships, Player & NPC. 24 */ 25 public class Ship extends Entity implements CollisionCallBack { 26 private static int shipCount = 0; 27 public static ObjectMap<Vector2, String> shipDirections; 28 29 private final Vector2 currentDir; 30 private boolean isFreeze; 31 /** 32 * Creates a ship entity, containing Transform, Renderable, RigidBody, and Pirate components. 33 */ 34 public Ship() { 35 super(4); 36 currentDir = new Vector2(); 37 setName("Ship (" + shipCount++ + ")"); // each ship has a unique name 38 isFreeze = false; 39 if (shipDirections == null) { 40 shipDirections = new ObjectMap<>(); 41 shipDirections.put(new Vector2(0, 1), "-up"); 42 shipDirections.put(new Vector2(0, -1), "-down"); 43 shipDirections.put(new Vector2(1, 0), "-right"); 44 shipDirections.put(new Vector2(-1, 0), "-left"); 45 shipDirections.put(new Vector2(1, 1), "-ur"); 46 shipDirections.put(new Vector2(-1, 1), "-ul"); 47 shipDirections.put(new Vector2(1, -1), "-dr"); 48 shipDirections.put(new Vector2(-1, -1), "-dl"); 49 } 50 51 Transform t = new Transform(); 52 t.setPosition(800, 800); 53 54 Renderable r; 55 if(Application.ApplicationType.HeadlessDesktop == Gdx.app.getType()){ 56 r =new Renderable(); 57 } 58 else{ 59 r = new Renderable(3, "white-up", RenderLayer.Transparent); 60 } 61 62 RigidBody rb = new RigidBody(PhysicsBodyType.Dynamic, r, t); 63 rb.setCallback(this); 64 65 Pirate p = new Pirate(); 66 67 // rb.setCallback(this); 68 69 addComponents(t, r, rb, p); 70 71 72 73 } 74 75 public boolean isAlive() { 76 return getComponent(Pirate.class).getHealth() > 0; 77 } 78 79 public static float getAttackRange() { 80 return Utilities.tilesToDistance(GameManager.getSettings().get("starting").getFloat("attackRange_tiles")); 81 } 82 83 public void plunder(int money) { 84 getComponent(Pirate.class).addPlunder(money); 85 } 86 87 /** 88 * Associates ship with faction and orients it to the default northern direction. 89 * 90 * @param factionId the desired faction id 91 */ 92 public void setFaction(int factionId) { 93 getComponent(Pirate.class).setFactionId(factionId); 94 if(!(Application.ApplicationType.HeadlessDesktop == Gdx.app.getType())) { 95 setShipDirection("-up"); 96 } 97 } 98 99 public void setFreeze(boolean state){ 100 isFreeze = state; 101 } 102 103 public boolean getFreeze(){ 104 return isFreeze; 105 } 106 /** 107 * gets the string representation of the direction the ship is facing 108 * 109 * @param dir the vector dir the ship is facing 110 * @return the string representation of the direction 111 */ 112 private String getShipDirection(Vector2 dir) { 113 if (!currentDir.equals(dir) && shipDirections.containsKey(dir)) { 114 currentDir.set(dir); 115 return shipDirections.get(dir); 116 } 117 return ""; 118 } 119 120 /** 121 * gets the faction colour 122 * 123 * @return the faction colour 124 */ 125 private String getColour() { 126 return getComponent(Pirate.class).getFaction().getColour(); 127 } 128 129 /** 130 * will rotate the ship to face the direction (just changes the sprite doesn't actually rotate) 131 * 132 * @param dir the dir to face (used to get the correct sprite from the texture atlas 133 */ 134 public void setShipDirection(Vector2 dir) { 135 setShipDirection(getShipDirection(dir)); 136 } 137 138 /** 139 * will rotate the ship to face the direction (just changes the sprite doesn't actually rotate) 140 * 141 * @param direction the dir to face (used to get the correct sprite from the texture atlas 142 */ 143 public void setShipDirection(String direction) { 144 if (Objects.equals(direction, "")) { 145 return; 146 } 147 Renderable r = getComponent(Renderable.class); 148 Sprite s = ResourceManager.getSprite(3, getColour() + direction); 149 150 try { 151 r.setTexture(s); 152 } catch (Exception ignored) { 153 154 } 155 } 156 157 public int getHealth() { 158 return getComponent(Pirate.class).getHealth(); 159 } 160 161 public void setHealth(int overrideHealth) { 162 getComponent(Pirate.class).setHealth(overrideHealth); 163 } 164 165 public int getPlunder() { 166 return getComponent(Pirate.class).getPlunder(); 167 } 168 169 public void shoot(Vector2 dir) { 170 getComponent(Pirate.class).shoot(dir); 171 } 172 173 public void shoot() { 174 getComponent(Pirate.class).shoot(currentDir); 175 } 176 177 public void tempImmortality(boolean state){ 178 getComponent(Pirate.class).setImmortality(state); 179 } 180 181 public void unlimitedAmmo(boolean state){ 182 getComponent(Pirate.class).setUnlimitedAmmo(state); 183 } 184 185 public void shoot8Directions(boolean state){ 186 getComponent(Pirate.class).setShootEightDirections(state); 187 } 188 189 public void biggerDamage(boolean state){ 190 getComponent(Pirate.class).setBiggerDamage(state); 191 } 192 193 194 /** 195 * @return copy of the transform's position 196 */ 197 public Vector2 getPosition() { 198 return getComponent(Transform.class).getPosition().cpy(); 199 } 200 201 public void setPosition(Float x,Float y){ 202 getComponent(Transform.class).setPosition(x,y); 203 } 204 205 @Override 206 public void BeginContact(CollisionInfo info) { 207 208 209 210 } 211 212 @Override 213 public void EndContact(CollisionInfo info) { 214 215 } 216 217 218 public void ShipDeath(){} 219 220 /** MAJOR ADDITIONS 221 * if called on a Player against anything else call it on the other thing 222 */ 223 @Override 224 public void EnterTrigger(CollisionInfo info) { 225 if (this instanceof Player && !(info.b instanceof Player) && !(info.b instanceof Weather) && !(info.b instanceof Monster)) { 226 ((CollisionCallBack) info.b).EnterTrigger(info); 227 } 228 229 if (info.a instanceof CannonBall){ 230 CannonBall ball = (CannonBall) info.a; 231 if(ball.getShooter() != this & (ball.getShooter().getComponent(Pirate.class).getFaction() != this.getComponent(Pirate.class).getFaction())){ 232 Pirate pirate = getComponent(Pirate.class); 233 pirate.takeDamage(5); 234 ball.kill(); 235 System.out.print(getName()); 236 System.out.println(getHealth()); 237 } 238 if(!isAlive()){ 239 Ship shooter = ball.getShooter(); 240 shooter.plunder(50); 241 shooter.getComponent(Pirate.class).addPoints(100); 242 } 243 244 } else if (info.a instanceof CannonBallCollege) { 245 CannonBallCollege ball = (CannonBallCollege) info.a; 246 Pirate pirate = getComponent(Pirate.class); 247 pirate.takeDamage(15); 248 ball.kill(); 249 System.out.print(getName()); 250 System.out.println(getHealth()); 251 } 252 else if (info.b instanceof Weather){ 253 Weather weather = (Weather) info.b; 254 Pirate pirate = getComponent(Pirate.class); 255 pirate.takeDamage(10); 256 System.out.print(getName()); 257 System.out.println(getHealth()); 258 System.out.println(getHealth()); 259 } 260 else if (info.b instanceof Monster){ 261 Monster monster = (Monster) info.b; 262 Pirate pirate = getComponent(Pirate.class); 263 pirate.takeDamage(40); 264 System.out.print(getName()); 265 System.out.println(getHealth()); 266 System.out.println(getHealth()); 267 } 268 if (!isAlive()) { 269 ShipDeath(); 270 } 271 } 272 273 /** 274 * if called on a Player against anything else call it on the other thing 275 */ 276 @Override 277 public void ExitTrigger(CollisionInfo info) { 278 if (this instanceof Player && !(info.b instanceof Player) && !(info.b instanceof Weather) && !(info.b instanceof Monster)) { 279 ((CollisionCallBack) info.b).ExitTrigger(info); 280 } 281 } 282 }