Coverage Summary for Class: Fortress (com.mozarellabytes.kroy.Entities)
Class | Class, % | Method, % | Line, % |
---|---|---|---|
Fortress | 100% (1/ 1) | 76.9% (10/ 13) | 75% (30/ 40) |
1 package com.mozarellabytes.kroy.Entities;
2
3 import com.badlogic.gdx.graphics.Color;
4 import com.badlogic.gdx.graphics.g2d.Batch;
5 import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
6 import com.badlogic.gdx.math.Rectangle;
7 import com.badlogic.gdx.math.Vector2;
8
9 import com.mozarellabytes.kroy.Utilities.SoundFX;
10
11 import java.util.ArrayList;
12
13 public class Fortress {
14
15 /*** Fortress health, destroyed on zero */
16 private float HP;
17
18 /*** Position of the Fortress */
19 private final Vector2 position;
20
21 /*** Where the Fortress lies on the map */
22 private final Rectangle area;
23
24 /*** List of bombs that are active */
25 private final ArrayList<Bomb> bombs;
26
27 /*** Gives Fortress certain stats */
28 private final FortressType fortressType;
29
30 /**
31 * Constructs Fortress at certain position and
32 * of a certain type
33 *
34 * @param x x coordinate of Fortress (lower left point)
35 * @param y y coordinate of Fortress (lower left point)
36 * @param type Type of Fortress to give certain stats
37 */
38 public Fortress(float x, float y, FortressType type) {
39 this.fortressType = type;
40 this.position = new Vector2(x, y);
41 this.HP = type.getMaxHP();
42 this.bombs = new ArrayList<Bomb>();
43 this.area = new Rectangle(this.position.x - (float) this.fortressType.getW()/2, this.position.y - (float) this.fortressType.getH()/2,
44 this.fortressType.getW(), this.fortressType.getH());
45 }
46
47 /**
48 * Checks if the truck's position is within the attack range of the fortress
49 *
50 * @param targetPos the truck position being checked
51 * @return <code>true</code> if truck within range of fortress
52 * <code>false</code> otherwise
53 */
54 public boolean withinRange(Vector2 targetPos) {
55 return targetPos.dst(this.position) <= fortressType.getRange();
56 }
57
58 /**
59 * Generates bombs to attack the FireTruck with
60 * @param target FireTruck being attacked
61 * @param randomTarget whether the bomb hits every time or
62 * there is a chance it misses
63 */
64 public void attack(FireTruck target, boolean randomTarget) {
65 if (target.getTimeOfLastAttack() + fortressType.getDelay() < System.currentTimeMillis()) {
66 this.bombs.add(new Bomb(this, target, randomTarget));
67 target.setTimeOfLastAttack(System.currentTimeMillis());
68 if (SoundFX.music_enabled) {
69 SoundFX.sfx_fortress_attack.play();
70 }
71 }
72 }
73
74 /**
75 * Updates the position of all the bombs and checks whether
76 * they have hit their target. If they have, it should deal
77 * damage to the truck, remove the bomb and shake the screen
78 * @return <code>true</code> if bomb hits a truck
79 * <code>false</code> if bomb does nt hit a true
80 */
81 public boolean updateBombs() {
82 for (int i = 0; i < this.getBombs().size(); i++) {
83 Bomb bomb = this.getBombs().get(i);
84 bomb.updatePosition();
85 if (bomb.checkHit()) {
86 bomb.damageTruck();
87 this.removeBomb(bomb);
88 return true;
89 } else if (bomb.hasReachedTargetTile()) {
90 this.removeBomb(bomb);
91 }
92 }
93 return false;
94 }
95
96 /**
97 * Removes Bomb from bomb list. This
98 * occurs when the bomb hits or misses
99 *
100 * @param bomb bomb being removed
101 */
102 private void removeBomb(Bomb bomb) {
103 this.bombs.remove(bomb);
104 }
105
106 /**
107 * Draws the health bars above the Fortress
108 *
109 * @param shapeMapRenderer The renderer to be drawn to
110 */
111 public void drawStats(ShapeRenderer shapeMapRenderer) {
112 shapeMapRenderer.rect(this.getPosition().x - 0.26f, this.getPosition().y + 1.4f, 0.6f, 1.2f, Color.WHITE, Color.WHITE, Color.WHITE, Color.WHITE);
113 shapeMapRenderer.rect(this.getPosition().x - 0.13f, this.getPosition().y + 1.5f, 0.36f, 1f, Color.FIREBRICK, Color.FIREBRICK, Color.FIREBRICK, Color.FIREBRICK);
114 shapeMapRenderer.rect(this.getPosition().x - 0.13f, this.getPosition().y + 1.5f, 0.36f, this.getHP() / this.fortressType.getMaxHP() * 1f, Color.RED, Color.RED, Color.RED, Color.RED);
115 }
116
117 /**
118 * Draws the Fortress on the map
119 *
120 * @param mapBatch the renderer in line with the map
121 */
122 public void draw(Batch mapBatch) {
123 mapBatch.draw(this.getFortressType().getTexture(), this.getArea().x, this.getArea().y, this.getArea().width, this.getArea().height);
124 }
125
126 public Vector2 getPosition() {
127 return this.position;
128 }
129
130 public float getHP() {
131 return this.HP;
132 }
133
134 public void damage(float HP){
135 this.HP -= HP;
136 }
137
138 public Rectangle getArea() {
139 return this.area;
140 }
141
142 public FortressType getFortressType() {
143 return this.fortressType;
144 }
145
146 public ArrayList<Bomb> getBombs() {
147 return this.bombs;
148 }
149
150 }