globals.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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 = true;
  45. /**
  46. * HTMLCanvasElement : Canvas where all the game elements are rendered.
  47. * @type {object}
  48. */
  49. let canvas;
  50. /**
  51. * Selected game.<br>
  52. * Can be the objects: squareOne, squareTwo or circleOne.
  53. *
  54. * @type {object}
  55. */
  56. let gameType;
  57. /**
  58. * Name of the selected game.<br>
  59. * Can be: 'squareOne', 'squareTwo' or 'circleOne'.
  60. *
  61. * @type {string}
  62. */
  63. let gameTypeString;
  64. /**
  65. * Used for text and game information.<br>
  66. * Shape that makes the name of the game - e.g in 'squareOne' it is 'square'.<br>
  67. * Can be: 'circle' or 'square'.
  68. *
  69. * @type {string}
  70. */
  71. let gameShape;
  72. /**
  73. * Holds selected game mode.<br>
  74. * In squareOne/circleOne can be: 'A' (click on the floor) or 'B' (click on the amount to go/stacked figures).<br>
  75. * In squareTwo can be: 'A' (more subdivisions on top) or 'B' (more subdivisions on bottom).
  76. *
  77. * @type {string}
  78. */
  79. let gameMode;
  80. /**
  81. * Holds game math operation.<br>
  82. * In squareOne can be: 'Plus' (green tractor goes right) or 'Minus' (red tractor goes left).<br>
  83. * In circleOne can be: 'Plus' (green tractor goes right), 'Minus' (red tractor goes left) or 'Mixed' (green tractor goes both sides).<br>
  84. * In squareTwo can be: 'Equals' (compares two rectangle subdivisions).
  85. *
  86. * @type {string}
  87. */
  88. let gameOperation;
  89. /**
  90. * Holds game overall difficulty. 1 (easier) -> n (harder).<br>
  91. * In squareOne can be: 1..3.<br>
  92. * In circleOne/squareTwo can be: 1..5.
  93. *
  94. * @type {number}
  95. */
  96. let gameDifficulty;
  97. /**
  98. * Turns displaying the fraction labels on levels ON/OFF
  99. * @type {boolean}
  100. */
  101. let fractionLabel = true;
  102. /**
  103. * When true, the character can move to next position in the map
  104. * @type {boolean}
  105. */
  106. let mapMove;
  107. /**
  108. * Character position on the map, aka game levels (1..4: valid; 5: end)
  109. * @type {number}
  110. */
  111. let mapPosition;
  112. /**
  113. * Number of finished levels in the map
  114. * @type {number}
  115. */
  116. let completedLevels;
  117. /**
  118. * Turns game audio ON/OFF
  119. * @type {boolean}
  120. */
  121. let audioStatus = false;
  122. /**
  123. * Player's name
  124. * @type {string}
  125. */
  126. let playerName;
  127. /**
  128. * String that contains the selected language.<br>
  129. * It is the name of the language file.
  130. * @type {string}
  131. */
  132. let langString;
  133. /**
  134. * Holds the current state.<br>
  135. * Is used as if it was a 'this' inside state functions.
  136. * @type {object}
  137. */
  138. let self;
  139. const medSrc = 'assets/img/'; // Base directory for media
  140. /**
  141. * Metadata for all games
  142. * @type {object}
  143. */
  144. const info = {
  145. squareOne: {
  146. gameShape: 'square',
  147. gameType: 'squareOne',
  148. gameTypeUrl: 'game0',
  149. gameMode: ['A', 'B'],
  150. gameModeUrl: ['mode0', 'mode1'],
  151. gameOperation: ['Plus', 'Minus'],
  152. gameDifficulty: 3
  153. },
  154. circleOne: {
  155. gameShape: 'circle',
  156. gameType: 'circleOne',
  157. gameTypeUrl: 'game1',
  158. gameMode: ['A', 'B'],
  159. gameModeUrl: ['mode2', 'mode3'],
  160. gameOperation: ['Plus', 'Minus', 'Mixed'],
  161. gameDifficulty: 5
  162. },
  163. squareTwo: {
  164. gameShape: 'square',
  165. gameType: 'squareTwo',
  166. gameTypeUrl: 'game2',
  167. gameMode: ['A', 'B'],
  168. gameModeUrl: ['mode4', 'mode5'],
  169. gameOperation: ['Equals'],
  170. gameDifficulty: 5
  171. },
  172. gameShape: [],
  173. gameType: [],
  174. gameTypeUrl: [],
  175. gameMode: [],
  176. gameModeUrl: [],
  177. gameOperation: [],
  178. gameDifficulty: [],
  179. // When game starts, update values
  180. start: function () {
  181. info.gameShape = [
  182. info.squareOne.gameShape,
  183. info.circleOne.gameShape,
  184. info.squareTwo.gameShape
  185. ];
  186. info.gameType = [
  187. info.squareOne.gameType,
  188. info.circleOne.gameType,
  189. info.squareTwo.gameType
  190. ];
  191. info.gameTypeUrl = [
  192. info.squareOne.gameTypeUrl,
  193. info.circleOne.gameTypeUrl,
  194. info.squareTwo.gameTypeUrl
  195. ];
  196. info.gameDifficulty = [
  197. info.squareOne.gameDifficulty,
  198. info.circleOne.gameDifficulty,
  199. info.squareTwo.gameDifficulty
  200. ];
  201. info.gameMode = info.squareOne.gameMode.concat(info.circleOne.gameMode, info.squareTwo.gameMode);
  202. info.gameModeUrl = info.squareOne.gameModeUrl.concat(info.circleOne.gameModeUrl, info.squareTwo.gameModeUrl);
  203. info.gameOperation = info.squareOne.gameOperation.concat(info.circleOne.gameOperation, info.squareTwo.gameOperation);
  204. }
  205. };
  206. /**
  207. * Preset colors for graphic elements.
  208. * @type {object}
  209. */
  210. const colors = {
  211. // Blues
  212. blueBckg: '#cce5ff', // Background color
  213. blueBckgOff: '#adc8e6',
  214. blueBckgInsideLevel: '#a8c0e6', // Background color in squareOne (used for floor gap)
  215. blue: '#003cb3', // Subtitle
  216. blueMenuLine: '#b7cdf4',
  217. darkBlue: '#183780', // Line color that indicates right and fraction numbers
  218. // Reds
  219. red: '#b30000', // Linecolor that indicates left
  220. lightRed: '#d27979', // squareTwo figures
  221. darkRed: '#330000', // squareTwo figures and some titles
  222. // Greens
  223. green: '#00804d', // Title
  224. lightGreen: '#83afaf', // squareTwo figures
  225. darkGreen: '#1e2f2f', // squareTwo figures
  226. intenseGreen: '#00d600',
  227. // Basics
  228. white: '#efeff5',
  229. gray: '#708090',
  230. black: '#000',
  231. yellow: '#ffef1f'
  232. };
  233. /**
  234. * Preset text styles for game text.<br>
  235. * Contains: font, size, text color and text align.
  236. * @type {object}
  237. */
  238. const textStyles = {
  239. h1_green: { font: '32px Arial,sans-serif', fill: colors.green, align: 'center' }, // Menu title
  240. h2_green: { font: '26px Arial,sans-serif', fill: colors.green, align: 'center' }, // Flag labels (langState)
  241. h1_white: { font: '32px Arial,sans-serif', fill: colors.white, align: 'center' }, // Ok button (nameState)
  242. h2_white: { font: '26px Arial,sans-serif', fill: colors.white, align: 'center' }, // Difficulty buttons (menuState)
  243. h3__white: { font: '23px Arial,sans-serif', fill: colors.white, align: 'center' }, // Difficulty numbers (menuState)
  244. h4_white: { font: '20px Arial,sans-serif', fill: colors.white, align: 'center' }, // Difficulty numbers (menuState)
  245. p_white: { font: '14px Arial,sans-serif', fill: colors.white, align: 'center' }, // Enter button (menuState)
  246. h2_brown: { font: '26px Arial,sans-serif', fill: colors.darkRed, align: 'center' }, // Map difficulty label
  247. h4_brown: { font: '20px Arial,sans-serif', fill: colors.darkRed, align: 'center' }, // Menu overtitle
  248. p_brown: { font: '14px Arial,sans-serif', fill: colors.darkRed, align: 'center' }, // Map difficulty label
  249. h2_blue_2: { font: '26px Arial,sans-serif', fill: colors.blue, align: 'center' }, // Menu subtitle
  250. h4_blue_2: { font: '20px Arial,sans-serif', fill: colors.blue, align: 'center' }, // Menu subtitle
  251. h2_blue: { font: '26px Arial,sans-serif', fill: colors.darkBlue, align: 'center' }, // Fractions
  252. h4_blue: { font: '20px Arial,sans-serif', fill: colors.darkBlue, align: 'center' }, // Fractions
  253. p_blue: { font: '14px Arial,sans-serif', fill: colors.darkBlue, align: 'center' } // Fractions
  254. };
  255. /**
  256. * List of URL for all media in the game
  257. * divided 1st by the 'state' that loads the media
  258. * and 2nd by the 'media type' for that state.
  259. *
  260. * @type {object}
  261. */
  262. const url = {
  263. /**
  264. * url.<state>
  265. * where <state> can be: boot, menu, squareOne, squareTwo, circleOne.
  266. */
  267. boot: {
  268. /**
  269. * url.<state>.<media type>
  270. * where <media type> can be: image, sprite, audio <br><br>
  271. *
  272. * image: [ [name, source], ... ] <br>
  273. * sprite: [ [name, source, number of frames], ... ] <br>
  274. * audio: [ [name, [source, alternative source] ], ... ]
  275. */
  276. image: [
  277. // Scene
  278. ['bgimage', medSrc + 'scene/bg.jpg'],
  279. ['bgmap', medSrc + 'scene/bg_map.png'],
  280. ['broken_sign', medSrc + 'scene/broken_sign.png'],
  281. ['bush', medSrc + 'scene/bush.png'],
  282. ['cloud', medSrc + 'scene/cloud.png'],
  283. ['floor', medSrc + 'scene/floor.png'],
  284. ['place_off', medSrc + 'scene/place_off.png'],
  285. ['place_on', medSrc + 'scene/place_on.png'],
  286. ['rock', medSrc + 'scene/rock.png'],
  287. ['road', medSrc + 'scene/road.png'],
  288. ['sign', medSrc + 'scene/sign.png'],
  289. ['tree1', medSrc + 'scene/tree.png'],
  290. ['tree2', medSrc + 'scene/tree2.png'],
  291. ['tree3', medSrc + 'scene/tree3.png'],
  292. ['tree4', medSrc + 'scene/tree4.png'],
  293. // Flags
  294. ['flag_BR', medSrc + 'flag/BRAZ.jpg'],
  295. ['flag_FR', medSrc + 'flag/FRAN.jpg'],
  296. ['flag_IT', medSrc + 'flag/ITAL.png'],
  297. ['flag_PE', medSrc + 'flag/PERU.jpg'],
  298. ['flag_US', medSrc + 'flag/UNST.jpg'],
  299. // Navigation icons on the top of the page
  300. ['back', medSrc + 'navig_icon/back.png'],
  301. ['help', medSrc + 'navig_icon/help.png'],
  302. ['home', medSrc + 'navig_icon/home.png'],
  303. ['language', medSrc + 'navig_icon/language.png'],
  304. ['menu', medSrc + 'navig_icon/menu.png'],
  305. // Interactive icons
  306. ['arrow_down', medSrc + 'interac_icon/down.png'],
  307. ['close', medSrc + 'interac_icon/close.png'],
  308. ['error', medSrc + 'interac_icon/error.png'],
  309. ['help_pointer', medSrc + 'interac_icon/pointer.png'],
  310. ['info', medSrc + 'interac_icon/info.png'],
  311. ['ok', medSrc + 'interac_icon/ok.png'],
  312. ],
  313. sprite: [
  314. // Game Sprites
  315. ['kid_walk', medSrc + 'character/kid/walk.png', 26],
  316. // Navigation icons on the top of the page
  317. ['audio', medSrc + 'navig_icon/audio.png', 2],
  318. // Interactive icons
  319. ['select', medSrc + 'interac_icon/selectionBox.png', 2]
  320. ],
  321. audio: [
  322. // Sound effects
  323. ['beepSound', ['assets/audio/beep.ogg', 'assets/audio/beep.mp3']],
  324. ['okSound', ['assets/audio/ok.ogg', 'assets/audio/ok.mp3']],
  325. ['errorSound', ['assets/audio/error.ogg', 'assets/audio/error.mp3']]
  326. ]
  327. },
  328. menu: {
  329. image: [
  330. // Game
  331. ['game0', medSrc + 'levels/squareOne.png'], // Square I
  332. ['game1', medSrc + 'levels/circleOne.png'], // Circle I
  333. ['game2', medSrc + 'levels/squareTwo.png'], // Square II
  334. // Info box icons
  335. ['c1-A', medSrc + 'info_box/c1-A.png'],
  336. ['c1-A-h', medSrc + 'info_box/c1-A-h.png'],
  337. ['c1-B-h', medSrc + 'info_box/c1-B-h.png'],
  338. ['c1-diff-1', medSrc + 'info_box/c1-diff-1.png'],
  339. ['c1-diff-5', medSrc + 'info_box/c1-diff-5.png'],
  340. ['c1-label', medSrc + 'info_box/c1-label.png'],
  341. ['map-c1s2', medSrc + 'info_box/map-c1s2.png'],
  342. ['map-s1', medSrc + 'info_box/map-s1.png'],
  343. ['s1-A', medSrc + 'info_box/s1-A.png'],
  344. ['s1-A-h', medSrc + 'info_box/s1-A-h.png'],
  345. ['s1-B-h', medSrc + 'info_box/s1-B-h.png'],
  346. ['s1-diff-1', medSrc + 'info_box/s1-diff-1.png'],
  347. ['s1-diff-3', medSrc + 'info_box/s1-diff-3.png'],
  348. ['s1-label', medSrc + 'info_box/s1-label.png'],
  349. ['s2', medSrc + 'info_box/s2.png'],
  350. ['s2-A-h', medSrc + 'info_box/s2-A-h.png'],
  351. ['s2-B-h', medSrc + 'info_box/s2-B-h.png'],
  352. ['s2-diff-1', medSrc + 'info_box/s2-diff-1.png'],
  353. ['s2-diff-5', medSrc + 'info_box/s2-diff-5.png'],
  354. ['s2-label', medSrc + 'info_box/s2-label.png'],
  355. ['operation_plus', medSrc + 'info_box/operation_plus.png'],
  356. ['operation_minus', medSrc + 'info_box/operation_minus.png'],
  357. ['operation_mixed', medSrc + 'info_box/operation_mixed.png'],
  358. ['operation_equals', medSrc + 'info_box/operation_equals.png'],
  359. ],
  360. sprite: [
  361. // Game modes
  362. ['mode0', medSrc + 'levels/squareOne_1.png', 2], // Square I : A
  363. ['mode1', medSrc + 'levels/squareOne_2.png', 2], // Square I : B
  364. ['mode2', medSrc + 'levels/circleOne_1.png', 2], // Circle I : A
  365. ['mode3', medSrc + 'levels/circleOne_2.png', 2], // Circle I : B
  366. ['mode4', medSrc + 'levels/squareTwo_1.png', 2], // Square II : A
  367. ['mode5', medSrc + 'levels/squareTwo_2.png', 2], // Square II : B
  368. // Math operations
  369. ['operation_plus', medSrc + 'levels/operation_plus.png', 2], // Square/circle I : right
  370. ['operation_minus', medSrc + 'levels/operation_minus.png', 2], // Square/circle I : left
  371. ['operation_mixed', medSrc + 'levels/operation_mixed.png', 2], // Circle I : mixed
  372. ['operation_equals', medSrc + 'levels/operation_equals.png', 2], // Square II : equals
  373. ],
  374. audio: []
  375. },
  376. squareOne: {
  377. image: [
  378. // Scene
  379. ['farm', medSrc + 'scene/farm.png'],
  380. ['garage', medSrc + 'scene/garage.png']
  381. ],
  382. sprite: [
  383. // Game sprites
  384. ['tractor', medSrc + 'character/tractor/tractor.png', 15]
  385. ],
  386. audio: []
  387. },
  388. squareTwo: {
  389. image: [
  390. // Scene
  391. ['house', medSrc + 'scene/house.png'],
  392. ['school', medSrc + 'scene/school.png']
  393. ],
  394. sprite: [
  395. // Game sprites
  396. ['kid_standing', medSrc + 'character/kid/lost.png', 6],
  397. ['kid_run', medSrc + 'character/kid/run.png', 12]
  398. ],
  399. audio: []
  400. },
  401. circleOne: {
  402. image: [
  403. // Scene
  404. ['house', medSrc + 'scene/house.png'],
  405. ['school', medSrc + 'scene/school.png'],
  406. // Game images
  407. ['balloon', medSrc + 'character/balloon/airballoon_upper.png'],
  408. ['balloon_basket', medSrc + 'character/balloon/airballoon_base.png']
  409. ],
  410. sprite: [
  411. // Game sprites
  412. ['kid_run', medSrc + 'character/kid/run.png', 12]
  413. ],
  414. audio: []
  415. }
  416. };
  417. /**
  418. * Manages navigation icons on the top of the screen
  419. * @namespace
  420. */
  421. const navigationIcons = {
  422. /**
  423. * Add navigation icons.<br>
  424. * * The icons on the left are ordered from left to right. <br>
  425. * * The icons on the right are ordered from right to left.
  426. *
  427. * @param {boolean} leftIcon0 1st left icon (back)
  428. * @param {boolean} leftIcon1 2nd left icon (main menu)
  429. * @param {boolean} leftIcon2 3rd left icon (solve game)
  430. * @param {boolean} rightIcon0 1st right icon (audio)
  431. * @param {boolean} rightIcon1 2nd right icon (lang)
  432. * @param {undefined|string} state state to be called by the 'back' button (must exist if param 'leftIcon0' is true)
  433. * @param {undefined|function} help function in the current game state that display correct answer
  434. */
  435. add: function (leftIcon0, leftIcon1, leftIcon2, rightIcon0, rightIcon1, state, help) {
  436. let left_x = 10;
  437. let right_x = defaultWidth - 50 - 10;
  438. this.iconsList = [];
  439. // 'Descriptive labels' for the navigation icons
  440. this.left_text = game.add.text(left_x, 73, '', textStyles.h4_brown);
  441. this.left_text.align = 'left';
  442. this.right_text = game.add.text(right_x + 50, 73, '', textStyles.h4_brown);
  443. this.right_text.align = 'right';
  444. // Left icons
  445. if (leftIcon0) { // Return to previous screen
  446. if (!state) {
  447. console.error('Game error: You tried to add a \'back\' icon without the \'state\' parameter.');
  448. } else {
  449. this.state = state;
  450. this.iconsList.push(game.add.image(left_x, 10, 'back'));
  451. left_x += 50;
  452. }
  453. }
  454. if (leftIcon1) { // Return to main menu screen
  455. this.iconsList.push(game.add.image(left_x, 10, 'menu'));
  456. left_x += 50;
  457. }
  458. if (leftIcon2) { // Shows solution to the game
  459. if (!help) {
  460. console.error('Game error: You tried to add a \'game solution\' icon without the \'help\' parameter.');
  461. } else {
  462. this.help = help;
  463. this.iconsList.push(game.add.image(left_x, 10, 'help'));
  464. left_x += 50;
  465. }
  466. }
  467. // Right icons
  468. if (rightIcon0) { // Turns game audio on/off
  469. this.audioIcon = game.add.sprite(right_x, 10, 'audio', 1);
  470. this.audioIcon.curFrame = audioStatus ? 0 : 1;
  471. this.iconsList.push(this.audioIcon);
  472. right_x -= 50;
  473. }
  474. if (rightIcon1) { // Return to select language screen
  475. this.iconsList.push(game.add.image(right_x, 10, 'language'));
  476. right_x -= 50;
  477. }
  478. },
  479. /**
  480. * When 'back' icon is clicked go to this state
  481. *
  482. * @param {string} state name of the next state
  483. */
  484. callState: function (state) {
  485. if (audioStatus) game.audio.beepSound.play();
  486. game.event.clear(self);
  487. game.state.start(state);
  488. },
  489. /**
  490. * Called by mouse click event
  491. *
  492. * @param {number} x contains the mouse x coordinate
  493. * @param {number} y contains the mouse y coordinate
  494. */
  495. onInputDown: function (x, y) {
  496. navigationIcons.iconsList.forEach(cur => {
  497. if (game.math.isOverIcon(x, y, cur)) {
  498. const name = cur.name;
  499. switch (name) {
  500. case 'back': navigationIcons.callState(navigationIcons.state); break;
  501. case 'menu': navigationIcons.callState('menu'); break;
  502. case 'help': navigationIcons.help(); break;
  503. case 'language': navigationIcons.callState('lang'); break;
  504. case 'audio':
  505. if (audioStatus) {
  506. audioStatus = false;
  507. navigationIcons.audioIcon.curFrame = 1;
  508. } else {
  509. audioStatus = true;
  510. if (audioStatus) game.audio.beepSound.play();
  511. navigationIcons.audioIcon.curFrame = 0;
  512. }
  513. game.render.all();
  514. break;
  515. default: console.error('Game error: error in navigation icon');
  516. }
  517. }
  518. });
  519. },
  520. /**
  521. * Called by mouse move event
  522. *
  523. * @param {number} x contains the mouse x coordinate
  524. * @param {number} y contains the mouse y coordinate
  525. */
  526. onInputOver: function (x, y) {
  527. let flag = false;
  528. navigationIcons.iconsList.forEach(cur => {
  529. if (game.math.isOverIcon(x, y, cur)) {
  530. flag = true;
  531. let name = cur.name;
  532. switch (name) {
  533. case 'back': navigationIcons.left_text.name = game.lang.nav_back; break;
  534. case 'menu': navigationIcons.left_text.name = game.lang.nav_menu; break;
  535. case 'help': navigationIcons.left_text.name = game.lang.nav_help; break;
  536. case 'language': navigationIcons.right_text.name = game.lang.nav_lang; break;
  537. case 'audio': navigationIcons.right_text.name = game.lang.audio; break;
  538. }
  539. }
  540. });
  541. if (!flag) {
  542. navigationIcons.left_text.name = '';
  543. navigationIcons.right_text.name = '';
  544. } else {
  545. document.body.style.cursor = 'pointer';
  546. }
  547. }
  548. };
  549. /**
  550. * Sends game information to database
  551. *
  552. * @param {string} extraData player information for the current game
  553. */
  554. const sendToDB = function (extraData) {
  555. // FOR MOODLE
  556. if (moodle) {
  557. if (self.result) moodleVar.hits[mapPosition - 1]++;
  558. else moodleVar.errors[mapPosition - 1]++;
  559. moodleVar.time[mapPosition - 1] += game.timer.elapsed;
  560. } else {
  561. // Create some variables we need to send to our PHP file
  562. // Attention: this names must be compactible to data table (MySQL server)
  563. // @see php/save.php
  564. const data = 'line_ip=143.107.45.11' // INSERT database server IP
  565. + '&line_name=' + playerName
  566. + '&line_lang=' + langString
  567. + extraData;
  568. const url = 'php/save.php';
  569. const init = { method: 'POST', body: data, headers: { 'Content-type': 'application/x-www-form-urlencoded' } };
  570. fetch(url, init)
  571. .then(response => {
  572. if (response.ok) {
  573. if (debugMode) console.log("Processing...");
  574. response.text().then(text => { if (debugMode) { console.log(text); } })
  575. } else {
  576. console.error("Game error: Network response was not ok.");
  577. }
  578. })
  579. .catch(error => {
  580. console.error('Game error: problem with fetch operation - ' + error.message + '.');
  581. });
  582. }
  583. };