Menu.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package pathgame.gameplay.menu;
  2. import pathgame.gameplay.Gamestate;
  3. import pathgame.gameplay.Gamestates;
  4. import pathgame.gameplay.Keys;
  5. import pathgame.gameplay.Level;
  6. /**
  7. * A container for holding and manages all kinds of menus
  8. *
  9. * @author julia
  10. */
  11. public class Menu
  12. {
  13. private final static int MAIN_ID;
  14. private final static int ESCAPE_ID;
  15. private final static int OPTION_ID;
  16. private final static int CHARACTER_ID;
  17. private final static int AFTER_SCORE_ID;
  18. static
  19. {
  20. int id = 0;
  21. MAIN_ID = id++;
  22. ESCAPE_ID = id++;
  23. OPTION_ID = id++;
  24. CHARACTER_ID = id++;
  25. AFTER_SCORE_ID = id++;
  26. }
  27. private final BaseMenu[] menus = new BaseMenu[]
  28. {
  29. new MainMenu(MAIN_ID, OPTION_ID, CHARACTER_ID),
  30. new EscMenu(ESCAPE_ID, MAIN_ID),
  31. new OptionMenu(OPTION_ID, MAIN_ID),
  32. new CharacterMenu(CHARACTER_ID, MAIN_ID),
  33. new AfterScoreMenu(AFTER_SCORE_ID, MAIN_ID)
  34. };
  35. private int currentIndex = 0;
  36. /**
  37. * Updates the menu state every gametick based on user input
  38. *
  39. * @param gamestate the gamestate
  40. * @param level a level containing the current map and the current player
  41. */
  42. public void tick(Gamestate gamestate, Level level)
  43. {
  44. if(gamestate.getState() == Gamestates.MENU)
  45. {
  46. currentIndex = menus[currentIndex].tick(gamestate, level);
  47. if(currentIndex == ESCAPE_ID && Keys.ESCAPE_KEY.getTime() == 1)
  48. {
  49. gamestate.setState(Gamestates.GAMEPLAY);
  50. }
  51. else if((currentIndex == OPTION_ID || currentIndex == CHARACTER_ID)
  52. && Keys.ESCAPE_KEY.getTime() == 1)
  53. {
  54. currentIndex = MAIN_ID;
  55. }
  56. }
  57. else if(gamestate.getState() == Gamestates.GAMEPLAY && Keys.ESCAPE_KEY.getTime() == 1)
  58. {
  59. currentIndex = ESCAPE_ID;
  60. gamestate.setState(Gamestates.MENU);
  61. menus[currentIndex].resetIndex();
  62. }
  63. else if(level.isShowingAfterScore())
  64. {
  65. currentIndex = AFTER_SCORE_ID;
  66. gamestate.setState(Gamestates.MENU);
  67. menus[currentIndex].resetIndex();
  68. }
  69. }
  70. /**
  71. * Returns the array of menu buttons of the menu
  72. *
  73. * @return the array of menu buttons of the menu
  74. */
  75. public MenuButton[] getOptions()
  76. {
  77. return menus[currentIndex].getOptions();
  78. }
  79. /**
  80. * Returns the index of the active menu button
  81. *
  82. * @return the index of the active menu button
  83. */
  84. public int getActiveIndex()
  85. {
  86. return menus[currentIndex].getActiveIndex();
  87. }
  88. /**
  89. * Returns if this is the OptionMenu
  90. *
  91. * @return if this is the OptionMenu
  92. */
  93. public boolean isOptionMenu()
  94. {
  95. return menus[currentIndex].isOptionMenu();
  96. }
  97. /**
  98. * Opens the escape menu
  99. *
  100. */
  101. public void showEscapeMenu()
  102. {
  103. currentIndex = ESCAPE_ID;
  104. }
  105. }