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

Class Class, % Method, % Line, %
Indicator 100% (1/1) 40% (2/5) 64.3% (18/28)


1 package com.mygdx.game.Entitys; 2  3 import com.badlogic.gdx.math.Vector2; 4 import com.mygdx.game.Components.*; 5 import com.mygdx.game.Managers.GameManager; 6 import com.mygdx.game.Managers.QuestManager; 7 import com.mygdx.game.Managers.RenderLayer; 8 import com.mygdx.game.Quests.KillQuest; 9  10 import static java.lang.Math.abs; 11 import static java.lang.Math.max; 12  13 /** 14  * Added for Assessment 2 in order to increase visual clarity on Kill Quests 15  */ 16 public class Indicator extends Entity { 17  private final Entity target; 18  private final KillQuest quest; 19  private Vector2 gradient; 20  21  /** 22  * Creates an indicator attached to a kill quest and targeting a college 23  * @param kq the Kill Quest this indicator is attached to 24  */ 25  public Indicator(KillQuest kq) { 26  super(2); 27  quest = kq; 28  target = quest.getTarget().getParent(); 29  30  Transform t = new Transform(); 31  t.setPosition(800, 800); 32  Renderable r = new Renderable(10, RenderLayer.Transparent); 33  r.hide(); 34  35  addComponents(t, r); 36  gradient = updateGradient(); 37  } 38  39  /** 40  * Called once per frame 41  */ 42  @Override 43  public void update(){ 44  if(QuestManager.currentQuest() == quest){ 45  getComponent(Renderable.class).show(); 46  }else{ 47  getComponent(Renderable.class).hide(); 48  } 49  followPlayer(); 50  } 51  52  /** 53  * Called when updating the gradient of the Indicator. 54  */ 55  public Vector2 updateGradient(){ 56  Player p = GameManager.getPlayer(); 57  Transform self = p.getComponent(Transform.class); 58  Transform goal = target.getComponent(Transform.class); 59  60  // Calculate the gradient to draw the indicator at. 61  float changeInX = goal.getPosition().x - self.getPosition().x; 62  float changeInY = goal.getPosition().y - self.getPosition().y; 63  float scaleFactor = max(abs(changeInX),abs(changeInY)); 64  float dx = changeInX / scaleFactor; 65  float dy = changeInY / scaleFactor; 66  67  return (new Vector2(dx, dy)); 68  } 69  70  /** 71  * Called when updating the position of the Indicator. 72  */ 73  public void followPlayer() { 74  gradient = updateGradient(); 75  rotateSprite(); 76  Player p = GameManager.getPlayer(); 77  Vector2 position = new Vector2( p.getPosition().x + (50 * gradient.x), p.getPosition().y + (50 * gradient.y) ); 78  getComponent(Transform.class).setPosition(position); 79  } 80  81  /** 82  * Called when rotating the sprite of the Indicator. 83  */ 84  public void rotateSprite() { 85  getComponent(Transform.class).setRotation((float) Math.atan2(gradient.y,gradient.x)); 86  } 87 }