globals.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. /**************************************************************
  2. * LInE - Free Education, Private Data - http://www.usp.br/line
  3. *
  4. * This file holds all global elements to the game.
  5. *
  6. * Generating game levels in menu:
  7. * .....................................................
  8. * ...............square....................circle...... } = gameShape
  9. * .........../...........\....................|........ } = gameType (game)
  10. * ........One.............Two................One....... }
  11. * ......./...\.........../...\............./....\......
  12. * ......A.....B.........A.....B...........A......B..... = gameMode (game mode)
  13. * .(floor)..(stack)..(top)..(bottom)..(floor)..(stack).
  14. * .......\./.............\./................\./........
  15. * ........|...............|..................|.........
  16. * ......./.\..............|................/.|.\.......
  17. * ...Plus...Minus.......Equals........Plus.Minus.Mixed. = gameOperation (game math operation)
  18. * .......\./..............|................\.|./.......
  19. * ........|...............|..................|.........
  20. * ......1,2,3.........1,2,3,4,5..........1,2,3,4,5..... = gameDifficulty (difficulty level)
  21. * .....................................................
  22. *
  23. * About levels in map:
  24. *
  25. * ..................(game.levels)......................
  26. * ......................__|__..........................
  27. * .....................|.|.|.|.........................
  28. * ...................0,1,2,3,4,5....................... = mapPosition (map positions)
  29. * ...................|.........|.......................
  30. * ................(start)....(end).....................
  31. **************************************************************/
  32. /**
  33. * Turns console messages ON/OFF (for debug purposes only)
  34. * @type {boolean}
  35. */
  36. const debugMode = false;
  37. /** FOR MOODLE <br>
  38. *
  39. * iFractions can run on a server or inside moodle through iAssign. <br>
  40. * This variable should be set according to where it is suposed to run: <br>
  41. * - if true, on moodle <br>
  42. * - if false, on a server
  43. */
  44. const moodle = false;
  45. /**
  46. * Name of the selected game.<br>
  47. * Can be: 'squareOne', 'squareTwo' or 'circleOne'.
  48. *
  49. * @type {string}
  50. */
  51. let gameType;
  52. /**
  53. * Used for text and game information.<br>
  54. * Shape that makes the name of the game - e.g in 'squareOne' it is 'square'.<br>
  55. * Can be: 'circle' or 'square'.
  56. *
  57. * @type {string}
  58. */
  59. let gameShape;
  60. /**
  61. * Holds selected game mode.<br>
  62. * In squareOne/circleOne can be: 'A' (click on the floor) or 'B' (click on the amount to go/stacked figures).<br>
  63. * In squareTwo can be: 'A' (more subdivisions on top) or 'B' (more subdivisions on bottom).
  64. *
  65. * @type {string}
  66. */
  67. let gameMode;
  68. /**
  69. * Holds game math operation.<br>
  70. * In squareOne can be: 'Plus' (green tractor goes right) or 'Minus' (red tractor goes left).<br>
  71. * In circleOne can be: 'Plus' (green tractor goes right), 'Minus' (red tractor goes left) or 'Mixed' (green tractor goes both sides).<br>
  72. * In squareTwo can be: 'Equals' (compares two rectangle subdivisions).
  73. *
  74. * @type {string}
  75. */
  76. let gameOperation;
  77. /**
  78. * Holds game overall difficulty. 1 (easier) -> n (harder).<br>
  79. * In squareOne can be: 1..3.<br>
  80. * In circleOne/squareTwo can be: 1..5.
  81. *
  82. * @type {number}
  83. */
  84. let gameDifficulty;
  85. /**
  86. * Turns displaying the fraction labels on levels ON/OFF
  87. * @type {boolean}
  88. */
  89. let fractionLabel = true;
  90. /**
  91. * When true, the character can move to next position in the map
  92. * @type {boolean}
  93. */
  94. let mapMove;
  95. /**
  96. * Character position on the map, aka game levels (1..4: valid; 5: end)
  97. * @type {number}
  98. */
  99. let mapPosition;
  100. /**
  101. * Number of finished levels in the map
  102. * @type {number}
  103. */
  104. let completedLevels;
  105. /**
  106. * Turns game audio ON/OFF
  107. * @type {boolean}
  108. */
  109. let audioStatus = false;
  110. /**
  111. * Player's name
  112. * @type {string}
  113. */
  114. let playerName;
  115. /**
  116. * String that contains the selected language.<br>
  117. * It is the name of the language file.
  118. * @type {string}
  119. */
  120. let langString;
  121. /**
  122. * Holds the current state.<br>
  123. * Is used as if it was a 'this' inside state functions.
  124. * @type {object}
  125. */
  126. let self;
  127. const medSrc = 'assets/img/'; // Base directory for media
  128. /**
  129. * Metadata for all games
  130. * @type {object}
  131. */
  132. const info = {
  133. all: {
  134. squareOne: {
  135. gameShape: 'square',
  136. gameType: 'squareOne',
  137. gameTypeUrl: 'game0',
  138. gameMode: ['A', 'B'],
  139. gameModeUrl: ['mode0', 'mode1'],
  140. gameOperation: ['Plus', 'Minus'],
  141. gameOperationUrl: ['operation_plus', 'operation_minus'],
  142. gameDifficulty: 3,
  143. },
  144. circleOne: {
  145. gameShape: 'circle',
  146. gameType: 'circleOne',
  147. gameTypeUrl: 'game1',
  148. gameMode: ['A', 'B'],
  149. gameModeUrl: ['mode2', 'mode3'],
  150. gameOperation: ['Plus', 'Minus', 'Mixed'],
  151. gameOperationUrl: [
  152. 'operation_plus',
  153. 'operation_minus',
  154. 'operation_mixed',
  155. ],
  156. gameDifficulty: 5,
  157. },
  158. squareTwo: {
  159. gameShape: 'square',
  160. gameType: 'squareTwo',
  161. gameTypeUrl: 'game2',
  162. gameMode: ['A', 'B'],
  163. gameModeUrl: ['mode4', 'mode5'],
  164. gameOperation: ['Equals'],
  165. gameOperationUrl: ['operation_equals'],
  166. gameDifficulty: 5,
  167. },
  168. },
  169. gameShape: [],
  170. gameType: [],
  171. gameTypeUrl: [],
  172. gameMode: [],
  173. gameModeUrl: [],
  174. gameOperation: [],
  175. gameOperationUrl: [],
  176. gameDifficulty: [],
  177. };
  178. // Called once when the game starts
  179. (function () {
  180. for (key in info.all) {
  181. info.gameShape.push(info.all[key].gameShape);
  182. info.gameType.push(info.all[key].gameType);
  183. info.gameTypeUrl.push(info.all[key].gameTypeUrl);
  184. info.gameMode.push(info.all[key].gameMode);
  185. info.gameModeUrl.push(info.all[key].gameMode);
  186. info.gameOperation.push(info.all[key].gameOperation);
  187. info.gameOperationUrl.push(info.all[key].gameOperationUrl);
  188. info.gameDifficulty.push(info.all[key].gameDifficulty);
  189. }
  190. })();
  191. /**
  192. * Preset colors for graphic elements.
  193. * @type {object}
  194. */
  195. const colors = {
  196. // Blues
  197. blue: '#003cb3', // Subtitle
  198. blueDark: '#183780', // Line color that indicates right and fraction numbers
  199. blueBg: '#cce5ff', // Background color
  200. blueBgOff: '#adc8e6',
  201. blueBgInsideLevel: '#a8c0e6', // Background color in squareOne (used for floor gap)
  202. blueMenuLine: '#b7cdf4',
  203. // Reds
  204. red: '#b30000', // Linecolor that indicates left
  205. redLight: '#d27979', // squareTwo figures
  206. redDark: '#330000', // squareTwo figures and some titles
  207. // Greens
  208. green: '#00804d', // Title
  209. greenLight: '#83afaf', // squareTwo figures
  210. greenDark: '#1e2f2f', // squareTwo figures
  211. greenNeon: '#00d600',
  212. // Basics
  213. white: '#efeff5',
  214. gray: '#708090',
  215. black: '#000',
  216. yellow: '#ffef1f',
  217. };
  218. /**
  219. * Preset text styles for game text.<br>
  220. * Contains: font, size, text color and text align.
  221. * @type {object}
  222. */
  223. const textStyles = {
  224. h1_green: {
  225. font: '32px Arial,sans-serif',
  226. fill: colors.green,
  227. align: 'center',
  228. }, // Menu title
  229. h2_green: {
  230. font: '26px Arial,sans-serif',
  231. fill: colors.green,
  232. align: 'center',
  233. }, // Flag labels (langState)
  234. h1_white: {
  235. font: '32px Arial,sans-serif',
  236. fill: colors.white,
  237. align: 'center',
  238. }, // Ok button (nameState)
  239. h2_white: {
  240. font: '26px Arial,sans-serif',
  241. fill: colors.white,
  242. align: 'center',
  243. }, // Difficulty buttons (menuState)
  244. h3__white: {
  245. font: '23px Arial,sans-serif',
  246. fill: colors.white,
  247. align: 'center',
  248. }, // Difficulty numbers (menuState)
  249. h4_white: {
  250. font: '20px Arial,sans-serif',
  251. fill: colors.white,
  252. align: 'center',
  253. }, // Difficulty numbers (menuState)
  254. p_white: {
  255. font: '14px Arial,sans-serif',
  256. fill: colors.white,
  257. align: 'center',
  258. }, // Enter button (menuState)
  259. h2_brown: {
  260. font: '26px Arial,sans-serif',
  261. fill: colors.redDark,
  262. align: 'center',
  263. }, // Map difficulty label
  264. h4_brown: {
  265. font: '20px Arial,sans-serif',
  266. fill: colors.redDark,
  267. align: 'center',
  268. }, // Menu overtitle
  269. p_brown: {
  270. font: '14px Arial,sans-serif',
  271. fill: colors.redDark,
  272. align: 'center',
  273. }, // Map difficulty label
  274. h2_blue: {
  275. font: '26px Arial,sans-serif',
  276. fill: colors.blue,
  277. align: 'center',
  278. }, // Menu subtitle
  279. h4_blue: {
  280. font: '20px Arial,sans-serif',
  281. fill: colors.blue,
  282. align: 'center',
  283. }, // Menu subtitle
  284. h2_blueDark: {
  285. font: '26px Arial,sans-serif',
  286. fill: colors.blueDark,
  287. align: 'center',
  288. }, // Fractions
  289. h4_blueDark: {
  290. font: '20px Arial,sans-serif',
  291. fill: colors.blueDark,
  292. align: 'center',
  293. }, // Fractions
  294. p_blueDark: {
  295. font: '14px Arial,sans-serif',
  296. fill: colors.blueDark,
  297. align: 'center',
  298. }, // Fractions
  299. };
  300. /**
  301. * List of URL for all media in the game
  302. * divided 1st by the 'state' that loads the media
  303. * and 2nd by the 'media type' for that state.
  304. *
  305. * @type {object}
  306. */
  307. const url = {
  308. /**
  309. * url.<state>
  310. * where <state> can be: boot, menu, squareOne, squareTwo, circleOne.
  311. */
  312. boot: {
  313. /**
  314. * url.<state>.<media type>
  315. * where <media type> can be: image, sprite, audio <br><br>
  316. *
  317. * image: [ [name, source], ... ] <br>
  318. * sprite: [ [name, source, number of frames], ... ] <br>
  319. * audio: [ [name, [source, alternative source] ], ... ]
  320. */
  321. image: [
  322. // Scene
  323. ['bgimage', medSrc + 'scene/bg.jpg'],
  324. ['bgmap', medSrc + 'scene/bg_map.png'],
  325. ['broken_sign', medSrc + 'scene/broken_sign.png'],
  326. ['bush', medSrc + 'scene/bush.png'],
  327. ['cloud', medSrc + 'scene/cloud.png'],
  328. ['floor', medSrc + 'scene/floor.png'],
  329. ['place_off', medSrc + 'scene/place_off.png'],
  330. ['place_on', medSrc + 'scene/place_on.png'],
  331. ['rock', medSrc + 'scene/rock.png'],
  332. ['road', medSrc + 'scene/road.png'],
  333. ['sign', medSrc + 'scene/sign.png'],
  334. ['tree1', medSrc + 'scene/tree.png'],
  335. ['tree2', medSrc + 'scene/tree2.png'],
  336. ['tree3', medSrc + 'scene/tree3.png'],
  337. ['tree4', medSrc + 'scene/tree4.png'],
  338. // Flags
  339. ['flag_BR', medSrc + 'flag/br.png'],
  340. ['flag_FR', medSrc + 'flag/fr.png'],
  341. ['flag_IT', medSrc + 'flag/it.png'],
  342. ['flag_PE', medSrc + 'flag/pe.png'],
  343. ['flag_US', medSrc + 'flag/us.png'],
  344. // Navigation icons on the top of the page
  345. ['back', medSrc + 'navig_icon/back.png'],
  346. ['help', medSrc + 'navig_icon/help.png'],
  347. ['home', medSrc + 'navig_icon/home.png'],
  348. ['language', medSrc + 'navig_icon/language.png'],
  349. ['menu', medSrc + 'navig_icon/menu.png'],
  350. // Interactive icons
  351. ['arrow_down', medSrc + 'interac_icon/down.png'],
  352. ['close', medSrc + 'interac_icon/close.png'],
  353. ['error', medSrc + 'interac_icon/error.png'],
  354. ['help_pointer', medSrc + 'interac_icon/pointer.png'],
  355. ['info', medSrc + 'interac_icon/info.png'],
  356. ['ok', medSrc + 'interac_icon/ok.png'],
  357. // Menu icons - Games
  358. ['game0', medSrc + 'levels/squareOne.png'], // Square I
  359. ['game1', medSrc + 'levels/circleOne.png'], // Circle I
  360. ['game2', medSrc + 'levels/squareTwo.png'], // Square II
  361. // Menu icons - Info box
  362. ['c1-A', medSrc + 'info_box/c1-A.png'],
  363. ['c1-A-h', medSrc + 'info_box/c1-A-h.png'],
  364. ['c1-B-h', medSrc + 'info_box/c1-B-h.png'],
  365. ['c1-diff-1', medSrc + 'info_box/c1-diff-1.png'],
  366. ['c1-diff-5', medSrc + 'info_box/c1-diff-5.png'],
  367. ['c1-label', medSrc + 'info_box/c1-label.png'],
  368. ['map-c1s2', medSrc + 'info_box/map-c1s2.png'],
  369. ['map-s1', medSrc + 'info_box/map-s1.png'],
  370. ['s1-A', medSrc + 'info_box/s1-A.png'],
  371. ['s1-A-h', medSrc + 'info_box/s1-A-h.png'],
  372. ['s1-B-h', medSrc + 'info_box/s1-B-h.png'],
  373. ['s1-diff-1', medSrc + 'info_box/s1-diff-1.png'],
  374. ['s1-diff-3', medSrc + 'info_box/s1-diff-3.png'],
  375. ['s1-label', medSrc + 'info_box/s1-label.png'],
  376. ['s2', medSrc + 'info_box/s2.png'],
  377. ['s2-A-h', medSrc + 'info_box/s2-A-h.png'],
  378. ['s2-B-h', medSrc + 'info_box/s2-B-h.png'],
  379. ['s2-diff-1', medSrc + 'info_box/s2-diff-1.png'],
  380. ['s2-diff-5', medSrc + 'info_box/s2-diff-5.png'],
  381. ['s2-label', medSrc + 'info_box/s2-label.png'],
  382. ['operation_plus', medSrc + 'info_box/operation_plus.png'],
  383. ['operation_minus', medSrc + 'info_box/operation_minus.png'],
  384. ['operation_mixed', medSrc + 'info_box/operation_mixed.png'],
  385. ['operation_equals', medSrc + 'info_box/operation_equals.png'],
  386. ],
  387. sprite: [
  388. // Game Sprites
  389. ['kid_walk', medSrc + 'character/kid/walk.png', 26],
  390. // Navigation icons on the top of the page
  391. ['audio', medSrc + 'navig_icon/audio.png', 2],
  392. // Interactive icons
  393. ['select', medSrc + 'interac_icon/selectionBox.png', 2],
  394. // Menu icons - Game modes
  395. ['mode0', medSrc + 'levels/squareOne_1.png', 2], // Square I : A
  396. ['mode1', medSrc + 'levels/squareOne_2.png', 2], // Square I : B
  397. ['mode2', medSrc + 'levels/circleOne_1.png', 2], // Circle I : A
  398. ['mode3', medSrc + 'levels/circleOne_2.png', 2], // Circle I : B
  399. ['mode4', medSrc + 'levels/squareTwo_1.png', 2], // Square II : A
  400. ['mode5', medSrc + 'levels/squareTwo_2.png', 2], // Square II : B
  401. // Menu icons - Math operations
  402. ['operation_plus', medSrc + 'levels/operation_plus.png', 2], // Square/circle I : right
  403. ['operation_minus', medSrc + 'levels/operation_minus.png', 2], // Square/circle I : left
  404. ['operation_mixed', medSrc + 'levels/operation_mixed.png', 2], // Circle I : mixed
  405. ['operation_equals', medSrc + 'levels/operation_equals.png', 2], // Square II : equals
  406. ],
  407. audio: [
  408. // Sound effects
  409. ['beepSound', ['assets/audio/beep.ogg', 'assets/audio/beep.mp3']],
  410. ['okSound', ['assets/audio/ok.ogg', 'assets/audio/ok.mp3']],
  411. ['errorSound', ['assets/audio/error.ogg', 'assets/audio/error.mp3']],
  412. ['popSound', ['', 'assets/audio/pop.wav']],
  413. ],
  414. },
  415. squareOne: {
  416. image: [
  417. // Map buildings
  418. ['farm', medSrc + 'scene/farm.png'],
  419. ['garage', medSrc + 'scene/garage.png'],
  420. ],
  421. sprite: [
  422. // Game sprites
  423. ['tractor', medSrc + 'character/tractor/tractor.png', 15],
  424. ],
  425. audio: [],
  426. },
  427. squareTwo: {
  428. image: [
  429. // Map buildings
  430. ['house', medSrc + 'scene/house.png'],
  431. ['school', medSrc + 'scene/school.png'],
  432. ],
  433. sprite: [
  434. // Game sprites
  435. ['kid_standing', medSrc + 'character/kid/lost.png', 6],
  436. ['kid_run', medSrc + 'character/kid/run.png', 12],
  437. ],
  438. audio: [],
  439. },
  440. circleOne: {
  441. image: [
  442. // Map buildings
  443. ['house', medSrc + 'scene/house.png'],
  444. ['school', medSrc + 'scene/school.png'],
  445. // Game images
  446. ['balloon', medSrc + 'character/balloon/airballoon_upper.png'],
  447. ['balloon_basket', medSrc + 'character/balloon/airballoon_base.png'],
  448. ],
  449. sprite: [
  450. // Game sprites
  451. ['kid_run', medSrc + 'character/kid/run.png', 12],
  452. ],
  453. audio: [],
  454. },
  455. };
  456. /**
  457. * Manages navigation icons on the top of the screen
  458. * @namespace
  459. */
  460. const navigationIcons = {
  461. /**
  462. * Add navigation icons.<br>
  463. * * The icons on the left are ordered from left to right. <br>
  464. * * The icons on the right are ordered from right to left.
  465. *
  466. * @param {boolean} leftIcon0 1st left icon (back)
  467. * @param {boolean} leftIcon1 2nd left icon (main menu)
  468. * @param {boolean} leftIcon2 3rd left icon (solve game)
  469. * @param {boolean} rightIcon0 1st right icon (audio)
  470. * @param {boolean} rightIcon1 2nd right icon (lang)
  471. * @param {undefined|string} state state to be called by the 'back' button (must exist if param 'leftIcon0' is true)
  472. * @param {undefined|function} help function in the current game state that display correct answer
  473. */
  474. add: function (
  475. leftIcon0,
  476. leftIcon1,
  477. leftIcon2,
  478. rightIcon0,
  479. rightIcon1,
  480. state,
  481. help
  482. ) {
  483. let left_x = 10;
  484. let right_x = context.canvas.width - 50 - 10;
  485. this.iconsList = [];
  486. // 'Descriptive labels' for the navigation icons
  487. this.left_text = game.add.text(left_x, 73, '', textStyles.h4_brown);
  488. this.left_text.align = 'left';
  489. this.right_text = game.add.text(right_x + 50, 73, '', textStyles.h4_brown);
  490. this.right_text.align = 'right';
  491. // Left icons
  492. if (leftIcon0) {
  493. // Return to previous screen
  494. if (!state) {
  495. console.error(
  496. "Game error: You tried to add a 'back' icon without the 'state' parameter."
  497. );
  498. } else {
  499. this.state = state;
  500. this.iconsList.push(game.add.image(left_x, 10, 'back'));
  501. left_x += 50;
  502. }
  503. }
  504. if (leftIcon1) {
  505. // Return to main menu screen
  506. this.iconsList.push(game.add.image(left_x, 10, 'menu'));
  507. left_x += 50;
  508. }
  509. if (leftIcon2) {
  510. // Shows solution to the game
  511. if (!help) {
  512. console.error(
  513. "Game error: You tried to add a 'game solution' icon without the 'help' parameter."
  514. );
  515. } else {
  516. this.help = help;
  517. this.iconsList.push(game.add.image(left_x, 10, 'help'));
  518. left_x += 50;
  519. }
  520. }
  521. // Right icons
  522. if (rightIcon0) {
  523. // Turns game audio on/off
  524. this.audioIcon = game.add.sprite(right_x, 10, 'audio', 1);
  525. this.audioIcon.curFrame = audioStatus ? 0 : 1;
  526. this.iconsList.push(this.audioIcon);
  527. right_x -= 50;
  528. }
  529. if (rightIcon1) {
  530. // Return to select language screen
  531. this.iconsList.push(game.add.image(right_x, 10, 'language'));
  532. right_x -= 50;
  533. }
  534. },
  535. /**
  536. * When 'back' icon is clicked go to this state
  537. *
  538. * @param {string} state name of the next state
  539. */
  540. callState: function (state) {
  541. if (audioStatus) game.audio.popSound.play();
  542. game.event.clear(self);
  543. game.state.start(state);
  544. },
  545. /**
  546. * Called by mouse click event
  547. *
  548. * @param {number} x contains the mouse x coordinate
  549. * @param {number} y contains the mouse y coordinate
  550. */
  551. onInputDown: function (x, y) {
  552. navigationIcons.iconsList.forEach((cur) => {
  553. if (game.math.isOverIcon(x, y, cur)) {
  554. const name = cur.name;
  555. switch (name) {
  556. case 'back':
  557. navigationIcons.callState(navigationIcons.state);
  558. break;
  559. case 'menu':
  560. navigationIcons.callState('menu');
  561. break;
  562. case 'help':
  563. navigationIcons.help();
  564. break;
  565. case 'language':
  566. navigationIcons.callState('lang');
  567. break;
  568. case 'audio':
  569. if (audioStatus) {
  570. audioStatus = false;
  571. navigationIcons.audioIcon.curFrame = 1;
  572. } else {
  573. audioStatus = true;
  574. if (audioStatus) game.audio.popSound.play();
  575. navigationIcons.audioIcon.curFrame = 0;
  576. }
  577. game.render.all();
  578. break;
  579. default:
  580. console.error('Game error: error in navigation icon');
  581. }
  582. }
  583. });
  584. },
  585. /**
  586. * Called by mouse move event
  587. *
  588. * @param {number} x contains the mouse x coordinate
  589. * @param {number} y contains the mouse y coordinate
  590. */
  591. onInputOver: function (x, y) {
  592. let flag = false;
  593. navigationIcons.iconsList.forEach((cur) => {
  594. if (game.math.isOverIcon(x, y, cur)) {
  595. flag = true;
  596. let name = cur.name;
  597. switch (name) {
  598. case 'back':
  599. navigationIcons.left_text.name = game.lang.nav_back;
  600. break;
  601. case 'menu':
  602. navigationIcons.left_text.name = game.lang.nav_menu;
  603. break;
  604. case 'help':
  605. navigationIcons.left_text.name = game.lang.nav_help;
  606. break;
  607. case 'language':
  608. navigationIcons.right_text.name = game.lang.nav_lang;
  609. break;
  610. case 'audio':
  611. navigationIcons.right_text.name = game.lang.audio;
  612. break;
  613. }
  614. }
  615. });
  616. if (!flag) {
  617. navigationIcons.left_text.name = '';
  618. navigationIcons.right_text.name = '';
  619. } else {
  620. document.body.style.cursor = 'pointer';
  621. }
  622. },
  623. };
  624. /**
  625. * Sends game information to database
  626. *
  627. * @param {string} extraData player information for the current game
  628. */
  629. const sendToDB = function (extraData) {
  630. // FOR MOODLE
  631. if (moodle) {
  632. if (self.result) moodleVar.hits[mapPosition - 1]++;
  633. else moodleVar.errors[mapPosition - 1]++;
  634. moodleVar.time[mapPosition - 1] += game.timer.elapsed;
  635. const url = iLMparameters.iLM_PARAM_ServerToGetAnswerURL;
  636. const grade = '' + getEvaluation();
  637. const report = getAnswer();
  638. const data =
  639. 'return_get_answer=1' +
  640. '&iLM_PARAM_ActivityEvaluation=' +
  641. encodeURIComponent(grade) +
  642. '&iLM_PARAM_ArchiveContent=' +
  643. encodeURIComponent(report);
  644. const init = {
  645. method: 'POST',
  646. body: data,
  647. headers: {
  648. 'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
  649. },
  650. };
  651. fetch(url, init)
  652. .then((response) => {
  653. if (response.ok) {
  654. if (debugMode) console.log('Processing...');
  655. } else {
  656. console.error('Game error: Network response was not ok.');
  657. }
  658. })
  659. .catch((error) => {
  660. console.error(
  661. 'Game error: problem with fetch operation - ' + error.message + '.'
  662. );
  663. });
  664. } else {
  665. // Create some variables we need to send to our PHP file
  666. // Attention: this names must be compactible to data table (MySQL server)
  667. // @see php/save.php
  668. const data =
  669. 'line_ip=143.107.45.11' + // INSERT database server IP
  670. '&line_name=' +
  671. playerName +
  672. '&line_lang=' +
  673. langString +
  674. extraData;
  675. const url = 'php/save.php';
  676. const init = {
  677. method: 'POST',
  678. body: data,
  679. headers: { 'Content-type': 'application/x-www-form-urlencoded' },
  680. };
  681. fetch(url, init)
  682. .then((response) => {
  683. if (response.ok) {
  684. if (debugMode) console.log('Processing...');
  685. response.text().then((text) => {
  686. if (debugMode) {
  687. console.log(text);
  688. }
  689. });
  690. } else {
  691. console.error('Game error: Network response was not ok.');
  692. }
  693. })
  694. .catch((error) => {
  695. console.error(
  696. 'Game error: problem with fetch operation - ' + error.message + '.'
  697. );
  698. });
  699. }
  700. };
  701. let gameFrame = function () {
  702. let x = (y = 200);
  703. let width = context.canvas.width - 2 * x;
  704. let height = context.canvas.height - 2 * y;
  705. let rect = function () {
  706. game.add.geom.rect(x, y, width, height, colors.red, 2);
  707. };
  708. let point = function (offsetW, offsetH) {
  709. for (let i = 0, y1 = y; i < 4; i++) {
  710. x1 = x;
  711. for (let j = 0; j < 7; j++) {
  712. game.add.geom.rect(x1, y1, 20, 20, undefined, 0, colors.red, 1);
  713. x1 += offsetW;
  714. }
  715. y1 += offsetH;
  716. }
  717. };
  718. return { x, y, width, height, rect, point };
  719. };
  720. // For debug
  721. const debugGrid = function () {
  722. const grid = 12;
  723. const h = 1920 / (grid + 0.5);
  724. const v = 1080 / (grid + 0.5);
  725. for (let i = 0; i < grid; i++) {
  726. game.add.geom.rect(h / 2 + i * h, 0, h / 2, 1080, '', 0, colors.blue, 0.3);
  727. game.add.geom.rect(0, v / 2 + i * v, 1920, v / 2, '', 0, colors.blue, 0.3);
  728. }
  729. };