Coverage Summary for Class: CannonBallCollege (com.mygdx.game.Entitys)

Class Class, % Method, % Line, %
CannonBallCollege 100% (1/1) 70% (7/10) 89.7% (35/39)


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 /** NEW CLASS 19  * Cannonball entity and the methods to get it flying. 20  */ 21 public class CannonBallCollege extends Entity implements CollisionCallBack { 22  private static float speed; 23  private boolean toggleLife; 24  private static final int MAX_AGE = 5; 25  // private float age = 0; 26  private College shooter; 27  28  public CannonBallCollege() { 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  RigidBody rb = new RigidBody(PhysicsBodyType.Dynamic, r, t, true); 43  rb.setCallback(this); 44  45  addComponents(t, r, rb); 46  47  speed = GameManager.getSettings().get("starting").getFloat("cannonSpeed"); 48  r.hide(); 49  } 50  51  @Override 52  public void update() { 53  super.update(); 54  removeOnCollision(); 55  } 56  57  /** 58  * Removes the cannonball offscreen once it hits a target. 59  */ 60  private void removeOnCollision() { 61  if (toggleLife) { 62  getComponent(Renderable.class).hide(); 63  Transform t = getComponent(Transform.class); 64  t.setPosition(10000, 10000); 65  66  RigidBody rb = getComponent(RigidBody.class); 67  rb.setPosition(t.getPosition()); 68  rb.setVelocity(0, 0); 69  toggleLife = false; 70  } 71  } 72  73  /** 74  * Teleport the cannonball in from offscreen and set in flying away from the ship. 75  * 76  * @param pos 2D vector location from where it sets off 77  * @param dir 2D vector direction for its movement 78  * @param sender ship entity firing it 79  */ 80  public void fire(Vector2 pos, Vector2 dir, College sender) { 81  Transform t = getComponent(Transform.class); 82  t.setPosition(pos); 83  84  RigidBody rb = getComponent(RigidBody.class); 85  Vector2 ta = dir.cpy().scl(speed * EntityManager.getDeltaTime()); 86  Vector2 o = new Vector2(TILE_SIZE * t.getScale().x, TILE_SIZE * t.getScale().y); 87  Vector2 v = ta.cpy().sub(o); 88  89  rb.setVelocity(v); 90  91  getComponent(Renderable.class).show(); 92  shooter = sender; 93  } 94  95  /** 96  * Marks cannonball for removal on next update. 97  */ 98  public void kill() { 99  toggleLife = true; 100  } 101  102  public College getShooter() { 103  return shooter; 104  } 105  106  @Override 107  public void BeginContact(CollisionInfo info) { 108  109  } 110  111  @Override 112  public void EndContact(CollisionInfo info) { 113  114  } 115  116  @Override 117  public void EnterTrigger(CollisionInfo info) { 118  119  } 120  121  @Override 122  public void ExitTrigger(CollisionInfo info) { 123  124  } 125 }