GameStateManager.java 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.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. PLAY_STATE,
  28. GAMEOVER_STATE,
  29. PRE_GAME_STATE
  30. }
  31. public GameStateManager(SongHandler sh, SQLConnector sql){
  32. gamestates = new ArrayList<GameState>();
  33. gamestates.add(new TitleState(this, sh, sql));
  34. gamestates.add(new MenuState(this, sh, sql));
  35. gamestates.add(new PlayState(this, sh, sql));
  36. gamestates.add(new GameOverState(this, sh, sql));
  37. gamestates.add(new PreGameState(this,sh, sql));
  38. setState(State.TITLE_STATE);
  39. }
  40. public void setState(State st) {
  41. currentState = gamestates.get(st.ordinal());
  42. init();
  43. }
  44. public void next() {
  45. index++;
  46. index %= gamestates.size();
  47. currentState = gamestates.get(index);
  48. init();
  49. }
  50. public void update(float factor){
  51. timeOfNoAction += factor;
  52. if(timeOfNoAction > maxTimeToHaveNoAction){
  53. setState(State.TITLE_STATE);
  54. timeOfNoAction = 0;
  55. }
  56. currentState.update(factor);
  57. fps = (int) (60/(factor/10));
  58. }
  59. public void init()
  60. {
  61. JoystickHandler.REPEAT = false;
  62. for (int i = 1; i < ButtonHandler.getButtons().size(); i++) {
  63. Button b = ButtonHandler.getButton(i);
  64. b.setColor(Color.BLACK);
  65. }
  66. currentState.init();
  67. }
  68. public void buttonPressed(ButtonEvent e) {
  69. timeOfNoAction = 0;
  70. currentState.buttonPressed(e);
  71. }
  72. public void buttonReleased(ButtonEvent e) {
  73. timeOfNoAction = 0;
  74. currentState.buttonReleased(e);
  75. }
  76. public void onJoystickMoved(JoystickEvent e) {
  77. timeOfNoAction = 0;
  78. currentState.onJoystickMoved(e);
  79. }
  80. }