Menu.java 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. public class Menu {
  7. private final static int MAIN_ID;
  8. private final static int ESCAPE_ID;
  9. private final static int OPTION_ID;
  10. private final static int CHARACTER_ID;
  11. private final static int AFTER_SCORE_ID;
  12. static {
  13. int id = 0;
  14. MAIN_ID = id++;
  15. ESCAPE_ID = id++;
  16. OPTION_ID = id++;
  17. CHARACTER_ID = id++;
  18. AFTER_SCORE_ID = id++;
  19. }
  20. private final BaseMenu[] menus;
  21. private int currentIndex = 0;
  22. public Menu(Keys keys) {
  23. menus = new BaseMenu[]{
  24. new MainMenu(MAIN_ID, OPTION_ID, CHARACTER_ID),
  25. new EscMenu(ESCAPE_ID, MAIN_ID),
  26. new OptionMenu(OPTION_ID, MAIN_ID, keys),
  27. new CharacterMenu(CHARACTER_ID, MAIN_ID),
  28. new AfterScoreMenu(AFTER_SCORE_ID, MAIN_ID)
  29. };
  30. }
  31. public void tick(Gamestate gamestate, Level level, Keys keys) {
  32. if(gamestate.is(Gamestates.MENU)) {
  33. currentIndex = menus[currentIndex].tick(gamestate, level, keys);
  34. if(currentIndex == ESCAPE_ID && keys.escape.getTime() == 1) {
  35. gamestate.setState(Gamestates.GAMEPLAY);
  36. } else if((currentIndex == OPTION_ID || currentIndex == CHARACTER_ID)
  37. && keys.escape.getTime() == 1) {
  38. currentIndex = MAIN_ID;
  39. }
  40. } else if(gamestate.is(Gamestates.GAMEPLAY) && keys.escape.getTime() == 1) {
  41. currentIndex = ESCAPE_ID;
  42. gamestate.setState(Gamestates.MENU);
  43. menus[currentIndex].resetIndex();
  44. } else if(level.isShowingAfterScore()) {
  45. currentIndex = AFTER_SCORE_ID;
  46. gamestate.setState(Gamestates.MENU);
  47. menus[currentIndex].resetIndex();
  48. }
  49. }
  50. public MenuButton[] getOptions() {
  51. return menus[currentIndex].getOptions();
  52. }
  53. public int getActiveIndex() {
  54. return menus[currentIndex].getActiveIndex();
  55. }
  56. public boolean isOptionMenu() {
  57. return menus[currentIndex].isOptionMenu();
  58. }
  59. public void showEscapeMenu() {
  60. currentIndex = ESCAPE_ID;
  61. }
  62. }