Coverage Summary for Class: FireStation (com.mozarellabytes.kroy.Entities)
Class | Class, % | Method, % | Line, % |
---|---|---|---|
FireStation | 100% (1/ 1) | 72.7% (8/ 11) | 90% (54/ 60) |
1 package com.mozarellabytes.kroy.Entities;
2
3 import com.badlogic.gdx.Gdx;
4 import com.badlogic.gdx.graphics.Texture;
5 import com.badlogic.gdx.graphics.g2d.Batch;
6 import com.badlogic.gdx.math.Vector2;
7 import java.util.ArrayList;
8 import com.mozarellabytes.kroy.Utilities.SoundFX;
9
10 /**
11 * FireStation is a class created when it is called from GameScreen.
12 * This class contains the location and sprite of the FireStation.
13 * The FireStation spawns, repairs and refills the firetrucks and
14 * ensures that trucks do not collide
15 */
16
17 public class FireStation {
18
19 /**
20 * X and Y co-ordinates of the FireStation's position on the game screen
21 * in tiles
22 */
23 private final int x,y;
24
25 /** The tile where new FireTrucks are spawned */
26 private final Vector2 spawnTile;
27
28 /** A tile inside the station where a truck can be repaired and refilled */
29 private final Vector2 bayTile1;
30
31 /** A second tile inside the station where a truck can be repaired and
32 * refilled */
33 private final Vector2 bayTile2;
34
35 /** The sprite image for the station */
36 private final Texture texture;
37
38 /** List of active fire trucks
39 * @link FireTruck */
40 private final ArrayList<FireTruck> trucks;
41
42 /**
43 * Constructs the Firestation with at a given position with locations
44 * for the repair and refill tiles and the spawn tiles.
45 *
46 * @param x x coordinate of Station in tiles (lower left point)
47 * @param y y coordinate of Station in tiles (lower left point)
48 */
49 public FireStation(int x, int y) {
50 this.x = x;
51 this.y = y;
52 this.spawnTile = new Vector2(x+3, y);
53 this.bayTile1 = new Vector2(x+1, y);
54 this.bayTile2 = new Vector2(x+2, y);
55 this.texture = new Texture(Gdx.files.internal("sprites/station/station.png"));
56 this.trucks = new ArrayList<FireTruck>();
57 }
58
59 /**
60 * Creates a fire truck of type specified from FireTruckType. It signals to
61 * the game state that a truck has been created and add the truck to the
62 * arraylist this.truck so the game screen can iterate through all active trucks
63 * @param truck truck to add to the arrayList of active trucks
64 *
65 */
66 public void spawn(FireTruck truck) {
67 if (SoundFX.music_enabled) {
68 SoundFX.sfx_truck_spawn.play();
69 }
70 this.trucks.add(truck);
71 }
72
73 /**
74 * Calls the repair and refill methods. When a truck is within the station
75 * (the trucks position is the same as one of the station's bay tiles) the
76 * truck will repair and refill at the same time.
77 */
78 public void restoreTrucks() {
79 for (FireTruck truck : this.trucks) {
80 if (truck.getPosition().equals(this.bayTile1) || truck.getPosition().equals(this.bayTile2)) {
81 repair(truck);
82 refill(truck);
83 }
84 }
85 }
86
87 /**
88 * Increments the truck's HP until the truck's HP equals the truck's maximum
89 * HP
90 * @param truck truck that is being refilled
91 */
92 private void refill(FireTruck truck) {
93 if (truck.getReserve() < truck.type.getMaxReserve()) {
94 truck.refill(Math.min(0.06f, truck.getType().getMaxReserve() - truck.getReserve()));
95 }
96 }
97
98 /**
99 * Increments the truck's reserve until the truck's reserve equals the
100 * truck's maximum reserve
101 * @param truck truck that is being repaired
102 */
103 private void repair(FireTruck truck) {
104 if (truck.getHP() < truck.type.getMaxHP()) {
105 truck.repair(Math.min(0.04f, truck.getType().getMaxHP() - truck.getHP()));
106 }
107 }
108
109 /**
110 * Called when a truck's HP reaches 0, it removes the truck from the
111 * array list of active trucks and the game screen.
112 *
113 * @param truck truck that is being removed from the arrayList of active trucks
114 */
115 public void destroyTruck(FireTruck truck) {
116 this.trucks.remove(truck);
117 }
118
119 /**
120 * Checks that no more than one truck occupies a tile at a time by checking trucks
121 * are not moving towards each other and that a moving truck is not going to go onto
122 * the same tile as a stationary truck. If two trucks are going to collide reset
123 * trucks is called.
124 */
125 public void checkForCollisions() {
126 for (FireTruck truck : trucks) {
127 for (FireTruck truck2 : trucks) {
128 if (!(truck.equals(truck2))) {
129 if (!truck.trailPath.isEmpty() && !truck.getPosition().equals(spawnTile)) {
130 Vector2 truck2tile = new Vector2(Math.round(truck2.getPosition().x), Math.round(truck2.getPosition().y));
131 Vector2 truckstile = new Vector2((float)Math.floor(truck2.getPosition().x),(float) Math.floor(truck2.getPosition().y));
132 if (!truck2.trailPath.isEmpty() && truck.trailPath.first().equals(truck2.trailPath.first())) {
133 truck.setCollision();
134 truck2.setCollision();
135 resetTruck(truck, truck2);
136 } else if (truck.trailPath.first().equals(truck2tile)) {
137 resetTruck(truck, truck2);
138 truck.trailPath.clear();
139 truck2.trailPath.clear();
140 } else if (truck.trailPath.first().equals(truckstile)) {
141 resetTruck(truck, truck2);
142 }
143 }
144 }
145 }
146 }
147 }
148
149 /** Resets two trucks - is called when both trucks are moving towards each other
150 * It removes their paths so they halt on the tile of the collision. It then adds
151 * the nearest tile to their path, the trucks move to this tile so that after the
152 * collision the trucks are positioned at the centre of adjacent tiles.
153 *
154 * @param truck one truck involved in the collision
155 * @param truck2 the second truck involved in the collision
156 */
157 private void resetTruck(FireTruck truck, FireTruck truck2) {
158 if (SoundFX.music_enabled) {
159 SoundFX.sfx_horn.play();
160 }
161
162 Vector2 hold = truck.trailPath.first();
163
164 truck.resetPath();
165 truck.addTileToPath(truck.getPosition());
166 truck.addTileToPath(new Vector2 ((float)Math.floor(truck.getX()),(float)Math.floor(truck.getY())));
167
168 truck2.resetPath();
169 truck2.addTileToPath(truck2.getPosition());
170 truck2.addTileToPath(hold);
171 }
172
173
174 /** Draws the firetruck to the gameScreen
175 * @param mapBatch batch being used to render to the gameScreen */
176 public void draw(Batch mapBatch) {
177 mapBatch.draw(this.texture, this.x, this.y, 5, 3);
178 }
179
180 public ArrayList<FireTruck> getTrucks() {
181 return this.trucks;
182 }
183
184 public FireTruck getTruck(int i) {
185 return this.trucks.get(i);
186 }
187 }