menu.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. // MENU SCREEN: main menu - player can select the game 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(url.menu.image);
  11. },
  12. create: function () {
  13. game.render.clear();
  14. // BACKGROUND
  15. game.add.graphic.rect(0, 0, 900, 600, undefined, 0, colors.blueBckg, 1);
  16. for (let i = 0; i < defaultWidth / 100; i++) {
  17. game.add.image(i * 100, 501, 'floor');
  18. }
  19. // LABELS
  20. // h4_brown: Welcome, <player name>!
  21. game.add.text(defaultWidth / 2, 40, game.lang.welcome + ", " + playerName + "!", textStyles.h4_brown);
  22. // Title : Select a game
  23. game.add.text(defaultWidth / 2, 80, game.lang.menu_title, textStyles.h1_green);
  24. // Subtitle : <game mode>
  25. this.lbl_game = game.add.text(defaultWidth / 2, 110, "", textStyles.h2_blue_2);
  26. // NAVIGATION ICONS
  27. // Loads navigation icons
  28. navigationIcons.func_addIcons(
  29. false, false, false,
  30. true, true,
  31. false, false);
  32. // -------------------- GAME ICONS
  33. this.menuIcons = [];
  34. const offset = defaultWidth / (info.gameType.length + 1);
  35. for (let i = 0, x = offset; i < info.gameType.length; i++, x += offset) {
  36. const icon = game.add.image(x, defaultHeight / 2 - 70, info.gameTypeUrl[i], 1);
  37. icon.anchor(0.5, 0.5);
  38. icon.gameShape = info.gameShape[i];
  39. icon.gameType = info.gameType[i];
  40. this.menuIcons.push(icon);
  41. }
  42. // EVENTS
  43. game.event.add("click", this.func_onInputDown);
  44. game.event.add("mousemove", this.func_onInputOver);
  45. game.render.all();
  46. },
  47. /* GAME FUNCTIONS */
  48. func_click: function (icon) {
  49. if (audioStatus) game.audio.beepSound.play();
  50. gameShape = icon.gameShape;
  51. gameTypeString = icon.gameType;
  52. switch (gameTypeString) {
  53. case 'squareOne': gameType = squareOne; break;
  54. case 'squareTwo': gameType = squareTwo; break;
  55. case 'circleOne': gameType = circleOne; break;
  56. default: console.error("Game error: the name of the game is not valid");
  57. }
  58. self.menuIcons = self.lbl_game.name;
  59. menuScreenCustom.preload();
  60. },
  61. func_showTitle: function (icon) {
  62. let title;
  63. switch (icon.gameShape){
  64. case 'Circle' : title = game.lang.circle_name; break;
  65. case 'Square' : title = game.lang.square_name; break;
  66. }
  67. const type = icon.gameType.substring(icon.gameType.length - 3);
  68. switch (type){
  69. case 'One' : title += ' I'; break;
  70. case 'Two' : title += ' II'; break;
  71. }
  72. self.lbl_game.name = title;
  73. },
  74. func_clearTitle: function () {
  75. self.lbl_game.name = '';
  76. },
  77. /* EVENTS */
  78. func_onInputDown: function (mouseEvent) {
  79. const x = mouseEvent.offsetX, y = mouseEvent.offsetY;
  80. // check menu icons
  81. for (let i in self.menuIcons) {
  82. // if mouse is within the bounds of an icon
  83. if ( game.math.isOverIcon(x, y, self.menuIcons[i]) ) {
  84. // click first valid icon
  85. self.func_click(self.menuIcons[i]);
  86. break;
  87. }
  88. }
  89. // check navigation icons
  90. navigationIcons.func_onInputDown(x, y);
  91. },
  92. func_onInputOver: function (mouseEvent) {
  93. const x = mouseEvent.offsetX, y = mouseEvent.offsetY;
  94. let flag = false;
  95. // check menu icons
  96. self.menuIcons.forEach( cur => {
  97. if ( game.math.isOverIcon(x, y, cur) ) {
  98. cur.scale = 1.08;
  99. self.func_showTitle(cur);
  100. flag = true;
  101. } else {
  102. cur.scale = 1;
  103. }
  104. });
  105. if (flag) {
  106. document.body.style.cursor = "pointer";
  107. } else {
  108. document.body.style.cursor = "auto";
  109. // menuScreen.func_clearTitle();
  110. }
  111. // check navigation icons
  112. navigationIcons.func_onInputOver(x, y);
  113. game.render.all();
  114. },
  115. };
  116. // IN MENU SCREEN: player can select level, sublevel and difficulty
  117. const menuScreenCustom = {
  118. preload: function () {
  119. document.body.style.cursor = "auto";
  120. game.loop.stop();
  121. game.event.clear();
  122. game.animation.clear();
  123. self = this;
  124. // LOADING MEDIA
  125. game.load.sprite(url[gameTypeString].sprite);
  126. game.load.image(url[gameTypeString].image);
  127. },
  128. create: function () {
  129. game.render.clear();
  130. let x, y, width, height, offsetH, offsetW;
  131. const iconScale = 0.5;
  132. // Background color
  133. game.add.graphic.rect(0, 0, 900, 600, undefined, 0, colors.blueBckg, 1);
  134. // Floor
  135. for (let i = 0; i < defaultWidth / 100; i++) { game.add.image(i * 100, 501, 'floor'); }
  136. // LABELS
  137. // Add Title : Select a game
  138. game.add.text(defaultWidth / 2, 80, game.lang.custom_game, textStyles.h1_green);
  139. // Selected game
  140. game.add.text(defaultWidth / 2, 40, menuScreen.menuIcons, textStyles.h4_brown);
  141. // Loads animation icons
  142. navigationIcons.func_addIcons(
  143. true, false, false,
  144. true, true,
  145. menuScreen, false);
  146. this.menuIcons = [];
  147. offsetW = 600 / 6;
  148. x = 150;
  149. y = 200;
  150. height = 280;
  151. width = 5;
  152. // Label 'Level'
  153. game.add.text(x + offsetW, y, game.lang.level, textStyles.h2_blue_2);
  154. // Label 'Sublevel'
  155. const sublevelLabel = game.add.text(x + 3 * offsetW, y, game.lang.sublevel, textStyles.h2_blue_2);
  156. // Label 'Difficulty'
  157. game.add.text(x + 5 * offsetW, y, game.lang.difficulty, textStyles.h2_blue_2);
  158. // Horizontal line
  159. game.add.graphic.rect(x , y + 10, 600, width, undefined, 0, colors.blueMenuLine).anchor(0,0.5);
  160. // Vertical line
  161. game.add.graphic.rect(x + 2 * offsetW, y - 25, width, height, undefined, 0, colors.blueMenuLine).anchor(0.5,0);
  162. game.add.graphic.rect(x + 4 * offsetW, y - 25, width, height, undefined, 0, colors.blueMenuLine).anchor(0.5,0);
  163. // --------------------------- TURN ON/OFF FRACTION LABELS
  164. if (gameTypeString == 'squareTwo') {
  165. sublevelLabel.alpha = 0.3;
  166. } else {
  167. // Horizontal line
  168. game.add.graphic.rect(x + 4 * offsetW, y + 136, 200, width, undefined, 0, colors.blueMenuLine).anchor(0,0.5);
  169. // Label 'Show Fractions'
  170. game.add.text(x + 5 * offsetW, y + 102, game.lang.show, textStyles.h4_blue_2);
  171. game.add.text(x + 5 * offsetW, y + 102 + 24, game.lang.title, textStyles.h2_blue_2);
  172. const frame = (fractionLabel) ? 1 : 0;
  173. // Selection box
  174. y += 40;
  175. const selectionBox = game.add.sprite(x + 5 * offsetW, y + 102 + 24 - 2, 'select', frame, 0.1);
  176. selectionBox.anchor(0.5, 0.5);
  177. selectionBox.iconType = 'selectionBox';
  178. selectionBox.originalScale = 0.1;
  179. this.menuIcons.push(selectionBox);
  180. }
  181. // ---------------------------- LEVEL ICONS
  182. offsetH = this.func_getOffset(height, info[gameTypeString].levelType.length);
  183. x = 150 + offsetW;
  184. y = 270;
  185. for (let i = 0; i < info[gameTypeString].levelTypeUrl.length; i++, y += offsetH) {
  186. const icon = game.add.image(x, y, info[gameTypeString].levelTypeUrl[i], iconScale, 1);
  187. icon.anchor(0.5, 0.5);
  188. icon.levelType = info[gameTypeString].levelType[i];
  189. icon.iconType = "level";
  190. icon.originalScale = iconScale;
  191. if (i == 0) {
  192. levelType = icon.levelType;
  193. icon.shadow = true;
  194. }
  195. this.menuIcons.push(icon);
  196. }
  197. // ---------------------------- SUBLEVEL ICONS
  198. offsetH = this.func_getOffset(height, info[gameTypeString].sublevelType.length);
  199. if (gameTypeString != 'squareTwo') x += 2 * offsetW;
  200. y = 270;
  201. let icon;
  202. let aux = [];
  203. aux['squareOne'] = [
  204. ['sublevel_right', 'Plus'],
  205. ['sublevel_left', 'Minus']
  206. ];
  207. aux['circleOne'] = [
  208. ['sublevel_right', 'Plus'],
  209. ['sublevel_left', 'Minus'],
  210. ['sublevel_mixed', 'Mixed']
  211. ];
  212. aux['squareTwo'] = [
  213. //['sublevel_top', 'A'],
  214. ['sublevel_bottom', 'B'],
  215. ['sublevel_top', 'C'],
  216. ];
  217. // Placing sublevel icons
  218. for (let i = 0; i < aux[gameTypeString].length; i++, y += offsetH) {
  219. icon = game.add.image(x, y, aux[gameTypeString][i][0], iconScale);
  220. icon.anchor(0.5, 0.5);
  221. icon.alpha = 1;
  222. icon.sublevelType = aux[gameTypeString][i][1];
  223. icon.iconType = "sublevel";
  224. icon.originalScale = iconScale;
  225. if ( i == 0 ) {
  226. sublevelType = icon.sublevelType;
  227. icon.shadow = true;
  228. }
  229. this.menuIcons.push(icon);
  230. }
  231. // --------------------------- DIFFICULTY ICONS
  232. x = (gameTypeString == 'squareOne') ? 600 : 570;
  233. y = 235;
  234. for (let i = 0; i < info[gameTypeString].gameDifficulty; i++) {
  235. // Parameters
  236. const curX = x + (30 + 10) * i;
  237. // Difficulty menuIcons
  238. const icon = game.add.graphic.rect(curX, y, 30, 30, undefined, 0, colors.green, 1);
  239. icon.anchor(0.5,0.5);
  240. icon.difficulty = i + 1;
  241. icon.iconType = 'difficulty';
  242. icon.originalScale = 1;
  243. if (i == 0) {
  244. gameDifficulty = icon.difficulty;
  245. icon.shadow = true;
  246. }
  247. this.menuIcons.push(icon);
  248. // Difficulty numbers
  249. game.add.text(curX, y + 7, i + 1, textStyles.h4_white);
  250. }
  251. // -------------- ENTER ICON
  252. x = defaultWidth - 100;
  253. y = defaultHeight - 110;
  254. const enterIcon = game.add.image(x, y, 'bush');
  255. enterIcon.anchor(0.5,0.5);
  256. enterIcon.iconType = 'enter';
  257. enterIcon.originalScale = 0.9;
  258. this.menuIcons.push(enterIcon);
  259. game.add.text(x, y, game.lang.continue.toUpperCase(), textStyles.p_white);
  260. // EVENTS
  261. game.render.all();
  262. game.event.add("click", this.func_onInputDown);
  263. game.event.add("mousemove", this.func_onInputOver);
  264. },
  265. /* GAME FUNCTIONS */
  266. func_load: function (icon) {
  267. if (audioStatus) game.audio.beepSound.play();
  268. const type = icon.iconType;
  269. switch (type) {
  270. case 'level' : levelType = icon.levelType; break;
  271. case 'sublevel' : sublevelType = icon.sublevelType; break;
  272. case 'difficulty' : gameDifficulty = icon.difficulty; break;
  273. case 'selectionBox' :
  274. if (icon.curFrame == 0) {
  275. icon.curFrame = 1;
  276. fractionLabel = true;
  277. } else {
  278. icon.curFrame = 0;
  279. fractionLabel = false;
  280. }
  281. game.render.all();
  282. break;
  283. case 'enter' :
  284. if (debugMode) console.log("Game State: " + gameTypeString + ", " + levelType);
  285. mapPosition = 0; // Map position
  286. mapMove = true; // Move no next point
  287. completedLevels = 0; // Reset the game progress when entering a new level
  288. mapScreen.preload();
  289. break;
  290. }
  291. },
  292. func_getOffset : function (width, numberOfIcons) {
  293. return width / (numberOfIcons + 1);
  294. },
  295. /* EVENTS */
  296. func_onInputDown: function (mouseEvent) {
  297. const x = mouseEvent.offsetX, y = mouseEvent.offsetY;
  298. let overIcon;
  299. // check if clicked on an icon
  300. for (let i in self.menuIcons) {
  301. if (game.math.isOverIcon(x, y, self.menuIcons[i])) {
  302. overIcon = i;
  303. break;
  304. }
  305. }
  306. // update gui
  307. if (overIcon) {
  308. document.body.style.cursor = "pointer";
  309. self.menuIcons.forEach( cur => {
  310. if ( cur.iconType == self.menuIcons[overIcon].iconType ) {
  311. if (cur == self.menuIcons[overIcon]) {
  312. cur.shadow = true;
  313. } else {
  314. cur.shadow = false;
  315. }
  316. }
  317. });
  318. self.func_load(self.menuIcons[overIcon]);
  319. } else document.body.style.cursor = "auto";
  320. navigationIcons.func_onInputDown(x, y);
  321. game.render.all();
  322. },
  323. func_onInputOver: function (mouseEvent) {
  324. const x = mouseEvent.offsetX, y = mouseEvent.offsetY;
  325. let overIcon;
  326. // check if pointer is over an icon
  327. for (let i in self.menuIcons) {
  328. if (game.math.isOverIcon(x, y, self.menuIcons[i])) {
  329. overIcon = i;
  330. break;
  331. }
  332. }
  333. // update gui
  334. if (overIcon) {
  335. document.body.style.cursor = "pointer";
  336. self.menuIcons.forEach( cur => {
  337. if ( cur.iconType == self.menuIcons[overIcon].iconType ) {
  338. if (cur == self.menuIcons[overIcon] ) {
  339. cur.scale = cur.originalScale * 1.1;
  340. } else {
  341. cur.scale = cur.originalScale;
  342. }
  343. }
  344. });
  345. } else {
  346. self.menuIcons.forEach( cur => {
  347. cur.scale = cur.originalScale;
  348. });
  349. document.body.style.cursor = "auto";
  350. }
  351. // check navigation icons
  352. navigationIcons.func_onInputOver(x, y);
  353. game.render.all();
  354. },
  355. }