Coverage Summary for Class: CannonBall (com.mygdx.game.Entitys)
| Class | Class, % | Method, % | Line, % |
|---|---|---|---|
| CannonBall | 100% (1/1) | 90% (9/10) | 88.9% (40/45) |
1 package com.mygdx.game.Entitys; 2 3 import com.badlogic.gdx.Application; 4 import com.badlogic.gdx.Gdx; 5 import com.badlogic.gdx.math.Vector2; 6 import com.mygdx.game.Components.Renderable; 7 import com.mygdx.game.Components.RigidBody; 8 import com.mygdx.game.Components.Transform; 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 = 10; 25 private float age = 0; 26 private Ship shooter; 27 28 public CannonBall() { 29 super(3); 30 setName("ball"); 31 toggleLife = false; 32 Transform t = new Transform(); 33 t.setPosition(-100, 100); 34 t.setScale(0.5f, 0.5f); 35 Renderable r; 36 if(Application.ApplicationType.HeadlessDesktop == Gdx.app.getType()){ 37 r =new Renderable(); 38 } 39 else{ 40 r = new Renderable(4, "ball", RenderLayer.Transparent); 41 } 42 43 RigidBody rb = new RigidBody(PhysicsBodyType.Dynamic, r, t, true); 44 rb.setCallback(this); 45 46 addComponents(t, r, rb); 47 48 speed = GameManager.getSettings().get("starting").getFloat("cannonSpeed"); 49 r.hide(); 50 } 51 52 @Override 53 public void update() { 54 super.update(); 55 removeOnCollision(); 56 } 57 58 /** 59 * Removes the cannonball offscreen once it hits a target. 60 */ 61 private void removeOnCollision() { 62 if (toggleLife) { 63 getComponent(Renderable.class).hide(); 64 Transform t = getComponent(Transform.class); 65 t.setPosition(10000, 10000); 66 67 RigidBody rb = getComponent(RigidBody.class); 68 rb.setPosition(t.getPosition()); 69 rb.setVelocity(0, 0); 70 toggleLife = false; 71 } 72 else{ 73 age += EntityManager.getDeltaTime(); 74 } 75 if(age > MAX_AGE) { 76 age = 0; 77 kill(); 78 } 79 } 80 81 /** 82 * Teleport the cannonball in from offscreen and set in flying away from the ship. 83 * 84 * @param pos 2D vector location from where it sets off 85 * @param dir 2D vector direction for its movement 86 * @param sender ship entity firing it 87 */ 88 public void fire(Vector2 pos, Vector2 dir, Ship sender) { 89 Transform t = getComponent(Transform.class); 90 t.setPosition(pos); 91 92 RigidBody rb = getComponent(RigidBody.class); 93 Vector2 ta = dir.cpy().scl(speed * EntityManager.getDeltaTime()); 94 Vector2 o = new Vector2(TILE_SIZE * t.getScale().x, TILE_SIZE * t.getScale().y); 95 Vector2 v = ta.cpy().sub(o); 96 97 rb.setVelocity(v); 98 99 getComponent(Renderable.class).show(); 100 shooter = sender; 101 } 102 103 /** 104 * Marks cannonball for removal on next update. 105 */ 106 public void kill() { 107 toggleLife = true; 108 } 109 110 public Ship getShooter() { 111 return shooter; 112 } 113 114 @Override 115 public void BeginContact(CollisionInfo info) { 116 117 } 118 119 120 @Override 121 public void EndContact(CollisionInfo info) { 122 123 } 124 125 @Override 126 public void EnterTrigger(CollisionInfo info) { 127 128 } 129 130 @Override 131 public void ExitTrigger(CollisionInfo info) { 132 133 } 134 }