Coverage Summary for Class: CannonBall (com.mygdx.game.Entitys)
| Class | Class, % | Method, % | Line, % | 
|---|---|---|---|
| CannonBall | 100% (1/1) | 100% (11/11) | 100% (38/38) | 
1 package com.mygdx.game.Entitys; 2 3 import com.badlogic.gdx.math.Vector2; 4 import com.mygdx.game.Components.Pirate; 5 import com.mygdx.game.Components.Renderable; 6 import com.mygdx.game.Components.RigidBody; 7 import com.mygdx.game.Components.Transform; 8 import com.mygdx.game.Faction; 9 import com.mygdx.game.Managers.EntityManager; 10 import com.mygdx.game.Managers.GameManager; 11 import com.mygdx.game.Managers.RenderLayer; 12 import com.mygdx.game.Physics.CollisionCallBack; 13 import com.mygdx.game.Physics.CollisionInfo; 14 import com.mygdx.game.Physics.PhysicsBodyType; 15 16 import static com.mygdx.utils.Constants.TILE_SIZE; 17 18 /** 19 * Cannonball entity and the methods to get it flying. 20 */ 21 public class CannonBall extends Entity implements CollisionCallBack { 22 private static float speed; 23 private boolean toggleLife; 24 private static final int MAX_AGE = 5; 25 private Entity shooter; // Changed for Assessment 2, type switched from Ship to Entity 26 27 /** 28 * Constructs the cannonball object by generating components 29 */ 30 public CannonBall() { 31 super(3); 32 setName("ball"); 33 toggleLife = false; 34 Transform t = new Transform(); 35 t.setPosition(-100, 100); 36 t.setScale(0.5f, 0.5f); 37 Renderable r = new Renderable(4, "ball", RenderLayer.Transparent); 38 RigidBody rb = new RigidBody(PhysicsBodyType.Dynamic, r, t, true); 39 rb.setCallback(this); 40 41 addComponents(t, r, rb); 42 43 speed = GameManager.getSettings().get("starting").getFloat("cannonSpeed"); 44 r.hide(); 45 } 46 47 /** 48 * Called once per frame 49 */ 50 @Override 51 public void update() { 52 super.update(); 53 removeOnCollision(); 54 } 55 56 /** 57 * Removes the cannonball offscreen once it hits a target. 58 */ 59 private void removeOnCollision() { 60 if (toggleLife) { 61 getComponent(Renderable.class).hide(); 62 Transform t = getComponent(Transform.class); 63 t.setPosition(10000, 10000); 64 65 RigidBody rb = getComponent(RigidBody.class); 66 rb.setPosition(t.getPosition()); 67 rb.setVelocity(0, 0); 68 toggleLife = false; 69 } 70 /*else{ 71 age += EntityManager.getDeltaTime(); 72 } 73 if(age > MAX_AGE) { 74 age = 0; 75 kill(); 76 }*/ 77 } 78 79 /** 80 * Teleport the cannonball in from offscreen and send in flying away from the ship. 81 * 82 * @param pos 2D vector location from where it sets off 83 * @param dir 2D vector direction for its movement 84 * @param sender ship entity firing it 85 */ 86 public void fire(Entity sender, Vector2 pos, Vector2 dir) { 87 Transform t = getComponent(Transform.class); 88 t.setPosition(pos); 89 90 RigidBody rb = getComponent(RigidBody.class); 91 Vector2 ta = dir.cpy().scl(speed * EntityManager.getDeltaTime()); 92 Vector2 o = new Vector2(TILE_SIZE * t.getScale().x, TILE_SIZE * t.getScale().y); 93 Vector2 v = ta.cpy().sub(o); 94 95 rb.setVelocity(v); 96 97 getComponent(Renderable.class).show(); 98 shooter = sender; 99 } 100 101 /** 102 * Marks cannonball for removal on next update. 103 */ 104 public void kill() { 105 toggleLife = true; 106 } 107 108 /** 109 * Added for Assessment 2 110 * @return The Faction of the Pirate Component attached to this entity 111 */ 112 public Faction getFaction() { 113 return shooter.getComponent(Pirate.class).getFaction(); 114 } 115 116 /** 117 * Added for Assessment 2 118 * @return the damage dealt by the unit shooting this cannonball 119 */ 120 public Float getAttackDmg() { 121 return shooter.getComponent(Pirate.class).getAttackDmg(); 122 } 123 124 /** 125 * `unused` 126 */ 127 @Override 128 public void BeginContact(CollisionInfo info) { 129 130 } 131 132 /** 133 * `unused` 134 */ 135 @Override 136 public void EndContact(CollisionInfo info) { 137 138 } 139 140 /** 141 * `unused` 142 */ 143 @Override 144 public void EnterTrigger(CollisionInfo info) { 145 146 } 147 148 /** 149 * `unused` 150 */ 151 @Override 152 public void ExitTrigger(CollisionInfo info) { 153 154 } 155 156 // public Entity getShooter() { 157 // return shooter; 158 // } 159 }