menu.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // MENU SCREEN: main menu of the game where the user can select the level he wants to play
  2. const menuScreen = {
  3. preload: function () {
  4. document.body.style.cursor = "auto";
  5. game.loop.stop();
  6. game.event.clear();
  7. game.animation.clear();
  8. self = this;
  9. // LOADING MEDIA
  10. game.load.image(game.url.menu.image);
  11. },
  12. create: function () {
  13. game.render.clear();
  14. // Background color
  15. game.add.graphic.rect(0, 0, 900, 600, undefined, 0, colors.blueBckg, 1);
  16. // Adds floor
  17. for (let i = 0; i < defaultWidth / 100; i++) {
  18. game.add.image(i * 100, 501, 'floor');
  19. }
  20. // LABELS
  21. // Adds Overtitle: Welcome, <player name>!
  22. game.add.text(defaultWidth / 2, 40, game.lang.welcome + ", " + playerName + "!", textStyles.overtitle);
  23. // Adds Title : Select a game
  24. game.add.text(defaultWidth / 2, 80, game.lang.menu_title, textStyles.title1);
  25. // Adds Subtitle : <game mode>
  26. this.lbl_game = game.add.text(defaultWidth / 2, 110, "", textStyles.subtitle1);
  27. // ICONS
  28. // Calls function that loads navigation icons
  29. navigationIcons.func_addIcons(false, false, false,
  30. true, true,
  31. false, false);
  32. // GAME LEVELS BUTTONS
  33. // Base coordinates for level buttons
  34. let x = -350; // First column
  35. let y = -70; // Top line
  36. menuScreen.menuObjList = [];
  37. for (let i in game.url.menu.image) {
  38. // Adds level buttons
  39. //try {
  40. this.menuObjList[i] = game.add.image(defaultWidth / 2 + x, defaultHeight / 2 + y, 'game' + i);
  41. this.menuObjList[i].anchor(0.5, 0.5);
  42. this.menuObjList[i].levelType = game.url.menu.image[i][3];
  43. this.menuObjList[i].gameShape = game.url.menu.image[i][2];
  44. //}catch (e) { console.log("Erro:",e)}
  45. // Refreshes coordinates for next button
  46. if (i % 2 == 0) y = 90; // The next will be at the bottom line
  47. else {
  48. y = -70; // The next will be at the top line
  49. x += 235; // The next will be at the next column
  50. }
  51. }
  52. game.event.add("click", menuScreen.func_onInputDown);
  53. game.event.add("mousemove", menuScreen.func_onInputOver);
  54. game.render.all();
  55. },
  56. /* EVENT HANDLER*/
  57. func_onInputDown: function (mouseEvent) {
  58. const x = mouseEvent.offsetX;
  59. const y = mouseEvent.offsetY;
  60. for (let i in menuScreen.menuObjList) {
  61. const cur = menuScreen.menuObjList[i];
  62. const valid = y >= cur.yWithAnchor && y <= (cur.yWithAnchor + cur.height * cur.scaleHeight) &&
  63. (x >= cur.xWithAnchor && x <= (cur.xWithAnchor + cur.width * cur.scaleWidth));
  64. if (valid) {
  65. menuScreen.func_loadGame(game.url.menu.image[i][3], game.url.menu.image[i][2]);
  66. break;
  67. }
  68. }
  69. navigationIcons.func_onInputDown(x, y);
  70. },
  71. func_onInputOver: function (mouseEvent) {
  72. const x = mouseEvent.offsetX;
  73. const y = mouseEvent.offsetY;
  74. let flag = false;
  75. menuScreen.menuObjList.forEach(cur => {
  76. const valid = y >= cur.yWithAnchor && y <= (cur.yWithAnchor + cur.height * cur.scaleHeight) &&
  77. (x >= cur.xWithAnchor && x <= (cur.xWithAnchor + cur.width * cur.scaleWidth));
  78. if (valid) {
  79. flag = true;
  80. cur.scaleWidth = cur.scaleHeight = 1.05;
  81. menuScreen.func_showTitle(cur.levelType, cur.gameShape);
  82. } else {
  83. cur.scaleWidth = cur.scaleHeight = 1;
  84. }
  85. });
  86. if (!flag) {
  87. menuScreen.func_clearTitle();
  88. }
  89. navigationIcons.func_onInputOver(x, y);
  90. game.render.all();
  91. },
  92. /* GAME FUNCTIONS */
  93. //calls the selected game menu screen
  94. func_loadGame: function (level, shape) {
  95. if (audioStatus) game.audio.beepSound.play();
  96. gameShape = shape;
  97. levelType = level;
  98. if (levelType == "C") gameTypeString = gameShape.toLowerCase() + "Two";
  99. else gameTypeString = gameShape.toLowerCase() + "One";
  100. switch (gameTypeString) {
  101. case 'squareOne': gameType = squareOne; break;
  102. case 'squareTwo': gameType = squareTwo; break;
  103. case 'circleOne': gameType = circleOne; break;
  104. default: console.error("Game error: the name of the game is not valid");
  105. }
  106. if (debugMode) console.log("Game State: " + gameTypeString + ", " + levelType);
  107. // Calls level difficulty screen
  108. difficultyScreen.preload();
  109. },
  110. func_showTitle: function (levelType, gameShape) {
  111. let title = "", type = "";
  112. if (levelType == 'A') type = "I";
  113. else if (levelType == 'B') type = "II";
  114. else if (levelType == 'C') type = "III";
  115. if (gameShape == "Circle") title += game.lang.circle_name;
  116. else if (gameShape == "Square") title += game.lang.square_name;
  117. if (type != "") title += " " + type;
  118. // Shows level title on the label
  119. menuScreen.lbl_game.name = title;
  120. document.body.style.cursor = "pointer";
  121. },
  122. func_clearTitle: function () {
  123. // Removes text from label
  124. menuScreen.lbl_game.name = "";
  125. document.body.style.cursor = "auto";
  126. }
  127. };