MenuState.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. package model.gameState;
  2. import java.awt.Color;
  3. import java.awt.Font;
  4. import java.awt.GradientPaint;
  5. import java.awt.Graphics2D;
  6. import java.awt.Polygon;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import model.GameModel;
  10. import model.SongHandler;
  11. import model.objects.DifficultyButton;
  12. import model.objects.MenuButton;
  13. import audio.Song;
  14. import audio.SongInstance;
  15. import control.GameStateManager;
  16. import control.button.ButtonEvent;
  17. import control.joystick.Joystick;
  18. import control.joystick.JoystickEvent;
  19. public class MenuState extends GameState {
  20. private ArrayList<MenuButton> buttons;
  21. private ArrayList<DifficultyButton> buttons2;
  22. private int selected, oldselected;
  23. private List<Song> songs;
  24. private Polygon triangle,triangle2;
  25. private int animationcounter;
  26. private boolean subscreen, startanimation;
  27. int yPosDiffButton = 900;
  28. private int difSelect=0;
  29. public MenuState(GameStateManager gsm, SongHandler sh) {
  30. super(gsm, sh);
  31. buttons = new ArrayList<MenuButton>();
  32. buttons2 = new ArrayList<DifficultyButton>();
  33. this.songs = sh.getSongs();
  34. startanimation = true;
  35. subscreen = false;
  36. buttons.add(new MenuButton(-600, 50,1.7, 0, Color.green, selectedToSong(selected-2) ));
  37. buttons.add(new MenuButton(-600, 150, 1.7, 10, Color.BLUE, selectedToSong(selected-1)));
  38. buttons.add(new MenuButton(-600, 250, 1.7, 20, Color.red, selectedToSong(selected)));
  39. buttons.add(new MenuButton(-600, 350, 1.7, 30, Color.yellow,selectedToSong(selected+1)));
  40. buttons.add(new MenuButton(-600, 450, 1.7, 30, Color.WHITE,selectedToSong(selected+2)));
  41. buttons.get(2).setSelected(true);
  42. }
  43. @Override
  44. public void init() {
  45. }
  46. @Override
  47. public void update() {
  48. if(startanimation){
  49. buttonInAnimation(animationcounter);
  50. if(animationcounter == 5){
  51. startanimation = false;
  52. }
  53. }else if(subscreen){
  54. nextScreen();
  55. }else{
  56. previousScreen();
  57. }
  58. for(MenuButton b:buttons){
  59. b.update();
  60. }
  61. if(selected != oldselected){
  62. for(int i = 0; i < buttons.size(); i++){
  63. buttons.get(i).setSong(selectedToSong(selected+(i-2)));
  64. }
  65. oldselected = selected;
  66. buttons2.clear();
  67. int instanceNr = 0;
  68. for(int i = sh.getCurrentSong().getSongs().size(); i>0; i--){
  69. if(sh.getCurrentSong().getSongs().size() == 0)
  70. continue;
  71. SongInstance si = sh.getCurrentSong().getSongs().get(i-1);
  72. buttons2.add(new DifficultyButton(yPosDiffButton-instanceNr,si.getDifficulty(), GameModel.colors[i-1]));
  73. instanceNr += 100;
  74. }
  75. }
  76. }
  77. @Override
  78. public void draw(Graphics2D g2) {
  79. g2.setColor(Color.BLACK);
  80. Font textFont2 = new Font("OCR A Extended", Font.BOLD, 50);
  81. g2.setFont(textFont2);
  82. if(!subscreen) {
  83. GradientPaint gp = new GradientPaint(0, 0, new Color(0,0,1, 0.6f),1280,1024 ,new Color(0,0,1, 0.2f));
  84. g2.setPaint(gp);
  85. g2.fillRect(0, 0, 1280, 1024);
  86. triangle2 = new Polygon();
  87. triangle2.addPoint(0, 0);
  88. triangle2.addPoint(0, 1024/4+50);
  89. triangle2.addPoint(1280/2+50, 0);
  90. triangle = new Polygon();
  91. triangle.addPoint(0, 0);
  92. triangle.addPoint(0, 1024/4);
  93. triangle.addPoint(1280/2, 0);
  94. for(MenuButton b:buttons){
  95. b.draw(g2);
  96. }
  97. GradientPaint gp2 = new GradientPaint(0, 0, new Color(1,1,0, 0.6f),1280,1024 ,new Color(0,0,1, 0.2f));
  98. g2.setPaint(gp2);
  99. g2.fillPolygon(triangle2);
  100. g2.setColor(Color.ORANGE);
  101. g2.fillPolygon(triangle);
  102. g2.setColor(Color.BLACK);
  103. g2.drawString("Main Menu", 30, 60);
  104. }
  105. if(subscreen) {
  106. //g2.setColor(Color.BLACK);
  107. GradientPaint gp3 = new GradientPaint(640, 1024/8,Color.BLUE,640,1024 ,Color.WHITE);
  108. g2.setPaint(gp3);
  109. g2.fillRect(0, 0, 1280, 1024);
  110. GradientPaint gp4 = new GradientPaint(0, 0,new Color(1,1,0,0.6f),1280,1024,new Color(1,1,1,0.2f));
  111. g2.setPaint(gp4);
  112. g2.fillRect(0,128,1280,25);
  113. g2.setColor(Color.ORANGE);
  114. g2.fillRect(0, 0, 1280, 1024/8);
  115. g2.setColor(Color.BLACK);
  116. g2.drawString(selectedToSong(selected).getTitle(), 30, 60);
  117. g2.setColor(Color.WHITE);
  118. g2.drawString("Overall Highscore: " + "", 30, 200);
  119. g2.drawString("Daily Highscore: " + "", 30, 300);
  120. g2.drawString("Beats per Minute: " + selectedToSong(selected).getBPM(), 30, 400);
  121. for(DifficultyButton b : buttons2){
  122. b.draw(g2);
  123. }
  124. g2.setColor(Color.BLACK);
  125. g2.fillRect(865, 425 - difSelect*100, 25, 25);
  126. }
  127. }
  128. @Override
  129. public void buttonPressed(ButtonEvent e) {
  130. if(subscreen){ //Screen for Song details
  131. if(e.getButton().getButtonID() == 2){
  132. subscreen = false;
  133. }
  134. if(e.getButton().getButtonID() == 1){
  135. gsm.next();
  136. }
  137. }else{ //Screen for selecting song
  138. if(e.getButton().getButtonID() == 1){
  139. subscreen = true;
  140. }else if(e.getButton().getButtonID() == 2){
  141. subscreen = false;
  142. }
  143. }
  144. }
  145. @Override
  146. public void buttonReleased(ButtonEvent e) {
  147. }
  148. @Override
  149. public void onJoystickMoved(JoystickEvent e) {
  150. if(subscreen){
  151. if(e.getJoystick().getPos() == Joystick.Position.DOWN){
  152. difSelect--;
  153. if(difSelect < 0){
  154. difSelect += buttons2.size();
  155. }
  156. // System.out.println(difSelect);
  157. }else if(e.getJoystick().getPos() == Joystick.Position.UP){
  158. difSelect++;
  159. if(difSelect > buttons2.size()-1){
  160. difSelect = 0;
  161. }
  162. // System.out.println(difSelect);
  163. }
  164. sh.set(sh.getCurrentSong().getSongs().get(difSelect));
  165. }else{ //Screen for selecting song
  166. if(e.getJoystick().getPos() == Joystick.Position.DOWN){
  167. selected++;
  168. }else if(e.getJoystick().getPos() == Joystick.Position.UP){
  169. selected--;
  170. }
  171. sh.set(songs.indexOf(selectedToSong(selected)));
  172. sh.play(true);
  173. }
  174. }
  175. public void buttonInAnimation(int button){
  176. if(button >= 0 && button < buttons.size() ){
  177. buttons.get(button).setX(buttons.get(button).getX()+40);
  178. if(buttons.get(button).getX() >= 320){
  179. animationcounter++;
  180. }
  181. if(buttons.get(button).getX() >= -200){
  182. buttonInAnimation(button+1);
  183. }
  184. }
  185. }
  186. public void nextScreen(){
  187. if(buttons.get(0).getX() > -700){
  188. for(MenuButton b:buttons){
  189. b.setX(b.getX()-100);
  190. }
  191. }
  192. }
  193. public void previousScreen(){
  194. if(buttons.get(0).getX() < 300){
  195. for(MenuButton b:buttons){
  196. b.setX(b.getX()+100);
  197. }
  198. }
  199. }
  200. public Song selectedToSong(int s){
  201. s %= songs.size();
  202. if(s < 0){
  203. return songs.get(songs.size()+s);
  204. }else{
  205. return songs.get(s);
  206. }
  207. }
  208. }