globals.js 22 KB

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