GameStateManager.java 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package control;
  2. import java.awt.Color;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import model.SongHandler;
  6. import model.gameState.GameOverState;
  7. import model.gameState.GameState;
  8. import model.gameState.HelpState;
  9. import model.gameState.MenuState;
  10. import model.gameState.PlayState;
  11. import model.gameState.PreGameState;
  12. import model.gameState.TitleState;
  13. import control.button.Button;
  14. import control.button.ButtonEvent;
  15. import control.button.ButtonHandler;
  16. import control.joystick.JoystickEvent;
  17. import control.joystick.JoystickHandler;
  18. import data.io.SQLConnector;
  19. public class GameStateManager {
  20. private List<GameState> gamestates;
  21. public GameState currentState;
  22. private int index = 0;
  23. public int fps;
  24. private float timeOfNoAction = 0,maxTimeToHaveNoAction = 45000;
  25. public enum State {
  26. TITLE_STATE,
  27. MENU_STATE,
  28. HELP_STATE,
  29. PLAY_STATE,
  30. GAMEOVER_STATE,
  31. PRE_GAME_STATE
  32. }
  33. public GameStateManager(SongHandler sh, SQLConnector sql){
  34. gamestates = new ArrayList<GameState>();
  35. gamestates.add(new TitleState(this, sh, sql));
  36. gamestates.add(new MenuState(this, sh, sql));
  37. gamestates.add(new HelpState(this, sh, sql));
  38. gamestates.add(new PlayState(this, sh, sql));
  39. gamestates.add(new GameOverState(this, sh, sql));
  40. gamestates.add(new PreGameState(this,sh, sql));
  41. setState(State.TITLE_STATE);
  42. }
  43. public void setState(State st) {
  44. if (st.ordinal() > 0 && !(currentState instanceof TitleState))
  45. currentState.stopAudio();
  46. currentState = gamestates.get(st.ordinal());
  47. init();
  48. }
  49. public void next() {
  50. index++;
  51. index %= gamestates.size();
  52. currentState = gamestates.get(index);
  53. init();
  54. }
  55. public void update(float factor){
  56. timeOfNoAction += factor;
  57. if(timeOfNoAction > maxTimeToHaveNoAction){
  58. setState(State.TITLE_STATE);
  59. timeOfNoAction = 0;
  60. }
  61. currentState.update(factor);
  62. fps = (int) (60/(factor/10));
  63. }
  64. public void init()
  65. {
  66. JoystickHandler.REPEAT = false;
  67. for (int i = 1; i < ButtonHandler.getButtons().size(); i++) {
  68. Button b = ButtonHandler.getButton(i);
  69. b.setColor(Color.BLACK);
  70. }
  71. currentState.init();
  72. }
  73. public void buttonPressed(ButtonEvent e) {
  74. timeOfNoAction = 0;
  75. currentState.buttonPressed(e);
  76. }
  77. public void buttonReleased(ButtonEvent e) {
  78. timeOfNoAction = 0;
  79. currentState.buttonReleased(e);
  80. }
  81. public void onJoystickMoved(JoystickEvent e) {
  82. timeOfNoAction = 0;
  83. currentState.onJoystickMoved(e);
  84. }
  85. }