GameStateManager.java 2.4 KB

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