Level.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. public final static float ALGORITHM_1 = 1.0f;
  14. public final static float ALGORITHM_2 = 1.1f;
  15. public final static float ALGORITHM_3 = 1.2f;
  16. public final static float ALGORITHM_4 = 1.3f;
  17. private final Player player = new Player();
  18. private int level = 1;
  19. private TileMap map = null;
  20. private int algorithm = 0;
  21. private boolean showScoreMenu = false;
  22. private boolean showAfterScore = false;
  23. /**
  24. * Contructor generating and initializing new Level and Player
  25. */
  26. public Level()
  27. {
  28. reset();
  29. }
  30. /**
  31. * Checks gamestate changes every gametick based on user input and game
  32. * events
  33. *
  34. * @param gamestate the gamestate
  35. */
  36. public void tick(Gamestate gamestate)
  37. {
  38. map.tick();
  39. if(gamestate.is(Gamestates.GAMEPLAY) && !showScoreMenu)
  40. {
  41. player.tick(map);
  42. }
  43. if(gamestate.is(Gamestates.GAMEPLAY) && !showScoreMenu && (player.hasLost() || player.hasWon()))
  44. {
  45. showScoreMenu = true;
  46. }
  47. else if(showScoreMenu && Keys.CONFIRM_KEY.getTime() == 1)
  48. {
  49. showScoreMenu = false;
  50. showAfterScore = true;
  51. }
  52. }
  53. /**
  54. * Loads the next level
  55. */
  56. public void nextLevel()
  57. {
  58. level++;
  59. reset();
  60. }
  61. /**
  62. * Resets the current level
  63. */
  64. public void reset()
  65. {
  66. int levelW = Math.round(16.0f + (level * 16.0f / 9.0f));
  67. int levelH = Math.round(9.0f + (level * 16.0f / 16.0f));
  68. int towns = Math.round(2.0f + level * 0.5f);
  69. map = TileMapGenerator.getMap(levelW, levelH, level, towns);
  70. player.reset(map.getHomeX(), map.getHomeY());
  71. player.setObjectivesAmount(map.getNumberOfTowns());
  72. algorithm = TravellingSalesAlg.calcSalesPathLen(map, player);
  73. player.setEnergySupply(Math.round(algorithm * ALGORITHM_4));
  74. Logger.onLevelReset(level, player, map);
  75. }
  76. /**
  77. * Returns the map of the current level
  78. *
  79. * @return the map of the current level
  80. */
  81. public TileMap getMap()
  82. {
  83. return map;
  84. }
  85. /**
  86. * Returns the player of the current level
  87. *
  88. * @return the player of the current level
  89. */
  90. public Player getPlayer()
  91. {
  92. return player;
  93. }
  94. /**
  95. * Returns the current level
  96. *
  97. * @return the current level
  98. */
  99. public int getLevel()
  100. {
  101. return level;
  102. }
  103. /**
  104. * Returns the energy use of the algorithm
  105. *
  106. * @return the energy use of the algorithm
  107. */
  108. public int getAlgorithmValue()
  109. {
  110. return algorithm;
  111. }
  112. /**
  113. * Returns if the AfterScoreMenu is showed
  114. *
  115. * @return if the AfterScoreMenu is showed
  116. */
  117. public boolean isShowingAfterScore()
  118. {
  119. return showAfterScore;
  120. }
  121. /**
  122. * Sets if the AfterScoreMenu should be showed
  123. *
  124. * @param show sets if the AfterScore should be showed
  125. */
  126. public void setShowAfterScore(boolean show)
  127. {
  128. showAfterScore = show;
  129. }
  130. /**
  131. * Returns if the ScoreMenu is showed
  132. *
  133. * @return if the ScoreMenu is showed
  134. */
  135. public boolean isShowingScoreMenu()
  136. {
  137. return showScoreMenu;
  138. }
  139. }