Coverage Summary for Class: GameScreen (com.mozarellabytes.kroy.Screens)
Class | Method, % | Line, % |
---|---|---|
GameScreen | 0% (0/ 21) | 0% (0/ 156) |
GameScreen$1 | 0% (0/ 1) | 0% (0/ 1) |
GameScreen$MockitoMock$1073561621 | ||
GameScreen$MockitoMock$1073561621$auxiliary$0JPxx39z | ||
GameScreen$MockitoMock$1073561621$auxiliary$7sWjDa29 | ||
GameScreen$MockitoMock$1073561621$auxiliary$8Pgd3oK0 | ||
GameScreen$MockitoMock$1073561621$auxiliary$AXl0LfsR | ||
GameScreen$MockitoMock$1073561621$auxiliary$bztaD9zx | ||
GameScreen$MockitoMock$1073561621$auxiliary$c6PeSLiq | ||
GameScreen$MockitoMock$1073561621$auxiliary$CS1TtIws | ||
GameScreen$MockitoMock$1073561621$auxiliary$CU8GJvrI | ||
GameScreen$MockitoMock$1073561621$auxiliary$DlFTuROx | ||
GameScreen$MockitoMock$1073561621$auxiliary$f4v5lSlt | ||
GameScreen$MockitoMock$1073561621$auxiliary$i01V7ZcF | ||
GameScreen$MockitoMock$1073561621$auxiliary$IUEmcsyj | ||
GameScreen$MockitoMock$1073561621$auxiliary$KnHqYejg | ||
GameScreen$MockitoMock$1073561621$auxiliary$nDAZ2vJf | ||
GameScreen$MockitoMock$1073561621$auxiliary$osSwAjPj | ||
GameScreen$MockitoMock$1073561621$auxiliary$pQRS8tSs | ||
GameScreen$MockitoMock$1073561621$auxiliary$YObkrsCT | ||
GameScreen$MockitoMock$1073561621$auxiliary$YwdJk5k3 | ||
GameScreen$MockitoMock$1073561621$auxiliary$zFYPS8NH | ||
GameScreen$MockitoMock$1528415953 | ||
GameScreen$MockitoMock$1528415953$auxiliary$0qKsTB8I | ||
GameScreen$MockitoMock$1528415953$auxiliary$4JZP6MNo | ||
GameScreen$MockitoMock$1528415953$auxiliary$5s90LAN2 | ||
GameScreen$MockitoMock$1528415953$auxiliary$7Dq1984d | ||
GameScreen$MockitoMock$1528415953$auxiliary$d3xsAYaN | ||
GameScreen$MockitoMock$1528415953$auxiliary$f0uBMGFd | ||
GameScreen$MockitoMock$1528415953$auxiliary$h7kNx7NV | ||
GameScreen$MockitoMock$1528415953$auxiliary$hXurVuNS | ||
GameScreen$MockitoMock$1528415953$auxiliary$kV42gUL5 | ||
GameScreen$MockitoMock$1528415953$auxiliary$LOet9S5W | ||
GameScreen$MockitoMock$1528415953$auxiliary$OVg2OM3b | ||
GameScreen$MockitoMock$1528415953$auxiliary$pFpgOmRT | ||
GameScreen$MockitoMock$1528415953$auxiliary$sDIkDl4c | ||
GameScreen$MockitoMock$1528415953$auxiliary$t88yExh3 | ||
GameScreen$MockitoMock$1528415953$auxiliary$uQYXyBTj | ||
GameScreen$MockitoMock$1528415953$auxiliary$vTFAkJXi | ||
GameScreen$MockitoMock$1528415953$auxiliary$X3kONajM | ||
GameScreen$MockitoMock$1528415953$auxiliary$yxg1FSFv | ||
GameScreen$MockitoMock$1528415953$auxiliary$zCkzghyH | ||
GameScreen$PlayState | 0% (0/ 2) | 0% (0/ 2) |
total | 0% (0/ 24) | 0% (0/ 159) |
1 package com.mozarellabytes.kroy.Screens;
2
3 import com.badlogic.gdx.Gdx;
4 import com.badlogic.gdx.Input;
5 import com.badlogic.gdx.Screen;
6 import com.badlogic.gdx.graphics.*;
7 import com.badlogic.gdx.graphics.g2d.*;
8 import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
9 import com.badlogic.gdx.maps.MapLayers;
10 import com.badlogic.gdx.maps.tiled.*;
11 import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
12 import com.badlogic.gdx.math.Vector2;
13 import com.mozarellabytes.kroy.Entities.*;
14 import com.mozarellabytes.kroy.GameState;
15 import com.mozarellabytes.kroy.Kroy;
16 import com.mozarellabytes.kroy.Utilities.*;
17
18 import java.util.ArrayList;
19
20 /**
21 * The Screen that our game is played in.
22 * Accessed from MenuScreen when the user
23 * clicks the Start button, and exits when
24 * the player wins or loses the game
25 */
26 public class GameScreen implements Screen {
27
28 /** Instance of our game that allows us the change screens */
29 private final Kroy game;
30
31 /** Renders our tiled map */
32 private final OrthogonalTiledMapRenderer mapRenderer;
33
34 /** Camera to set the projection for the screen */
35 private final OrthographicCamera camera;
36
37 /** Renders shapes such as the health/reserve
38 * stat bars above entities */
39 private final ShapeRenderer shapeMapRenderer;
40
41 /** Stores the layers of our tiled map */
42 private final MapLayers mapLayers;
43
44 /** Stores the structures layers, stores the background layer */
45 private final int[] structureLayersIndices, backgroundLayerIndex;
46
47 /** Batch that has dimensions in line with the 40x25 map */
48 private final Batch mapBatch;
49
50 /** Used for shaking the camera when a bomb hits a truck */
51 private final CameraShake camShake;
52
53 /** Stores whether the game is running or is paused */
54 private PlayState state;
55
56 /**
57 * Deals with all the user interface on the screen
58 * that does not want to be inline with the map
59 * coordinates, e.g. big stat bars, buttons, pause
60 * screen
61 */
62 private final GUI gui;
63
64 /**
65 * Stores the progress through the game. It keeps
66 * track of trucks/fortresses and will end the game
67 * once an end game condition has been met
68 */
69 public final GameState gameState;
70
71 /** List of Fortresses currently active on the map */
72 private final ArrayList<Fortress> fortresses;
73
74 /** Where the FireEngines' spawn, refill and repair */
75 private final FireStation station;
76
77 /** The FireTruck that the user is currently drawing a path for */
78 public FireTruck selectedTruck;
79
80 /** The entity that the user has clicked on to show
81 * the large stats in the top left corner */
82 public Object selectedEntity;
83
84 /** Play when the game is being played
85 * Pause when the pause button is clicked */
86 public enum PlayState {
87 PLAY, PAUSE
88 }
89
90 /**
91 * Constructor which has the game passed in
92 *
93 * @param game LibGdx game
94 */
95 public GameScreen(Kroy game) {
96 this.game = game;
97
98 state = PlayState.PLAY;
99
100 camera = new OrthographicCamera();
101 camera.setToOrtho(false, Constants.VIEWPORT_WIDTH, Constants.VIEWPORT_HEIGHT);
102
103 TiledMap map = new TmxMapLoader().load("maps/YorkMap.tmx");
104 mapRenderer = new OrthogonalTiledMapRenderer(map, 1 / Constants.TILE_WxH);
105 mapRenderer.setView(camera);
106
107 shapeMapRenderer = new ShapeRenderer();
108 shapeMapRenderer.setProjectionMatrix(camera.combined);
109
110 gui = new GUI(game, this);
111
112 Gdx.input.setInputProcessor(new GameInputHandler(this, gui));
113
114 gameState = new GameState();
115 camShake = new CameraShake();
116
117 //Orders renderer to start rendering the background, then the player layer, then structures
118 mapLayers = map.getLayers();
119 backgroundLayerIndex = new int[]{mapLayers.getIndex("background")};
120
121 structureLayersIndices = new int[]{mapLayers.getIndex("structures"),
122 mapLayers.getIndex("structures2"),
123 mapLayers.getIndex("transparentStructures")};
124
125 station = new FireStation(3, 2);
126
127 spawn(FireTruckType.Ocean);
128 spawn(FireTruckType.Speed);
129
130 fortresses = new ArrayList<Fortress>();
131 fortresses.add(new Fortress(12, 18.5f, FortressType.Revs));
132 fortresses.add(new Fortress(30.5f, 17.5f, FortressType.Walmgate));
133 fortresses.add(new Fortress(16, 3.5f, FortressType.Clifford));
134
135 // sets the origin point to which all of the polygon's local vertices are relative to.
136 for (FireTruck truck : station.getTrucks()) {
137 truck.setOrigin(Constants.TILE_WxH / 2, Constants.TILE_WxH / 2);
138 }
139
140 mapBatch = mapRenderer.getBatch();
141
142 if (SoundFX.music_enabled) {
143 SoundFX.sfx_soundtrack.setVolume(.5f);
144 SoundFX.sfx_soundtrack.play();
145 }
146
147 }
148
149 @Override
150 public void show() {
151 }
152
153 @Override
154 public void render(float delta) {
155
156 camera.update();
157
158 mapRenderer.setView(camera);
159 mapRenderer.render(backgroundLayerIndex);
160
161 mapBatch.begin();
162
163 for (FireTruck truck : station.getTrucks()) {
164 truck.drawPath(mapBatch);
165 truck.drawSprite(mapBatch);
166 }
167
168 station.draw(mapBatch);
169
170 for (Fortress fortress : this.fortresses) {
171 fortress.draw(mapBatch);
172 }
173
174 mapBatch.end();
175
176 mapRenderer.render(structureLayersIndices);
177
178 shapeMapRenderer.begin(ShapeRenderer.ShapeType.Filled);
179
180 for (FireTruck truck : station.getTrucks()) {
181 truck.drawStats(shapeMapRenderer);
182 }
183
184 for (Fortress fortress : fortresses) {
185 fortress.drawStats(shapeMapRenderer);
186 for (Bomb bomb : fortress.getBombs()) {
187 bomb.drawBomb(shapeMapRenderer);
188 }
189 }
190
191 shapeMapRenderer.end();
192
193 gui.renderSelectedEntity(selectedEntity);
194
195 switch (state) {
196 case PLAY:
197 this.update(delta);
198 break;
199 case PAUSE:
200 // render dark background
201 Gdx.graphics.getGL20().glEnable(GL20.GL_BLEND);
202 shapeMapRenderer.begin(ShapeRenderer.ShapeType.Filled);
203 shapeMapRenderer.setColor(0, 0, 0, 0.5f);
204 shapeMapRenderer.rect(0, 0, this.camera.viewportWidth, this.camera.viewportHeight);
205 shapeMapRenderer.end();
206 gui.renderPauseScreenText();
207 }
208 gui.renderButtons();
209 }
210
211 /**
212 * Manages all of the updates/checks during the game
213 *
214 * @param delta The time in seconds since the last render
215 */
216 private void update(float delta) {
217 gameState.hasGameEnded(game);
218 CameraShake.update(delta, camera, new Vector2(camera.viewportWidth / 2f, camera.viewportHeight / 2f));
219
220 station.restoreTrucks();
221 station.checkForCollisions();
222 gameState.setTrucksInAttackRange(0);
223
224 for (int i = 0; i < station.getTrucks().size(); i++) {
225 FireTruck truck = station.getTruck(i);
226
227 truck.move();
228 truck.updateSpray();
229
230 // manages attacks between trucks and fortresses
231 for (Fortress fortress : this.fortresses) {
232 if (fortress.withinRange(truck.getVisualPosition())) {
233 fortress.attack(truck, true);
234 }
235 if (truck.fortressInRange(fortress.getPosition())) {
236 gameState.incrementTrucksInAttackRange();
237 truck.attack(fortress);
238 break;
239 }
240 }
241
242 // check if truck is destroyed
243 if (truck.getHP() <= 0) {
244 gameState.removeFireTruck();
245 station.destroyTruck(truck);
246 if (truck.equals(this.selectedTruck)) {
247 this.selectedTruck = null;
248 }
249 }
250 }
251
252 for (int i = 0; i < this.fortresses.size(); i++) {
253 Fortress fortress = this.fortresses.get(i);
254
255 boolean hitTruck = fortress.updateBombs();
256 if (hitTruck) {
257 camShake.shakeIt(.2f);
258 }
259
260 // check if fortress is destroyed
261 if (fortress.getHP() <= 0) {
262 gameState.addFortress();
263 this.fortresses.remove(fortress);
264 if (SoundFX.music_enabled) {
265 SoundFX.sfx_fortress_destroyed.play();
266 }
267 }
268
269 }
270
271 if (Gdx.input.isKeyPressed(Input.Keys.A)) {
272 if (gameState.getTrucksInAttackRange() > 0) {
273 SoundFX.playTruckAttack();
274 }
275 else {
276 SoundFX.stopTruckAttack();
277 }
278 }
279
280 System.out.println(SoundFX.isPlaying);
281
282 shapeMapRenderer.end();
283 shapeMapRenderer.setColor(Color.WHITE);
284
285 gui.renderSelectedEntity(selectedEntity);
286 }
287
288 @Override
289 public void resize(int width, int height) {
290
291 }
292
293 @Override
294 public void pause() {
295
296 }
297
298 @Override
299 public void resume() {
300
301 }
302
303 @Override
304 public void hide() {
305
306 }
307
308 @Override
309 public void dispose() {
310 mapRenderer.dispose();
311 shapeMapRenderer.dispose();
312 mapBatch.dispose();
313 SoundFX.sfx_soundtrack.stop();
314 }
315
316 /**
317 * Checks whether the player has clicked on a truck and sets that
318 * truck to selected truck and entity
319 *
320 * @param position coordinates of where the user clicked
321 * @return <code>true</code> if player clicks on a truck
322 * <code>false</code> otherwise
323 */
324 public boolean checkClick(Vector2 position) {
325 for (int i = this.station.getTrucks().size() - 1; i >= 0; i--) {
326 FireTruck selectedTruck = this.station.getTruck(i);
327 Vector2 truckTile = getTile(selectedTruck.getPosition());
328 if (position.equals(truckTile) &&!selectedTruck.getMoving()) {
329 this.selectedTruck = this.station.getTruck(i);
330 this.selectedEntity = this.station.getTruck(i);
331 return true;
332 }
333 }
334 return false;
335 }
336
337 /**
338 * Gets the coordinates of the tile that the truck is closest to
339 *
340 * @param position coordinates of truck
341 * @return coordinates of closest tile
342 */
343 private Vector2 getTile(Vector2 position) {
344 return new Vector2((float) Math.round((position.x)), (float) Math.round(position.y));
345 }
346
347 /**
348 * Checks whether the user has clicked on a the last tile in a
349 * truck's trail path and selects the truck as active truck and
350 * entity
351 *
352 * @param position the coordinates where the user clicked
353 * @return <code>true</code> if player clicks on the
354 * last tile in a truck's path
355 * <code>false</code> otherwise
356 */
357 public boolean checkTrailClick(Vector2 position) {
358 for (int i=this.station.getTrucks().size()-1; i>=0; i--) {
359 if (!this.station.getTruck(i).path.isEmpty()) {
360 if (position.equals(this.station.getTruck(i).path.last())) {
361 this.selectedTruck = this.station.getTruck(i);
362 this.selectedEntity = this.station.getTruck(i);
363 return true;
364 }
365 }
366 }
367 return false;
368 }
369
370 /**
371 * Checks whether the tile that the user is trying to add to the
372 * truck's path is on the road. This uses the custom "road"
373 * boolean property in the collisions layer within the tiled map
374 *
375 * @param x x coordinate of tile
376 * @param y y coordinate of tile
377 * @return <code>true</code> if the tile is a road
378 * <code>false</code> otherwise
379 */
380 public boolean isRoad(int x, int y) {
381 return ((TiledMapTileLayer) mapLayers.get("collisions")).getCell(x, y).getTile().getProperties().get("road").equals(true);
382 }
383
384 /**
385 * Changes from GameScreen to Control screen, passing "game" so that when
386 * the player exits the control screen, it knows to return to the Game
387 */
388 public void toControlScreen() {
389 game.setScreen(new ControlsScreen(game, this, "game"));
390 }
391
392 /** Exits the main game screen and goes to the menu, called when the home
393 * button is clicked */
394 public void toHomeScreen() {
395 game.setScreen(new MenuScreen(game));
396 SoundFX.sfx_soundtrack.dispose();
397 }
398
399 /**
400 * Creates a new FireEngine, plays a sound and adds it gameState to track
401 * @param type Type of truck to be spawned (Ocean, Speed)
402 */
403 private void spawn(FireTruckType type) {
404 SoundFX.sfx_truck_spawn.play();
405 station.spawn(new FireTruck(this, new Vector2(6,2), type));
406 gameState.addFireTruck();
407 }
408
409 /** Toggles between Play and Pause state when the Pause button is clicked */
410 public void changeState() {
411 if (this.state.equals(PlayState.PLAY)) {
412 this.state = PlayState.PAUSE;
413 } else {
414 this.state = PlayState.PLAY;
415 }
416 }
417
418 public FireStation getStation() {
419 return this.station;
420 }
421
422 public OrthographicCamera getCamera() {
423 return this.camera;
424 }
425
426 public ArrayList<Fortress> getFortresses() {
427 return this.fortresses;
428 }
429
430 public PlayState getState() {
431 return this.state;
432 }
433
434 }
435