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

Class Class, % Method, % Line, %
College 100% (1/1) 90.9% (10/11) 83.1% (74/89)


1 package com.mygdx.game.Entitys; 2  3 import com.badlogic.gdx.math.Vector2; 4 import com.badlogic.gdx.utils.JsonValue; 5 import com.badlogic.gdx.utils.TimeUtils; 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  public Faction f; 21  private Faction mostRecentAttacker; //Added for Assessment 2, used to determine ownership after capturing 22  private Building flag; //Added for Assessment 2, allows flag to be referenced independently of other buildings. 23  private long lastShootTime; //Added for Assessment 2, stores the time when the college last attacked 24  private float buffer; //Added for Assessment 2, stores the radius of the college's area 25  private boolean alive; //Added for Assessment 2, keeps track of the college's life status 26  27  /** 28  * Creates a college. 29  * Changed for Assessment 2: 30  * - Added lastShootTime to store the time when the college last attacked 31  */ 32  public College() { 33  super(); 34  buildings = new ArrayList<>(); 35  buildingNames = new ArrayList<>(); 36  buildingNames.add("big"); 37  buildingNames.add("small"); 38  buildingNames.add("clock"); 39  Transform t = new Transform(); 40  Pirate p = new Pirate(); 41  addComponents(t, p); 42  lastShootTime = TimeUtils.millis() / 1000; 43  } 44  45  /** 46  * Creates a college at the location associated with the given faction id. 47  * Changed for Assessment 2: 48  * - Set infinite ammo to true in Pirate Component 49  * @param factionId numerical id of the faction 50  */ 51  public College(int factionId) { 52  this(); 53  f = GameManager.getFaction(factionId); 54  Transform t = getComponent(Transform.class); 55  t.setPosition(f.getPosition()); 56  Pirate p = getComponent(Pirate.class); 57  p.setFactionId(factionId); 58  p.setInfiniteAmmo(true); 59  spawn(f.getColour()); 60  alive = true; 61  } 62  63  /** 64  * Randomly populates the college radius with buildings. 65  * Changed for Assessment 2: 66  * - Set buffer in Pirate Component to the radius of the college 67  * @param colour used to pull the appropriate flag sprite 68  */ 69  private void spawn(String colour) { 70  JsonValue collegeSettings = GameManager.getSettings().get("college"); 71  float radius = collegeSettings.getFloat("spawnRadius"); 72  buffer = Utilities.tilesToDistance(radius+1f) ; 73  // radius = Utilities.tilesToDistance(radius) * BUILDING_SCALE; 74  final Vector2 origin = getComponent(Transform.class).getPosition(); 75  ArrayList<Vector2> posList = new ArrayList<>(); 76  posList.add(new Vector2(0, 0)); 77  78  for (int i = 0; i < collegeSettings.getInt("numBuildings"); i++) { 79  Vector2 pos = Utilities.randomPos(-radius, radius); 80  pos = Utilities.floor(pos); 81  82  if (!posList.contains(pos)) { 83  posList.add(pos); 84  85  pos = Utilities.tilesToDistance(pos).add(origin); 86  87  Building b = new Building(this); 88  buildings.add(b); 89  90  String b_name = Utilities.randomChoice(buildingNames); 91  92  b.create(pos, b_name); 93  } 94  } 95  flag = new Building(this,true); 96  buildings.add(flag); 97  flag.create(origin, colour); 98  } 99  100  /** 101  * True as long as unharmed buildings remain, false otherwise. 102  * Changed for Assessment 2: 103  * - Added boolean return for Kill Quest functionality. 104  * - Renamed boolean for readability 105  * - Added functionality for changing Flag upon capture. 106  * @return the status of this college 107  */ 108  public boolean isAlive() { 109  if (!alive) { 110  return false; 111  } 112  boolean buildingsRemain = false; 113  for (int i = 0; i < buildings.size() - 1; i++) { 114  Building b = buildings.get(i); 115  if (b.isAlive()) { 116  buildingsRemain = true; 117  } 118  } 119  if (!buildingsRemain) { 120  getComponent(Pirate.class).kill(); 121  if(mostRecentAttacker != null){ 122  final Vector2 origin = getComponent(Transform.class).getPosition(); 123  flag.create(origin,mostRecentAttacker.getColour()); 124  getComponent(Pirate.class).setFactionId(mostRecentAttacker.getID()); 125  } 126  Player p = GameManager.getPlayer(); 127  p.plunder(25); 128  } 129  130  alive = buildingsRemain; 131  return alive; 132  } 133  134  /** 135  * Added for Assessment 2 136  * @return The Faction of the Pirate Component attached to this entity 137  */ 138  public Faction getFaction() { 139  return getComponent(Pirate.class).getFaction(); 140  } 141  142  /** 143  * Added for Assessment 2 144  * Sets the Faction which most recently attacked this College 145  * @param conqueror the Faction which most recently attacked this college 146  */ 147  public void setMostRecentAttacker(Faction conqueror){ 148  mostRecentAttacker = conqueror; 149  } 150  151  /** 152  * Added for Assessment 2 153  * @return copy of the transform's position 154  */ 155  public Vector2 getPosition() { 156  return getComponent(Transform.class).getPosition().cpy(); 157  } 158  159  /** 160  * Added for Assessment 2 161  * @param ship the Ship being checked 162  * @return displacement in form [magnitude,direction] between parsed ship and this college 163  */ 164  public ArrayList<Vector2> displacementFromShip(Ship ship) { 165  Vector2 shipLocale = ship.getPosition(); 166  Vector2 thisPosition = getPosition(); 167  168  float xDiff = shipLocale.x-thisPosition.x; 169  float yDiff = shipLocale.y-thisPosition.y; 170  171  Vector2 magnitude = new Vector2(Math.abs(xDiff),Math.abs(yDiff)); 172  Vector2 direction = new Vector2(xDiff,yDiff); 173  174  ArrayList<Vector2> displacement = new ArrayList<>(); 175  displacement.add(magnitude); 176  displacement.add(direction); 177  178  return displacement; 179  } 180  181  /** 182  * Added for Assessment 2 to meet requirements 183  * Calls shoot function of internal component 184  * @param startPos the position the cannonball is to be spawned at 185  * @param direction the direction the cannonball is to be fired in 186  */ 187  public void shoot(Vector2 startPos, Vector2 direction) { 188  getComponent(Pirate.class).shoot(startPos, direction); 189  } 190  191  /** 192  * Added for Assessment 2 193  * Systematically kills each building attached to this college, then marks the college as dead. 194  */ 195  public void killThisCollege(Faction conqueror) { 196  for (int i = 0; i < buildings.size() - 1; i++) { 197  Building b = buildings.get(i); 198  if (b.isAlive()) { 199  b.destroy(conqueror); 200  } 201  } 202  isAlive(); 203  } 204  205  /** 206  * Runs once per frame 207  * Changed for Assessment 2: 208  * - Added checks for if the college is in a specific range of the player 209  * - Added functionality where the college shoots at the player if on opposing factions and in range. 210  */ 211  @Override 212  public void update() { 213  super.update(); 214  isAlive(); 215  216  Player p = GameManager.getPlayer(); 217  long current = TimeUtils.millis() / 1000; 218  if ( (current > lastShootTime) && !getFaction().equals(p.getFaction()) ) { 219  220  ArrayList<Vector2> displacementToPlayer = displacementFromShip(p); 221  Vector2 distanceToPlayer = displacementToPlayer.get(0); 222  223  if( (distanceToPlayer.x < buffer*5) && (distanceToPlayer.y < buffer*5) ) { 224  float scaleFactor = Math.max( Math.abs(displacementToPlayer.get(1).x) , Math.abs(displacementToPlayer.get(1).y) ); 225  float xBuffer = (displacementToPlayer.get(1).x / scaleFactor)*buffer; 226  float yBuffer = (displacementToPlayer.get(1).y / scaleFactor)*buffer; 227  Vector2 bufferShift = new Vector2(xBuffer,yBuffer); 228  Vector2 bufferedStart = getPosition().add(bufferShift); 229  230  shoot(bufferedStart, displacementToPlayer.get(1)); 231  } 232  } 233  lastShootTime = current; 234  } 235 }