globals.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  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. const fontSizes = {
  219. h0: '56px',
  220. h1: '40px',
  221. h2: '32px',
  222. h3: '30px',
  223. h4: '28px',
  224. p: '22px',
  225. h1_old: '32px',
  226. h2_old: '26px',
  227. h3_old: '22px',
  228. h4_old: '20px',
  229. p_old: '14px',
  230. };
  231. /**
  232. * Preset text styles for game text.<br>
  233. * Contains: font, size, text color and text align.
  234. * @type {object}
  235. */
  236. const textStyles = {
  237. h0_green: {
  238. font: fontSizes.h0 + ' Arial,sans-serif',
  239. fill: colors.green,
  240. align: 'center',
  241. }, // menu title
  242. h1_green: {
  243. font: fontSizes.h1 + ' Arial,sans-serif',
  244. fill: colors.green,
  245. align: 'center',
  246. }, // Menu title
  247. h2_green: {
  248. font: fontSizes.h2 + ' Arial,sans-serif',
  249. fill: colors.green,
  250. align: 'center',
  251. }, // Flag labels (langState)
  252. h1_white: {
  253. font: fontSizes.h1 + ' Arial,sans-serif',
  254. fill: colors.white,
  255. align: 'center',
  256. }, // Ok button (nameState)
  257. h2_white: {
  258. font: fontSizes.h2 + ' Arial,sans-serif',
  259. fill: colors.white,
  260. align: 'center',
  261. }, // Difficulty buttons (menuState)
  262. h3__white: {
  263. font: fontSizes.h3 + ' Arial,sans-serif',
  264. fill: colors.white,
  265. align: 'center',
  266. }, // Difficulty numbers (menuState)
  267. h4_white: {
  268. font: fontSizes.h4 + ' Arial,sans-serif',
  269. fill: colors.white,
  270. align: 'center',
  271. }, // Difficulty numbers (menuState)
  272. p_white: {
  273. font: fontSizes.p + ' Arial,sans-serif',
  274. fill: colors.white,
  275. align: 'center',
  276. }, // Enter button (menuState)
  277. h2_brown: {
  278. font: fontSizes.h2 + ' Arial,sans-serif',
  279. fill: colors.redDark,
  280. align: 'center',
  281. }, // Map difficulty label
  282. h4_brown: {
  283. font: fontSizes.h4 + ' Arial,sans-serif',
  284. fill: colors.redDark,
  285. align: 'center',
  286. }, // Menu overtitle
  287. p_brown: {
  288. font: fontSizes.p + ' Arial,sans-serif',
  289. fill: colors.redDark,
  290. align: 'center',
  291. }, // Map difficulty label
  292. h2_blue: {
  293. font: fontSizes.h2 + ' Arial,sans-serif',
  294. fill: colors.blue,
  295. align: 'center',
  296. }, // Menu subtitle
  297. h4_blue: {
  298. font: fontSizes.h4 + ' Arial,sans-serif',
  299. fill: colors.blue,
  300. align: 'center',
  301. }, // Menu subtitle
  302. h2_blueDark: {
  303. font: fontSizes.h2 + ' Arial,sans-serif',
  304. fill: colors.blueDark,
  305. align: 'center',
  306. }, // Fractions
  307. h4_blueDark: {
  308. font: fontSizes.h4 + ' Arial,sans-serif',
  309. fill: colors.blueDark,
  310. align: 'center',
  311. }, // Fractions
  312. p_blueDark: {
  313. font: fontSizes.p + ' Arial,sans-serif',
  314. fill: colors.blueDark,
  315. align: 'center',
  316. }, // Fractions
  317. };
  318. /**
  319. * List of URL for all media in the game
  320. * divided 1st by the 'state' that loads the media
  321. * and 2nd by the 'media type' for that state.
  322. *
  323. * @type {object}
  324. */
  325. const url = {
  326. /**
  327. * url.<state>
  328. * where <state> can be: boot, menu, squareOne, squareTwo, circleOne.
  329. */
  330. boot: {
  331. /**
  332. * url.<state>.<media type>
  333. * where <media type> can be: image, sprite, audio <br><br>
  334. *
  335. * image: [ [name, source], ... ] <br>
  336. * sprite: [ [name, source, number of frames], ... ] <br>
  337. * audio: [ [name, [source, alternative source] ], ... ]
  338. */
  339. image: [
  340. // Scene
  341. ['bgimage', medSrc + 'scene/bg.jpg'],
  342. ['bgmap', medSrc + 'scene/bg_map.png'],
  343. ['broken_sign', medSrc + 'scene/broken_sign.png'],
  344. ['bush', medSrc + 'scene/bush.png'],
  345. ['cloud', medSrc + 'scene/cloud.png'],
  346. ['floor', medSrc + 'scene/floor.png'],
  347. ['place_off', medSrc + 'scene/place_off.png'],
  348. ['place_on', medSrc + 'scene/place_on.png'],
  349. ['rock', medSrc + 'scene/rock.png'],
  350. ['road', medSrc + 'scene/road.png'],
  351. ['sign', medSrc + 'scene/sign.png'],
  352. ['tree1', medSrc + 'scene/tree.png'],
  353. ['tree2', medSrc + 'scene/tree2.png'],
  354. ['tree3', medSrc + 'scene/tree3.png'],
  355. ['tree4', medSrc + 'scene/tree4.png'],
  356. // Flags
  357. ['flag_BR', medSrc + 'flag/br.png'],
  358. ['flag_FR', medSrc + 'flag/fr.png'],
  359. ['flag_IT', medSrc + 'flag/it.png'],
  360. ['flag_PE', medSrc + 'flag/pe.png'],
  361. ['flag_US', medSrc + 'flag/us.png'],
  362. // Navigation icons on the top of the page
  363. ['back', medSrc + 'navig_icon/back.png'],
  364. ['help', medSrc + 'navig_icon/help.png'],
  365. ['home', medSrc + 'navig_icon/home.png'],
  366. ['language', medSrc + 'navig_icon/language.png'],
  367. ['menu', medSrc + 'navig_icon/menu.png'],
  368. // Interactive icons
  369. ['arrow_down', medSrc + 'interac_icon/down.png'],
  370. ['close', medSrc + 'interac_icon/close.png'],
  371. ['error', medSrc + 'interac_icon/error.png'],
  372. ['help_pointer', medSrc + 'interac_icon/pointer.png'],
  373. ['info', medSrc + 'interac_icon/info.png'],
  374. ['ok', medSrc + 'interac_icon/ok.png'],
  375. // Menu icons - Games
  376. ['game0', medSrc + 'levels/squareOne.png'], // Square I
  377. ['game1', medSrc + 'levels/circleOne.png'], // Circle I
  378. ['game2', medSrc + 'levels/squareTwo.png'], // Square II
  379. // Menu icons - Info box
  380. ['c1-A', medSrc + 'info_box/c1-A.png'],
  381. ['c1-A-h', medSrc + 'info_box/c1-A-h.png'],
  382. ['c1-B-h', medSrc + 'info_box/c1-B-h.png'],
  383. ['c1-diff-1', medSrc + 'info_box/c1-diff-1.png'],
  384. ['c1-diff-5', medSrc + 'info_box/c1-diff-5.png'],
  385. ['c1-label', medSrc + 'info_box/c1-label.png'],
  386. ['map-c1s2', medSrc + 'info_box/map-c1s2.png'],
  387. ['map-s1', medSrc + 'info_box/map-s1.png'],
  388. ['s1-A', medSrc + 'info_box/s1-A.png'],
  389. ['s1-A-h', medSrc + 'info_box/s1-A-h.png'],
  390. ['s1-B-h', medSrc + 'info_box/s1-B-h.png'],
  391. ['s1-diff-1', medSrc + 'info_box/s1-diff-1.png'],
  392. ['s1-diff-3', medSrc + 'info_box/s1-diff-3.png'],
  393. ['s1-label', medSrc + 'info_box/s1-label.png'],
  394. ['s2', medSrc + 'info_box/s2.png'],
  395. ['s2-A-h', medSrc + 'info_box/s2-A-h.png'],
  396. ['s2-B-h', medSrc + 'info_box/s2-B-h.png'],
  397. ['s2-diff-1', medSrc + 'info_box/s2-diff-1.png'],
  398. ['s2-diff-5', medSrc + 'info_box/s2-diff-5.png'],
  399. ['s2-label', medSrc + 'info_box/s2-label.png'],
  400. ['operation_plus', medSrc + 'info_box/operation_plus.png'],
  401. ['operation_minus', medSrc + 'info_box/operation_minus.png'],
  402. ['operation_mixed', medSrc + 'info_box/operation_mixed.png'],
  403. ['operation_equals', medSrc + 'info_box/operation_equals.png'],
  404. ],
  405. sprite: [
  406. // Game Sprites
  407. ['kid_walk', medSrc + 'character/kid/walk.png', 26],
  408. // Navigation icons on the top of the page
  409. ['audio', medSrc + 'navig_icon/audio.png', 2],
  410. // Interactive icons
  411. ['select', medSrc + 'interac_icon/selectionBox.png', 2],
  412. // Menu icons - Game modes
  413. ['mode0', medSrc + 'levels/squareOne_1.png', 2], // Square I : A
  414. ['mode1', medSrc + 'levels/squareOne_2.png', 2], // Square I : B
  415. ['mode2', medSrc + 'levels/circleOne_1.png', 2], // Circle I : A
  416. ['mode3', medSrc + 'levels/circleOne_2.png', 2], // Circle I : B
  417. ['mode4', medSrc + 'levels/squareTwo_1.png', 2], // Square II : A
  418. ['mode5', medSrc + 'levels/squareTwo_2.png', 2], // Square II : B
  419. // Menu icons - Math operations
  420. ['operation_plus', medSrc + 'levels/operation_plus.png', 2], // Square/circle I : right
  421. ['operation_minus', medSrc + 'levels/operation_minus.png', 2], // Square/circle I : left
  422. ['operation_mixed', medSrc + 'levels/operation_mixed.png', 2], // Circle I : mixed
  423. ['operation_equals', medSrc + 'levels/operation_equals.png', 2], // Square II : equals
  424. ],
  425. audio: [
  426. // Sound effects
  427. ['beepSound', ['assets/audio/beep.ogg', 'assets/audio/beep.mp3']],
  428. ['okSound', ['assets/audio/ok.ogg', 'assets/audio/ok.mp3']],
  429. ['errorSound', ['assets/audio/error.ogg', 'assets/audio/error.mp3']],
  430. ['popSound', ['', 'assets/audio/pop.wav']],
  431. ],
  432. },
  433. squareOne: {
  434. image: [
  435. // Map buildings
  436. ['farm', medSrc + 'scene/farm.png'],
  437. ['garage', medSrc + 'scene/garage.png'],
  438. ],
  439. sprite: [
  440. // Game sprites
  441. ['tractor', medSrc + 'character/tractor/tractor.png', 15],
  442. ],
  443. audio: [],
  444. },
  445. squareTwo: {
  446. image: [
  447. // Map buildings
  448. ['house', medSrc + 'scene/house.png'],
  449. ['school', medSrc + 'scene/school.png'],
  450. ],
  451. sprite: [
  452. // Game sprites
  453. ['kid_standing', medSrc + 'character/kid/lost.png', 6],
  454. ['kid_run', medSrc + 'character/kid/run.png', 12],
  455. ],
  456. audio: [],
  457. },
  458. circleOne: {
  459. image: [
  460. // Map buildings
  461. ['house', medSrc + 'scene/house.png'],
  462. ['school', medSrc + 'scene/school.png'],
  463. // Game images
  464. ['balloon', medSrc + 'character/balloon/airballoon_upper.png'],
  465. ['balloon_basket', medSrc + 'character/balloon/airballoon_base.png'],
  466. ],
  467. sprite: [
  468. // Game sprites
  469. ['kid_run', medSrc + 'character/kid/run.png', 12],
  470. ],
  471. audio: [],
  472. },
  473. };
  474. /**
  475. * Manages navigation icons on the top of the screen
  476. * @namespace
  477. */
  478. const navigationIcons = {
  479. /**
  480. * Add navigation icons.<br>
  481. * * The icons on the left are ordered from left to right. <br>
  482. * * The icons on the right are ordered from right to left.
  483. *
  484. * @param {boolean} leftIcon0 1st left icon (back)
  485. * @param {boolean} leftIcon1 2nd left icon (main menu)
  486. * @param {boolean} leftIcon2 3rd left icon (solve game)
  487. * @param {boolean} rightIcon0 1st right icon (audio)
  488. * @param {boolean} rightIcon1 2nd right icon (lang)
  489. * @param {undefined|string} state state to be called by the 'back' button (must exist if param 'leftIcon0' is true)
  490. * @param {undefined|function} help function in the current game state that display correct answer
  491. */
  492. add: function (
  493. leftIcon0,
  494. leftIcon1,
  495. leftIcon2,
  496. rightIcon0,
  497. rightIcon1,
  498. state,
  499. help
  500. ) {
  501. let left_x = 10;
  502. let right_x = context.canvas.width - 50 - 10;
  503. this.iconsList = [];
  504. // 'Descriptive labels' for the navigation icons
  505. this.left_text = game.add.text(left_x, 73, '', textStyles.h4_brown);
  506. this.left_text.align = 'left';
  507. this.right_text = game.add.text(right_x + 50, 73, '', textStyles.h4_brown);
  508. this.right_text.align = 'right';
  509. // Left icons
  510. if (leftIcon0) {
  511. // Return to previous screen
  512. if (!state) {
  513. console.error(
  514. "Game error: You tried to add a 'back' icon without the 'state' parameter."
  515. );
  516. } else {
  517. this.state = state;
  518. this.iconsList.push(game.add.image(left_x, 10, 'back'));
  519. left_x += 50;
  520. }
  521. }
  522. if (leftIcon1) {
  523. // Return to main menu screen
  524. this.iconsList.push(game.add.image(left_x, 10, 'menu'));
  525. left_x += 50;
  526. }
  527. if (leftIcon2) {
  528. // Shows solution to the game
  529. if (!help) {
  530. console.error(
  531. "Game error: You tried to add a 'game solution' icon without the 'help' parameter."
  532. );
  533. } else {
  534. this.help = help;
  535. this.iconsList.push(game.add.image(left_x, 10, 'help'));
  536. left_x += 50;
  537. }
  538. }
  539. // Right icons
  540. if (rightIcon0) {
  541. // Turns game audio on/off
  542. this.audioIcon = game.add.sprite(right_x, 10, 'audio', 1);
  543. this.audioIcon.curFrame = audioStatus ? 0 : 1;
  544. this.iconsList.push(this.audioIcon);
  545. right_x -= 50;
  546. }
  547. if (rightIcon1) {
  548. // Return to select language screen
  549. this.iconsList.push(game.add.image(right_x, 10, 'language'));
  550. right_x -= 50;
  551. }
  552. },
  553. /**
  554. * When 'back' icon is clicked go to this state
  555. *
  556. * @param {string} state name of the next state
  557. */
  558. callState: function (state) {
  559. if (audioStatus) game.audio.popSound.play();
  560. game.event.clear(self);
  561. game.state.start(state);
  562. },
  563. /**
  564. * Called by mouse click event
  565. *
  566. * @param {number} x contains the mouse x coordinate
  567. * @param {number} y contains the mouse y coordinate
  568. */
  569. onInputDown: function (x, y) {
  570. navigationIcons.iconsList.forEach((cur) => {
  571. if (game.math.isOverIcon(x, y, cur)) {
  572. const name = cur.name;
  573. switch (name) {
  574. case 'back':
  575. navigationIcons.callState(navigationIcons.state);
  576. break;
  577. case 'menu':
  578. navigationIcons.callState('menu');
  579. break;
  580. case 'help':
  581. navigationIcons.help();
  582. break;
  583. case 'language':
  584. navigationIcons.callState('lang');
  585. break;
  586. case 'audio':
  587. if (audioStatus) {
  588. audioStatus = false;
  589. navigationIcons.audioIcon.curFrame = 1;
  590. } else {
  591. audioStatus = true;
  592. if (audioStatus) game.audio.popSound.play();
  593. navigationIcons.audioIcon.curFrame = 0;
  594. }
  595. game.render.all();
  596. break;
  597. default:
  598. console.error('Game error: error in navigation icon');
  599. }
  600. }
  601. });
  602. },
  603. /**
  604. * Called by mouse move event
  605. *
  606. * @param {number} x contains the mouse x coordinate
  607. * @param {number} y contains the mouse y coordinate
  608. */
  609. onInputOver: function (x, y) {
  610. let flag = false;
  611. navigationIcons.iconsList.forEach((cur) => {
  612. if (game.math.isOverIcon(x, y, cur)) {
  613. flag = true;
  614. let name = cur.name;
  615. switch (name) {
  616. case 'back':
  617. navigationIcons.left_text.name = game.lang.nav_back;
  618. break;
  619. case 'menu':
  620. navigationIcons.left_text.name = game.lang.nav_menu;
  621. break;
  622. case 'help':
  623. navigationIcons.left_text.name = game.lang.nav_help;
  624. break;
  625. case 'language':
  626. navigationIcons.right_text.name = game.lang.nav_lang;
  627. break;
  628. case 'audio':
  629. navigationIcons.right_text.name = game.lang.audio;
  630. break;
  631. }
  632. }
  633. });
  634. if (!flag) {
  635. navigationIcons.left_text.name = '';
  636. navigationIcons.right_text.name = '';
  637. } else {
  638. document.body.style.cursor = 'pointer';
  639. }
  640. },
  641. };
  642. /**
  643. * Sends game information to database
  644. *
  645. * @param {string} extraData player information for the current game
  646. */
  647. const sendToDB = function (extraData) {
  648. // FOR MOODLE
  649. if (moodle) {
  650. if (self.result) moodleVar.hits[mapPosition - 1]++;
  651. else moodleVar.errors[mapPosition - 1]++;
  652. moodleVar.time[mapPosition - 1] += game.timer.elapsed;
  653. const url = iLMparameters.iLM_PARAM_ServerToGetAnswerURL;
  654. const grade = '' + getEvaluation();
  655. const report = getAnswer();
  656. const data =
  657. 'return_get_answer=1' +
  658. '&iLM_PARAM_ActivityEvaluation=' +
  659. encodeURIComponent(grade) +
  660. '&iLM_PARAM_ArchiveContent=' +
  661. encodeURIComponent(report);
  662. const init = {
  663. method: 'POST',
  664. body: data,
  665. headers: {
  666. 'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
  667. },
  668. };
  669. fetch(url, init)
  670. .then((response) => {
  671. if (response.ok) {
  672. if (debugMode) console.log('Processing...');
  673. } else {
  674. console.error('Game error: Network response was not ok.');
  675. }
  676. })
  677. .catch((error) => {
  678. console.error(
  679. 'Game error: problem with fetch operation - ' + error.message + '.'
  680. );
  681. });
  682. } else {
  683. // Create some variables we need to send to our PHP file
  684. // Attention: this names must be compactible to data table (MySQL server)
  685. // @see php/save.php
  686. const data =
  687. 'line_ip=143.107.45.11' + // INSERT database server IP
  688. '&line_name=' +
  689. playerName +
  690. '&line_lang=' +
  691. langString +
  692. extraData;
  693. const url = 'php/save.php';
  694. const init = {
  695. method: 'POST',
  696. body: data,
  697. headers: { 'Content-type': 'application/x-www-form-urlencoded' },
  698. };
  699. fetch(url, init)
  700. .then((response) => {
  701. if (response.ok) {
  702. if (debugMode) console.log('Processing...');
  703. response.text().then((text) => {
  704. if (debugMode) {
  705. console.log(text);
  706. }
  707. });
  708. } else {
  709. console.error('Game error: Network response was not ok.');
  710. }
  711. })
  712. .catch((error) => {
  713. console.error(
  714. 'Game error: problem with fetch operation - ' + error.message + '.'
  715. );
  716. });
  717. }
  718. };
  719. let gameFrame = function () {
  720. let x = (y = 200);
  721. let width = context.canvas.width - 2 * x;
  722. let height = context.canvas.height - 2 * y;
  723. let rect = function () {
  724. game.add.geom.rect(x, y, width, height, colors.red, 2);
  725. };
  726. let point = function (offsetW, offsetH) {
  727. for (let i = 0, y1 = y; i < 4; i++) {
  728. x1 = x;
  729. for (let j = 0; j < 7; j++) {
  730. game.add.geom.rect(x1, y1, 20, 20, undefined, 0, colors.red, 1);
  731. x1 += offsetW;
  732. }
  733. y1 += offsetH;
  734. }
  735. };
  736. return { x, y, width, height, rect, point };
  737. };
  738. // For debug
  739. const debug = {
  740. grid: function () {
  741. const grid = 2;
  742. const h = 1920 / (grid + 0.5);
  743. const v = 1080 / (grid + 0.5);
  744. for (let i = 0; i < grid; i++) {
  745. game.add.geom.rect(
  746. h / 2 + i * h,
  747. 0,
  748. h / 2,
  749. 1080,
  750. '',
  751. 0,
  752. colors.blue,
  753. 0.3
  754. );
  755. game.add.geom.rect(
  756. 0,
  757. v / 2 + i * v,
  758. 1920,
  759. v / 2,
  760. '',
  761. 0,
  762. colors.blue,
  763. 0.3
  764. );
  765. }
  766. },
  767. };