menu.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. let menuState = {
  3. create: function(){},
  4. ---------------------------- end of phaser functions
  5. func_loadGame: function(){},
  6. func_showTitle: function(){},
  7. func_clearTitle: function(){},
  8. }
  9. */
  10. // MENU SCREEN: main menu of the game where the user can select the level he wants to play
  11. let menuState = {
  12. create: function() {
  13. // SCREEN SETTINGS
  14. // The menu screen can hold up to 8 levels without needing a side scroller
  15. // If there are more than 8 levels, sets 'extraWidth'
  16. if(loadAssets.levelTypeList.length > 8){
  17. const aux = loadAssets.levelTypeList.length - 8;
  18. // Each extra column holds 2 levels
  19. this.extraWidth = (aux%2==0) ? (aux/2)*235 : ((aux+1)/2)*235;
  20. }else{
  21. this.extraWidth = 0;
  22. }
  23. // Refreshes stage size to have the 'extraWidth' if necessary (visible part of the screen remains the default)
  24. this.game.world.setBounds(0, 0, this.game.world.width + this.extraWidth, this.game.world.height);
  25. // LABELS
  26. // Adds Overtitle: Welcome, <player name>!
  27. this.player_info = game.add.text(this.game.world.centerX - this.extraWidth/2, 40, lang.welcome + ", " + username + "!", textStyles.overtitle);
  28. this.player_info.anchor.setTo(0.5,0.5);
  29. // Adds Title : Select a game
  30. this.title = game.add.text(this.game.world.centerX - this.extraWidth/2, 80, lang.menu_title, textStyles.title);
  31. this.title.anchor.setTo(0.5,0.5);
  32. // Adds Subtitle : <game mode>
  33. this.lbl_game = game.add.text(this.game.world.centerX - this.extraWidth/2, 110, "", textStyles.subtitle);
  34. this.lbl_game.anchor.setTo(0.5,0.5);
  35. // ICONS
  36. // Calls function that loads navigation icons
  37. iconSettings.func_addIcons(false,true,
  38. false,false,false,
  39. true,true,
  40. false,false);
  41. // BACKGROUND
  42. // Adds floor
  43. for(let i=0;i<this.game.world.width/100;i++){
  44. game.add.image(i*100, 501, 'floor');
  45. }
  46. // GAME LEVELS BUTTONS
  47. // Base coordinates for level buttons
  48. let x = -350; // First column
  49. let y = -70; // Top line
  50. const menuObjList = [];
  51. for(let i=0; i<loadAssets.levelTypeList.length; i++){
  52. // Adds level buttons
  53. menuObjList[i] = game.add.sprite(defaultWidth/2 + x, this.game.world.centerY + y, 'game'+i);
  54. menuObjList[i].anchor.setTo(0.5, 0.5);
  55. // Events
  56. menuObjList[i].inputEnabled = true;
  57. menuObjList[i].input.useHandCursor = true;
  58. menuObjList[i].events.onInputDown.add(this.func_loadGame,{levelType: loadAssets.levelTypeList[i], beep: beepSound, shape: loadAssets.levelShapeList[i], game: this.game, extraWidth: this.extraWidth});
  59. menuObjList[i].events.onInputOver.add(this.func_showTitle,{levelType: loadAssets.levelTypeList[i], beep: beepSound, shape : loadAssets.levelShapeList[i], menu: menuObjList[i], lbl_game: this.lbl_game});
  60. menuObjList[i].events.onInputOut.add(this.func_clearTitle, {menu: menuObjList[i], lbl_game: this.lbl_game});
  61. // Refreshes coordinates for next button
  62. if(i%2==0){
  63. y = 90; // The next will be at the bottom line
  64. }else{
  65. y = -70; // The next will be at the top line
  66. x += 235; // The next will be at the next column
  67. }
  68. }
  69. // TURNING 'MOUSE INPUT CAPTURE' ON TO MANAGE PAGE SCROLL
  70. this.input.mouse.capture = true;
  71. },
  72. // MANAGES SIDE SCROLLING
  73. update: function(){
  74. // On mouse release calls function
  75. if(this.input.activePointer.leftButton.isUp){
  76. this.inputUp();
  77. }
  78. // On mouse click calls function
  79. if(this.input.activePointer.leftButton.isDown){
  80. this.inputDown();
  81. }
  82. // If true side scrolls
  83. if(this.isCameraMoving){
  84. // 'this.camera' is a shorthand for 'World.camera' defined by Phaser
  85. this.camera.x += (this.inputStartPosition.x - this.input.activePointer.x)/50;
  86. // Elements that appear fixed in the screen
  87. this.title.x = (this.game.world.centerX - this.extraWidth/2) + this.camera.x;
  88. this.lbl_game.x = (this.game.world.centerX - this.extraWidth/2) + this.camera.x;
  89. this.player_info.x = (this.game.world.centerX - this.extraWidth/2) + this.camera.x;
  90. iconSettings.func_refreshRightIcons_x( (defaultWidth) + this.camera.x );
  91. }
  92. },
  93. // ON RELEASE
  94. inputUp: function(){
  95. this.isCameraMoving = false;
  96. },
  97. // ON CLICK
  98. inputDown: function(){
  99. if(!this.isCameraMoving){
  100. this.inputStartPosition = new Phaser.Point(this.input.activePointer.x, this.input.activePointer.y);
  101. }
  102. // Allows side scrolling to be checked as true
  103. this.isCameraMoving = true;
  104. },
  105. //calls the selected game menu screen
  106. func_loadGame: function(){
  107. // Sets stage width back to default
  108. if(debugMode) console.log("stage width (before): " + this.game.world.width);
  109. this.game.world.setBounds(0, 0, defaultWidth, this.game.world.height);
  110. if(debugMode) console.log("stage width (after): " + this.game.world.width);
  111. if(audioStatus) this.beep.play();
  112. levelShape = this.shape;
  113. levelType = this.levelType;
  114. if(levelType=="C") currentGameState = "game"+levelShape+"Two";
  115. else currentGameState = "game"+levelShape+"One";
  116. if(debugMode) console.log(currentGameState+", "+levelType);
  117. // Calls level difficulty screen
  118. game.state.start('difficulty');
  119. },
  120. func_showTitle: function(){
  121. let title = "", type = "";
  122. if(this.levelType=='A') type = "I";
  123. else if(this.levelType=='B') type = "II";
  124. else if(this.levelType=='C') type = "III";
  125. if(this.shape=="Circle") title += lang.circle_name;
  126. else if(this.shape=="Square") title += lang.square_name;
  127. if(type!="") title += " " + type;
  128. // Shows level title on the label
  129. this.lbl_game.text = title;
  130. // Increases size of button when mouse is on top of it
  131. this.menu.scale.setTo(1.05);
  132. },
  133. func_clearTitle: function(){
  134. // Removes text from label
  135. this.lbl_game.text = "";
  136. // changes button size back to default
  137. this.menu.scale.setTo(1);
  138. }
  139. };