Coverage Summary for Class: Bomb (com.mozarellabytes.kroy.Entities)

Class Class, % Method, % Line, %
Bomb 100% (1/ 1) 62.5% (5/ 8) 60% (15/ 25)


1 package com.mozarellabytes.kroy.Entities; 2  3 import com.badlogic.gdx.graphics.Color; 4 import com.badlogic.gdx.graphics.g2d.Sprite; 5 import com.badlogic.gdx.graphics.glutils.ShapeRenderer; 6 import com.badlogic.gdx.math.Interpolation; 7 import com.badlogic.gdx.math.Vector2; 8  9 import java.util.Random; 10  11 /** 12  * Bomb is a class created when a FireTruck is within 13  * a Fortress' range, and timer has passed. It will travel 14  * towards either the truck or a tile near the truck. 15  * If the bomb hits the truck, it will deal damage 16  */ 17 public class Bomb extends Sprite { 18  19  /** The target FireTruck that the bomb is heading towards */ 20  private final FireTruck target; 21  22  /** The position of the target at bomb creation */ 23  private final Vector2 truckPosition; 24  25  /** The start position of the bomb */ 26  private final Vector2 startPosition; 27  28  /** Current position of the bomb */ 29  private Vector2 currentPosition; 30  31  /** The tile where the bomb "lands" */ 32  private final Vector2 targetPosition; 33  34  /** The amount of damage that is inflicted on hit */ 35  private final float damage; 36  37  /** 38  * Constructs a bomb with the given source and target 39  * 40  * @param fortress Fortress that the bomb came from 41  * @param target FireTruck being targeted 42  * @param isRandom <code>true</code> bomb heads towards 43  * the target 44  * <code>false</code> chance that 45  * bomb doesnt head towards target 46  */ 47  public Bomb(Fortress fortress, FireTruck target, boolean isRandom) { 48  this.target = target; 49  this.truckPosition = new Vector2(getMiddleOfTile(target.getPosition())); 50  this.startPosition = new Vector2(fortress.getPosition()); 51  this.currentPosition = this.startPosition; 52  if (isRandom) { 53  this.targetPosition = getMiddleOfTile(generateBombTarget()); 54  } else { 55  this.targetPosition = getMiddleOfTile(this.truckPosition); 56  57  } 58  this.damage = fortress.getFortressType().getAP(); 59  } 60  61  /** 62  * Checks whether the bomb has hit the target truck 63  * 64  * @return <code>true</code> if the bomb and target truck's position 65  * are in the middle of the same tile 66  * <code>false</code> otherwise. 67  */ 68  public boolean checkHit() { 69  return targetPosition.equals(truckPosition) && getMiddleOfTile(this.currentPosition).equals(truckPosition); 70  } 71  72  /** 73  * Checks if the bomb has reached the calculated target yet 74  * 75  * @return <code>true</code> if bomb enters the tile that was targeted 76  * <code>false</code> otherwise 77  */ 78  public boolean hasReachedTargetTile() { 79  return (int) this.currentPosition.x == (int) this.targetPosition.x && (int) this.currentPosition.y == (int) this.targetPosition.y; 80  } 81  82  /** 83  * Updates the current bomb position depending on the Interpolation function. 84  * 85  */ 86  public void updatePosition() { 87  this.currentPosition = this.startPosition.interpolate(this.targetPosition, 0.03f, Interpolation.pow5Out); 88  } 89  90  /** 91  * Helper function to return the middle of the tile. It sets the trucks 92  * position to the middle of a tile and is needed by checkHit to see 93  * if a bomb and a truck are in the middle of the same tile 94  * 95  * @param position The whole values of the bottom left hand corner of a tile 96  * @return Position in the middle of the tile 97  */ 98  private Vector2 getMiddleOfTile(Vector2 position) { 99  return new Vector2((int) position.x + 0.5f, (int) position.y + 0.5f); 100  } 101  102  /** 103  * Damages the truck 104  */ 105  public void damageTruck() { 106  this.target.fortressDamage(this.damage); 107  } 108  109  110  /** 111  * Determines and returns the position where the bomb will 'land.' 112  * This is either the truck's current position or a tile near to the truck; 113  * this is so the bomb does not always hit the truck. 114  * 115  * @return Target either truck or random position near truck 116  */ 117  private Vector2 generateBombTarget() { 118  float xCoord = (int) (Math.random() * (((truckPosition.x + 1) - (truckPosition.x - 1) + 1))); 119  float yCoord = (int) (Math.random() * (((truckPosition.y + 1) - (truckPosition.y - 1) + 1))); 120  Vector2 positionNearTruck = new Vector2(truckPosition.x - 1 + xCoord, truckPosition.y - 1 + yCoord); 121  return new Random().nextBoolean() ? truckPosition : positionNearTruck; 122  } 123  124  /** 125  * Draws the bomb (a red circle) at the bomb's current position 126  * 127  * @param shapeMapRenderer The ShapeRenderer from GameScreen used to draw 128  * the bomb 129  */ 130  public void drawBomb(ShapeRenderer shapeMapRenderer) { 131  shapeMapRenderer.setColor(Color.RED); 132  shapeMapRenderer.circle(this.currentPosition.x, this.currentPosition.y, 0.2f, 40); 133  shapeMapRenderer.setColor(Color.WHITE); 134  } 135 } 136  137