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

Class Class, % Method, % Line, %
College 100% (1/1) 75% (6/8) 81.5% (53/65)


1 package com.mygdx.game.Entitys; 2  3 import com.badlogic.gdx.Game; 4 import com.badlogic.gdx.math.Vector2; 5 import com.badlogic.gdx.utils.JsonValue; 6 import com.mygdx.game.Components.Pirate; 7 import com.mygdx.game.Components.Transform; 8 import com.mygdx.game.Faction; 9 import com.mygdx.game.Managers.GameManager; 10 import com.mygdx.utils.Utilities; 11  12 import java.util.ArrayList; 13  14 /** 15  * Defines a college and its associated buildings. 16  */ 17 public class College extends Entity { 18  private static ArrayList<String> buildingNames; 19  private final ArrayList<Building> buildings; 20  private int i; 21  private Vector2 target; 22  23  private boolean active; 24  25  public College() { 26  super(); 27  buildings = new ArrayList<>(); 28  buildingNames = new ArrayList<>(); 29  buildingNames.add("big"); 30  buildingNames.add("small"); 31  buildingNames.add("clock"); 32  i = 0; 33  Transform t = new Transform(); 34  Pirate p = new Pirate(); 35  addComponents(t, p); 36  active = true; 37  } 38  39  /** 40  * Creates a college at the location associated with the given faction id. 41  * 42  * @param factionId numerical id of the faction 43  */ 44  public College(int factionId) { 45  this(); 46  Faction f = GameManager.getFaction(factionId); 47  Transform t = getComponent(Transform.class); 48  t.setPosition(f.getPosition()); 49  Pirate p = getComponent(Pirate.class); 50  p.setFactionId(factionId); 51  spawn(f.getColour()); 52  } 53  54  /** 55  * Randomly populates the college radius with buildings. 56  * 57  * @param colour used to pull the appropriate flag sprite 58  */ 59  private void spawn(String colour) { 60  JsonValue collegeSettings = GameManager.getSettings().get("college"); 61  float radius = collegeSettings.getFloat("spawnRadius"); 62  // radius = Utilities.tilesToDistance(radius) * BUILDING_SCALE; 63  final Vector2 origin = getComponent(Transform.class).getPosition(); 64  ArrayList<Vector2> posList = new ArrayList<>(); 65  posList.add(new Vector2(0, 0)); 66  67  for (int i = 0; i < collegeSettings.getInt("numBuildings"); i++) { 68  Vector2 pos = Utilities.randomPos(-radius, radius); 69  pos = Utilities.floor(pos); 70  71  if (!posList.contains(pos)) { 72  posList.add(pos); 73  74  pos = Utilities.tilesToDistance(pos).add(origin); 75  76  Building b = new Building(); 77  buildings.add(b); 78  79  String b_name = Utilities.randomChoice(buildingNames, 0); 80  81  b.create(pos, b_name); 82  } 83  84  85  } 86  Building flag = new Building(true); 87  buildings.add(flag); 88  flag.create(origin, colour); 89  } 90  91  /** 92  * True as long as unharmed buildings remain, false otherwise. 93  */ 94  public boolean isAlive() { 95  boolean res = false; 96  for (int i = 0; i < buildings.size() - 1; i++) { 97  Building b = buildings.get(i); 98  if (b.isAlive()) { 99  res = true; 100  } 101  } 102  if (!res) { 103  if(getComponent(Pirate.class).isAlive()){ 104  GameManager.getPlayer().getComponent(Pirate.class).addPoints(10000); 105  GameManager.getPlayer().getComponent(Pirate.class).addPlunder(1000); 106  active = false; 107  } 108  getComponent(Pirate.class).kill(); 109  110  111  } 112  return res; 113  } 114  115  /**NEW 116  * Can be called to kill a college and all of its buildings 117  */ 118  public void kill(){ 119  for (int i = 0; i < buildings.size() - 1; i++) { 120  Building b = buildings.get(i); 121  b.destroy(); 122  } 123  active = false; 124  125  } 126  127  128  public void shoot(Vector2 target) { 129  GameManager.shoot2(this, target); 130  } 131  132  public Vector2 getPosition() { 133  return getComponent(Transform.class).getPosition().cpy(); 134  } 135  136  @Override 137  public void update() { 138  super.update(); 139  isAlive(); 140  if(this.getComponent(Pirate.class).getFaction()== GameManager.getPlayer().getComponent(Pirate.class).getFaction()){ 141  return; 142  } 143  if(active){ 144  if (i == 50) { 145  target = new Vector2(-1 * (this.getPosition().x - GameManager.ships.get(0).getPosition().x), -1 * (this.getPosition().y - GameManager.ships.get(0).getPosition().y)); 146  shoot(target); 147  i = 0; 148  } 149  i++; 150  151  } 152  153  } 154  155 }