Level.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package pathgame.gameplay;
  2. import pathgame.algorithm.TravellingSalesAlg;
  3. import pathgame.logging.Logger;
  4. import pathgame.tilemap.TileMap;
  5. import pathgame.tilemap.TileMapGenerator;
  6. /**
  7. * A level of the game containing the current map and the current player
  8. *
  9. * @author julia
  10. */
  11. public final class Level
  12. {
  13. private final Player player = new Player();
  14. private int level = 1;
  15. private TileMap map = null;
  16. private boolean showScoreMenu = false;
  17. private boolean showAfterScore = false;
  18. /**
  19. * Contructor generating and initializing new Level and Player
  20. */
  21. public Level()
  22. {
  23. reset();
  24. }
  25. /**
  26. * Checks gamestate changes every gametick based on user input and game
  27. * events
  28. *
  29. * @param gamestate the gamestate
  30. */
  31. public void tick(Gamestate gamestate)
  32. {
  33. map.tick();
  34. if(gamestate.is(Gamestates.GAMEPLAY) && !showScoreMenu)
  35. {
  36. player.tick(map);
  37. }
  38. if(gamestate.is(Gamestates.GAMEPLAY) && !showScoreMenu && (player.hasLost() || player.hasWon()))
  39. {
  40. showScoreMenu = true;
  41. }
  42. else if(showScoreMenu && Keys.CONFIRM_KEY.getTime() == 1)
  43. {
  44. showScoreMenu = false;
  45. showAfterScore = true;
  46. }
  47. }
  48. /**
  49. * Loads the next level
  50. */
  51. public void nextLevel()
  52. {
  53. level++;
  54. reset();
  55. }
  56. /**
  57. * Resets the current level
  58. */
  59. public void reset()
  60. {
  61. int levelW = Math.round(16.0f + (level * 16.0f / 9.0f));
  62. int levelH = Math.round(9.0f + (level * 16.0f / 16.0f));
  63. int towns = Math.round(2.0f + level * 0.5f);
  64. map = TileMapGenerator.getMap(levelW, levelH, level, towns);
  65. player.reset(map.getHomeX(), map.getHomeY());
  66. player.setObjectivesAmount(map.getNumberOfTowns());
  67. int algoEnergy = TravellingSalesAlg.calcSalesPathLen(map, player);
  68. player.setEnergySupply(algoEnergy);
  69. Logger.onLevelReset(level, player, map);
  70. }
  71. /**
  72. * Returns the map of the current level
  73. *
  74. * @return the map of the current level
  75. */
  76. public TileMap getMap()
  77. {
  78. return map;
  79. }
  80. /**
  81. * Returns the player of the current level
  82. *
  83. * @return the player of the current level
  84. */
  85. public Player getPlayer()
  86. {
  87. return player;
  88. }
  89. /**
  90. * Returns the current level
  91. *
  92. * @return the current level
  93. */
  94. public int getLevel()
  95. {
  96. return level;
  97. }
  98. /**
  99. * Returns if the AfterScoreMenu is showed
  100. *
  101. * @return if the AfterScoreMenu is showed
  102. */
  103. public boolean isShowingAfterScore()
  104. {
  105. return showAfterScore;
  106. }
  107. /**
  108. * Sets if the AfterScoreMenu should be showed
  109. *
  110. * @param show sets if the AfterScore should be showed
  111. */
  112. public void setShowAfterScore(boolean show)
  113. {
  114. showAfterScore = show;
  115. }
  116. /**
  117. * Returns if the ScoreMenu is showed
  118. *
  119. * @return if the ScoreMenu is showed
  120. */
  121. public boolean isShowingScoreMenu()
  122. {
  123. return showScoreMenu;
  124. }
  125. }