globals.js 18 KB

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