globals.js 17 KB

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