Coverage Summary for Class: GameScreen (com.mygdx.game.UI)
| Class | Class, % | Method, % | Line, % |
|---|---|---|---|
| GameScreen | 0% (0/1) | 0% (0/10) | 0% (0/386) |
1 package com.mygdx.game.UI; 2 3 import com.badlogic.gdx.Gdx; 4 import com.badlogic.gdx.Input; 5 import com.badlogic.gdx.graphics.OrthographicCamera; 6 import com.badlogic.gdx.graphics.Texture; 7 import com.badlogic.gdx.math.Interpolation; 8 import com.badlogic.gdx.scenes.scene2d.ui.Image; 9 import com.badlogic.gdx.scenes.scene2d.ui.Label; 10 import com.badlogic.gdx.scenes.scene2d.ui.Table; 11 import com.badlogic.gdx.scenes.scene2d.ui.Window; 12 import com.badlogic.gdx.utils.ScreenUtils; 13 import com.mygdx.game.Components.ComponentEvent; 14 import com.mygdx.game.Components.Pirate; 15 import com.mygdx.game.Entitys.Player; 16 import com.mygdx.game.Entitys.Ship; 17 import com.mygdx.game.Managers.*; 18 import com.mygdx.game.PirateGame; 19 import com.mygdx.game.Quests.Quest; 20 21 22 import java.util.ArrayList; 23 import java.util.Random; 24 25 import static com.mygdx.utils.Constants.*; 26 27 public class GameScreen extends Page { 28 public static Label healthLabel; 29 public static Label dosh; 30 public static Label ammo; 31 32 public static Label points; 33 public static Label questName; 34 public static Label questDesc; 35 36 private Label power_up_name1; 37 private Label power_up_time1; 38 private Label power_up_name2; 39 private Label power_up_time2; 40 private Label warn1; 41 private Label warn2; 42 43 44 public static float[] powers; 45 public static int[] durations; 46 public static float warn1_time, warn2_time; 47 public static int timer; 48 public static int num_powers; 49 public static int message1, message2; 50 private Texture texture; 51 private float interval; 52 private Random r = new Random(); 53 54 55 56 /*private final Label questComplete; 57 private float showTimer = 0; 58 // in seconds 59 private static final float showDuration = 1;*/ 60 61 /** 62 * Boots up the actual game: starts PhysicsManager, GameManager, EntityManager, 63 * loads texture atlases into ResourceManager. Draws quest and control info. 64 * 65 * @param parent PirateGame UI screen container 66 * @param id_map the resource id of the tile map 67 */ 68 public GameScreen(PirateGame parent, int id_map) { 69 super(parent); 70 INIT_CONSTANTS(); 71 PhysicsManager.Initialize(false); 72 73 /*int id_ship = ResourceManager.addTexture("ship.png"); 74 int id_map = ResourceManager.addTileMap("Map.tmx"); 75 int atlas_id = ResourceManager.addTextureAtlas("Boats.txt"); 76 int extras_id = ResourceManager.addTextureAtlas("UISkin/skin.atlas"); 77 int buildings_id = ResourceManager.addTextureAtlas("Buildings.txt"); 78 ResourceManager.loadAssets();*/ 79 80 81 GameManager.SpawnGame(id_map); 82 //QuestManager.addQuest(new KillQuest(c)); 83 84 EntityManager.raiseEvents(ComponentEvent.Awake, ComponentEvent.Start); 85 86 Window questWindow = new Window("Current Quest", parent.skin); 87 88 Quest q = QuestManager.currentQuest(); 89 Table t = new Table(); 90 questName = new Label("NAME", parent.skin); 91 t.add(questName); 92 t.row(); 93 questDesc = new Label("DESCRIPTION", parent.skin); 94 if (q != null) { 95 questName.setText(q.getName()); 96 questDesc.setText(q.getDescription()); 97 } 98 99 t.add(questDesc).left(); 100 questWindow.add(t); 101 actors.add(questWindow); 102 103 Table t1 = new Table(); 104 t1.top().right(); 105 t1.setFillParent(true); 106 actors.add(t1); 107 108 Window tutWindow = new Window("Controls", parent.skin); 109 Table table = new Table(); 110 tutWindow.add(table); 111 t1.add(tutWindow); 112 113 table.add(new Label("Move with", parent.skin)).top().left(); 114 table.add(new Image(parent.skin, "key-w")); 115 table.add(new Image(parent.skin, "key-s")); 116 table.add(new Image(parent.skin, "key-a")); 117 table.add(new Image(parent.skin, "key-d")); 118 table.row(); 119 table.add(new Label("Shoot in direction of mouse", parent.skin)).left(); 120 //table.add(new Image(parent.skin, "space")); 121 table.add(new Image(parent.skin, "mouse")); 122 table.row(); 123 table.add(new Label("Shoot in direction of ship", parent.skin)).left(); 124 table.add(new Image(parent.skin, "space")); 125 table.row(); 126 table.add(new Label("Quit", parent.skin)).left(); 127 table.add(new Image(parent.skin, "key-esc")); 128 table.row(); 129 table.add(new Label("Shop", parent.skin)).left(); 130 table.add(new Image(parent.skin, "key-e")); 131 132 powers = new float[6]; 133 durations = new int[]{20, 30, 25, 20, 15, 5}; 134 num_powers = 0; 135 message1 = -1; 136 message2 = -1; 137 warn1_time = 0; 138 warn2_time = 0; 139 interval = 0f; 140 GameManager.ships.get(1).setHealth(0); 141 GameManager.ships.get(2).setHealth(0); 142 143 } 144 145 private float accumulator; 146 147 /** 148 * Called every frame calls all other functions that need to be called every frame by rasing events and update methods 149 * 150 * @param delta delta time 151 */ 152 @Override 153 public void render(float delta) { 154 ScreenUtils.clear(BACKGROUND_COLOUR.x, BACKGROUND_COLOUR.y, BACKGROUND_COLOUR.z, 1); 155 156 EntityManager.raiseEvents(ComponentEvent.Update, ComponentEvent.Render); 157 158 accumulator += EntityManager.getDeltaTime(); 159 160 // fixed update loop so that physics manager is called regally rather than somewhat randomly 161 while (accumulator >= PHYSICS_TIME_STEP) { 162 PhysicsManager.update(); 163 accumulator -= PHYSICS_TIME_STEP; 164 } 165 166 // show end screen if esc is pressed 167 if (Gdx.input.isKeyJustPressed(Input.Keys.ESCAPE)) { 168 parent.setScreen(parent.quitConfirm); 169 } 170 if (Gdx.input.isKeyJustPressed(Input.Keys.E)) { 171 parent.setScreen(parent.shop); 172 } 173 174 GameManager.update(); 175 super.render(0); 176 } 177 178 /** 179 * disposed of all stuff it something is missing from this method you will get memory leaks 180 */ 181 @Override 182 public void dispose() { 183 super.dispose(); 184 ResourceManager.cleanUp(); 185 EntityManager.cleanUp(); 186 RenderingManager.cleanUp(); 187 PhysicsManager.cleanUp(); 188 } 189 190 /** 191 * Resize camera, effectively setting the viewport to display game assets 192 * at pixel ratios other than 1:1. 193 * 194 * @param width of camera viewport 195 * @param height of camera viewport 196 */ 197 @Override 198 public void resize(int width, int height) { 199 //((Table) actors.get(0)).setFillParent(false); 200 super.resize(width, height); 201 OrthographicCamera cam = RenderingManager.getCamera(); 202 cam.viewportWidth = width / ZOOM; 203 cam.viewportHeight = height / ZOOM; 204 cam.update(); 205 206 // ((Table) actors.get(0)).setFillParent(true); 207 } 208 209 /** 210 * Update the UI with new values for health, quest status, etc. 211 * also called once per frame but used for actors by my own convention 212 */ 213 @Override 214 protected void update() { 215 super.update(); 216 Player p = GameManager.getPlayer(); 217 boolean reward_powerUp = p.getReward_powerUp(); 218 219 if (reward_powerUp == true){ 220 rewardPowerUp(); 221 p.setReward_powerUp(false); 222 } 223 224 powerUp(); 225 if (timer ==60){ 226 timer =0; 227 p.getComponent(Pirate.class).addPoints(1); 228 } 229 else{ 230 timer++; 231 } 232 healthLabel.setText(String.valueOf(p.getHealth())); 233 dosh.setText(String.valueOf(p.getPlunder())); 234 ammo.setText(String.valueOf(p.getAmmo())); 235 points.setText(String.valueOf(p.getComponent(Pirate.class).getPoints())); 236 if (!QuestManager.anyQuests()) { 237 parent.end.win(); 238 parent.setScreen(parent.end);} 239 else if(!p.isAlive()){ 240 parent.setScreen(parent.end); 241 } 242 else { 243 Quest q = QuestManager.currentQuest(); 244 /*if(Objects.equals(prevQuest, "")) { 245 prevQuest = q.getDescription(); 246 } 247 if(!Objects.equals(prevQuest, q.getDescription())) { 248 questComplete.setText("Quest Competed"); 249 prevQuest = ""; 250 }*/ 251 questName.setText(q.getName()); 252 questDesc.setText(q.getDescription()); 253 } 254 /*if(!questComplete.getText().equals("")) { 255 showTimer += EntityManager.getDeltaTime(); 256 } 257 if(showTimer >= showDuration) { 258 showTimer = 0; 259 questComplete.setText(""); 260 }*/ 261 //createMessage(); 262 263 } 264 265 /** 266 * Draw UI elements showing player health, plunder, and ammo. 267 */ 268 @Override 269 protected void CreateActors() { 270 Table table = new Table(); 271 table.setFillParent(true); 272 actors.add(table); 273 274 275 table.add(new Image(parent.skin.getDrawable("heart"))).top().left().size(1.25f * TILE_SIZE); 276 healthLabel = new Label("N/A", parent.skin); 277 table.add(healthLabel).top().left().size(50); 278 279 table.row(); 280 table.setDebug(false); 281 282 table.add(new Image(parent.skin.getDrawable("coin"))).top().left().size(1.25f * TILE_SIZE); 283 dosh = new Label("N/A", parent.skin); 284 table.add(dosh).top().left().size(50); 285 286 table.row(); 287 table.add(new Image(ResourceManager.getTexture("points.png"))).top().left().size(1.25f * TILE_SIZE); 288 points = new Label("N/A", parent.skin); 289 table.add(points).top().left().size(50); 290 291 table.row(); 292 table.add(new Image(parent.skin.getDrawable("ball"))).top().left().size(1.25f * TILE_SIZE); 293 ammo = new Label("N/A", parent.skin); 294 table.add(ammo).top().left().size(50); 295 296 table.top().left(); 297 298 Table table_message = new Table(); 299 table_message.setFillParent(true); 300 actors.add(table_message); 301 302 303 // Power Up Stuff 304 305 power_up_name1 = new Label("", parent.skin); 306 power_up_time1 = new Label("", parent.skin); 307 power_up_name2 = new Label("", parent.skin); 308 power_up_time2 = new Label("", parent.skin); 309 310 warn1 = new Label("", parent.skin); 311 warn2 = new Label("", parent.skin); 312 313 table_message.add(warn2).bottom().right().size(60); 314 table_message.row(); 315 316 table_message.add(warn1).bottom().right().size(60); 317 table_message.row(); 318 319 table_message.add(power_up_name2).bottom().right().size(60); 320 table_message.row(); 321 322 table_message.add(power_up_time2).bottom().right().size(60); 323 table_message.row(); 324 325 table_message.add(power_up_name1).bottom().right().size(60); 326 table_message.row(); 327 328 table_message.add(power_up_time1).bottom().right().size(60); 329 table_message.row(); 330 331 table_message.bottom(); 332 generatePowerUpTable(); 333 } 334 335 /** 336 * 337 * @param type The type of warning 338 */ 339 private void send_warn(int type){ 340 if(type == 1 ) { 341 warn1_time = 5; 342 warn1.setText("*You cannot have 2 identical power-ups at the same time*"); 343 } 344 else{ 345 warn2_time=5; 346 warn2.setText("*You can only have maximum 2 power-ups at the same time *"); 347 } 348 } 349 350 /** 351 * Reward the player with a powerup 352 */ 353 private void rewardPowerUp(){ 354 ArrayList<Ship> ships = GameManager.getShips(); 355 int j = r.nextInt(5); 356 switch (j){ 357 case 0: 358 // TempImmortality 359 if (PowerupScreen.isImortal && num_powers < 2) { 360 if (message1 == -1) { 361 power_up_name1.setText("Power Up - Temporary Immortality"); 362 message1 = 0; 363 } else { 364 power_up_name2.setText("Power Up - Temporary Immortality"); 365 message2 = 0; 366 } 367 ships.get(0).tempImmortality(true); 368 powers[0] = durations[0]; 369 num_powers++; 370 interval = 0; 371 } 372 else if (powers[0] > 0 && num_powers < 2) 373 send_warn(1); 374 else if (num_powers==2) 375 send_warn(2); 376 377 break; 378 case 1: 379 // BiggerDamage 380 if (PowerupScreen.isIncreasedDamage && num_powers < 2){ 381 if (message1==-1){ 382 power_up_name1.setText("Power Up - Bigger Damage"); 383 message1 = 1; 384 } 385 else{ 386 power_up_name2.setText("Power Up - Bigger Damage"); 387 message2 = 1; 388 } 389 ships.get(0).biggerDamage(true); 390 powers[1] = durations[1]; 391 num_powers++; 392 } 393 else if (powers[1] > 0 && num_powers < 2) 394 send_warn(1); 395 else if (num_powers==2) 396 send_warn(2); 397 break; 398 case 2: 399 // EightDirections 400 if (PowerupScreen.isMultiDamage && num_powers < 2){ 401 if (message1==-1){ 402 power_up_name1.setText("Power Up - Shoot All 8 Directions"); 403 message1 = 2; 404 } 405 else{ 406 power_up_name2.setText("Power Up - Shoot All 8 Directions"); 407 message2 = 2; 408 } 409 ships.get(0).shoot8Directions(true); 410 powers[2] = durations[2]; 411 num_powers++; 412 } 413 else if (powers[2] > 0 && num_powers < 2) 414 send_warn(1); 415 else if (num_powers==2) 416 send_warn(2); 417 break; 418 case 3: 419 // UnlimitedAmmo 420 if (PowerupScreen.isUnlimitedAmmo && num_powers < 2){ 421 if (message1==-1){ 422 power_up_name1.setText("Power Up - Unlimited Ammo"); 423 message1 = 3; 424 } 425 else{ 426 power_up_name2.setText("Power Up - Unlimited Ammo"); 427 message2 = 3; 428 } 429 ships.get(0).unlimitedAmmo(true); 430 powers[3] = durations[3]; 431 num_powers++; 432 } 433 434 else if (powers[3] > 0 && num_powers < 2) 435 send_warn(1); 436 437 else if (num_powers==2) 438 send_warn(2); 439 440 break; 441 case 4: 442 // FreezeEnemies 443 if (PowerupScreen.isFreezeEnemy && num_powers < 2){ 444 if (message1==-1){ 445 power_up_name1.setText("Power Up - Freeze Enemies"); 446 message1 = 4; 447 } 448 else{ 449 power_up_name2.setText("Power Up - Freeze Enemies"); 450 message2 = 4; 451 } 452 for(int i=1; i<ships.size();i++){ 453 ships.get(i).setFreeze(true); 454 } 455 powers[4] = durations[4]; 456 num_powers++; 457 } 458 else if (powers[4] > 0 && num_powers < 2) 459 send_warn(1); 460 else if (num_powers==2) 461 send_warn(2); 462 break; 463 464 default: 465 break; 466 } 467 } 468 469 /** 470 * Generate the table that displays powerups 471 */ 472 private void generatePowerUpTable(){ 473 Table t2 = new Table(); 474 t2.bottom().right(); 475 t2.setFillParent(true); 476 actors.add(t2); 477 478 Window powerWindow = new Window("Powerups", parent.skin); 479 Table powerTable = new Table(); 480 powerWindow.add(powerTable); 481 t2.row(); 482 t2.add(powerWindow).right(); 483 484 powerTable.add(new Image(ResourceManager.getTexture("powerups/powerup1.png"))); 485 486 487 powerTable.add(new Image(ResourceManager.getTexture("powerups/powerup2.png"))); 488 489 490 powerTable.add(new Image(ResourceManager.getTexture("powerups/powerup3.png"))); 491 492 493 powerTable.add(new Image(ResourceManager.getTexture("powerups/powerup4.png"))); 494 495 496 powerTable.add(new Image(ResourceManager.getTexture("powerups/powerup5.png"))); 497 498 499 500 501 } 502 503 /** 504 * Check if the player wants to use a powerup and then give them that power 505 */ 506 private void powerUp(){ 507 ArrayList<Ship> ships = GameManager.getShips(); 508 float threshold = 0.1f; 509 interval += Gdx.graphics.getDeltaTime(); 510 if(Gdx.input.isKeyPressed(Input.Keys.NUM_1)) { 511 // TempImmortality 512 if (PowerupScreen.isImortal && num_powers < 2) { // If owned 513 if (message1 == -1) { 514 power_up_name1.setText("Power Up - Temporary Immortality"); 515 message1 = 0; 516 } else { 517 power_up_name2.setText("Power Up - Temporary Immortality"); 518 message2 = 0; 519 } 520 ships.get(0).tempImmortality(true); 521 PowerupScreen.isImortal = false; 522 powers[0] = durations[0]; 523 num_powers++; 524 interval = 0; 525 } else if (powers[0] > 0 && num_powers < 2) { 526 if (interval>threshold){ 527 send_warn(1); 528 interval = 0; 529 } 530 } else if (num_powers==2) { 531 if (interval>threshold){ 532 send_warn(2); 533 interval = 0; 534 } 535 } 536 } 537 if(Gdx.input.isKeyPressed(Input.Keys.NUM_2)) { 538 // BiggerDamage 539 if (PowerupScreen.isIncreasedDamage && num_powers < 2){ 540 if (message1==-1){ 541 power_up_name1.setText("Power Up - Bigger Damage"); 542 message1 = 1; 543 } 544 else{ 545 power_up_name2.setText("Power Up - Bigger Damage"); 546 message2 = 1; 547 } 548 ships.get(0).biggerDamage(true); 549 PowerupScreen.isIncreasedDamage = false; 550 powers[1] = durations[1]; 551 num_powers++; 552 interval = 0; 553 } 554 else if (powers[1] > 0 && num_powers < 2) { 555 if (interval>threshold){ 556 send_warn(1); 557 interval = 0; 558 } 559 } else if (num_powers==2) { 560 if (interval>threshold){ 561 send_warn(2); 562 interval = 0; 563 } 564 } 565 566 } 567 if(Gdx.input.isKeyPressed(Input.Keys.NUM_3)) { 568 // EightDirections 569 if (PowerupScreen.isMultiDamage && num_powers < 2){ 570 if (message1==-1){ 571 power_up_name1.setText("Power Up - Shoot All 8 Directions"); 572 message1 = 2; 573 } 574 else{ 575 power_up_name2.setText("Power Up - Shoot All 8 Directions"); 576 message2 = 2; 577 } 578 ships.get(0).shoot8Directions(true); 579 PowerupScreen.isMultiDamage = false; 580 powers[2] = durations[2]; 581 num_powers++; 582 interval =0; 583 } 584 else if (powers[2] > 0 && num_powers < 2) { 585 if (interval>threshold){ 586 send_warn(1); 587 interval = 0; 588 } 589 } else if (num_powers==2) { 590 if (interval>threshold){ 591 send_warn(2); 592 interval = 0; 593 } 594 } 595 596 } 597 if(Gdx.input.isKeyPressed(Input.Keys.NUM_4)) { 598 // UnlimitedAmmo 599 if (PowerupScreen.isUnlimitedAmmo && num_powers < 2){ 600 if (message1==-1){ 601 power_up_name1.setText("Power Up - Unlimited Ammo"); 602 message1 = 3; 603 } 604 else{ 605 power_up_name2.setText("Power Up - Unlimited Ammo"); 606 message2 = 3; 607 } 608 ships.get(0).unlimitedAmmo(true); 609 PowerupScreen.isUnlimitedAmmo = false; 610 powers[3] = durations[3]; 611 num_powers++; 612 interval = 0; 613 } 614 615 else if (powers[3] > 0 && num_powers < 2) { 616 if (interval>threshold){ 617 send_warn(1); 618 interval = 0; 619 } 620 } else if (num_powers==2) { 621 if (interval>threshold){ 622 send_warn(2); 623 interval = 0; 624 } 625 } 626 } 627 628 if(Gdx.input.isKeyPressed(Input.Keys.NUM_5)) { 629 // FreezeEnemies 630 if (PowerupScreen.isFreezeEnemy && num_powers < 2){ 631 if (message1==-1){ 632 power_up_name1.setText("Power Up - Freeze Enemies"); 633 message1 = 4; 634 } 635 else{ 636 power_up_name2.setText("Power Up - Freeze Enemies"); 637 message2 = 4; 638 } 639 for(int i=1; i<ships.size();i++){ 640 ships.get(i).setFreeze(true); 641 } 642 PowerupScreen.isFreezeEnemy = false; 643 644 powers[4] = durations[4]; 645 num_powers++; 646 interval = 0; 647 } 648 else if (powers[4] > 0 && num_powers < 2) { 649 if (interval>threshold){ 650 send_warn(1); 651 interval = 0; 652 } 653 } else if (num_powers==2) { 654 if (interval>threshold){ 655 send_warn(2); 656 interval = 0; 657 } 658 } 659 660 } 661 662 for(int i=0 ; i<6 ; i++){ 663 if (powers[i]>=5){ 664 powers[i] -= Gdx.graphics.getDeltaTime(); 665 666 } 667 else if( powers[i]>0 && powers[i]<5){ 668 powers[i] -= Gdx.graphics.getDeltaTime(); 669 switch (i){ 670 case 0: 671 ships.get(0).tempImmortality(false); 672 break; 673 case 1: 674 ships.get(0).biggerDamage(false); 675 break; 676 case 2: 677 ships.get(0).shoot8Directions(false); 678 break; 679 case 3: 680 ships.get(0).unlimitedAmmo(false); 681 break; 682 case 4: 683 for(int j=1; j<ships.size();j++) 684 ships.get(j).setFreeze(false); 685 break; 686 default: 687 break; 688 } 689 } 690 else if( powers[i]<0){ 691 powers[i] = 0; 692 num_powers--; 693 } 694 } 695 696 if(message1!=-1){ 697 if (powers[message1]>5) 698 power_up_time1.setText(String.valueOf((int)powers[message1]-5)); 699 else if(powers[message1]<0){ 700 power_up_name1.setText(""); 701 power_up_time1.setText(""); 702 message1 = -1; 703 } 704 else{ 705 power_up_time1.setText("TIME UP"); 706 if (message1==5) 707 power_up_time1.setText("ACTIVATED"); 708 } 709 } 710 711 if(message2!=-1){ 712 if (powers[message2]>5) 713 power_up_time2.setText(String.valueOf((int)powers[message2]-5)); 714 else if(powers[message2]<0){ 715 power_up_name2.setText(""); 716 power_up_time2.setText(""); 717 message2 = -1; 718 } 719 else{ 720 power_up_time2.setText("TIME UP"); 721 if (message2==5) 722 power_up_time2.setText("ACTIVATED"); 723 } 724 } 725 726 if (warn1_time>0){ 727 warn1_time -= Gdx.graphics.getDeltaTime(); 728 } 729 else if (warn1_time<0){ 730 warn1_time = 0; 731 warn1.setText(""); 732 } 733 734 if (warn2_time>0){ 735 warn2_time -= Gdx.graphics.getDeltaTime(); 736 } 737 else if (warn2_time<0){ 738 warn2_time = 0; 739 warn2.setText(""); 740 } 741 } 742 } 743