menu.js 6.6 KB

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