GameStateManager.java 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. System.out.println(timeOfNoAction);
  50. if(timeOfNoAction > maxTimeToHaveNoAction){
  51. setState(State.TITLE_STATE);
  52. timeOfNoAction = 0;
  53. }
  54. currentState.update(factor);
  55. fps = (int) (60/(factor/10));
  56. }
  57. public void init()
  58. {
  59. JoystickHandler.REPEAT = false;
  60. for (int i = 1; i < ButtonHandler.getButtons().size(); i++) {
  61. Button b = ButtonHandler.getButton(i);
  62. b.setColor(Color.BLACK);
  63. }
  64. currentState.init();
  65. }
  66. public void buttonPressed(ButtonEvent e) {
  67. timeOfNoAction = 0;
  68. currentState.buttonPressed(e);
  69. }
  70. public void buttonReleased(ButtonEvent e) {
  71. timeOfNoAction = 0;
  72. currentState.buttonReleased(e);
  73. }
  74. public void onJoystickMoved(JoystickEvent e) {
  75. timeOfNoAction = 0;
  76. currentState.onJoystickMoved(e);
  77. }
  78. }