globals.js 18 KB

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