GameStateManager.java 2.1 KB

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