globals.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*
  2. LInE - Free Education, Private Data.
  3. ....................................................
  4. .............square.................circle.......... } } (gameShape)
  5. .........../........\.................|............. } game (gameType)
  6. ........One..........Two.............One............ }
  7. ......./...\..........|............./...\...........
  8. ......A.....B.........C............A.....B.......... } level (levelType)
  9. .(floor)..(stack)..(equal).....(floor).(stack)...... }
  10. .......\./............|..............\./............
  11. ........|.............|...............|.............
  12. ......./.\.........../.\............/.|.\...........
  13. ...Plus...Minus.....B...C.......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: 'C' (comparing fractions).
  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: 'B' or 'C'
  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: ['C'],
  143. levelTypeUrl: [],
  144. sublevelType: [/*'A',*/ 'B', 'C'],
  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. h4_white: { font: '20px Arial,sans-serif', fill: colors.white, align: 'center' }, // Difficulty numbers (menuState)
  221. p_white: { font: '14px Arial,sans-serif', fill: colors.white, align: 'center' }, // Enter button (menuState)
  222. h2_brown: { font: '26px Arial,sans-serif', fill: colors.darkRed, align: 'center' }, // Map difficulty label
  223. h4_brown: { font: '20px Arial,sans-serif', fill: colors.darkRed, align: 'center' }, // Menu overtitle
  224. h2_blue_2: { font: '26px Arial,sans-serif', fill: colors.blue, align: 'center' }, // Menu subtitle
  225. h4_blue_2: { font: '20px Arial,sans-serif', fill: colors.blue, align: 'center' }, // Menu subtitle
  226. h2_blue: { font: '26px Arial,sans-serif', fill: colors.darkBlue, align: 'center' }, // Fractions
  227. h4_blue: { font: '20px Arial,sans-serif', fill: colors.darkBlue, align: 'center' }, // Fractions
  228. p_blue: { font: '14px Arial,sans-serif', fill: colors.darkBlue, align: 'center' } // Fractions
  229. };
  230. // List of media URL
  231. /**
  232. * List of URL for all media in the game.<br>
  233. * Divided: 1st by the state that loads the media / 2nd by the media type.
  234. * @type {object}
  235. */
  236. const url = {
  237. boot: {
  238. image: [
  239. // Scene
  240. ['bgimage', medSrc + 'scene/bg.jpg'],
  241. ['bgmap', medSrc + 'scene/bg_map.png'],
  242. ['bush', medSrc + 'scene/bush.png'],
  243. ['cloud', medSrc + 'scene/cloud.png'],
  244. ['floor', medSrc + 'scene/floor.png'],
  245. ['place_off', medSrc + 'scene/place_off.png'],
  246. ['place_on', medSrc + 'scene/place_on.png'],
  247. ['rock', medSrc + 'scene/rock.png'],
  248. ['road', medSrc + 'scene/road.png'],
  249. ['sign', medSrc + 'scene/sign.png'],
  250. ['tree1', medSrc + 'scene/tree.png'],
  251. ['tree2', medSrc + 'scene/tree2.png'],
  252. ['tree3', medSrc + 'scene/tree3.png'],
  253. ['tree4', medSrc + 'scene/tree4.png'],
  254. // Flags
  255. ['flag_BR', medSrc + 'flag/BRAZ.jpg'],
  256. ['flag_FR', medSrc + 'flag/FRAN.jpg'],
  257. ['flag_IT', medSrc + 'flag/ITAL.png'],
  258. ['flag_PE', medSrc + 'flag/PERU.jpg'],
  259. ['flag_US', medSrc + 'flag/UNST.jpg'],
  260. // Navigation icons on the top of the page
  261. ['back', medSrc + 'navig_icon/back.png'],
  262. ['help', medSrc + 'navig_icon/help.png'],
  263. ['home', medSrc + 'navig_icon/home.png'],
  264. ['language', medSrc + 'navig_icon/language.png'],
  265. ['menu', medSrc + 'navig_icon/menu.png'],
  266. // Interactive icons
  267. ['arrow_down', medSrc + 'interac_icon/down.png'],
  268. ['error', medSrc + 'interac_icon/error.png'],
  269. ['help_pointer', medSrc + 'interac_icon/pointer.png'],
  270. ['ok', medSrc + 'interac_icon/ok.png'],
  271. // Non-interactive icons
  272. ['arrow_double', medSrc + 'non_interac_icon/double.png'],
  273. ['arrow_left', medSrc + 'non_interac_icon/left_arrow.png'],
  274. ['arrow_right', medSrc + 'non_interac_icon/right_arrow.png'],
  275. ['equal', medSrc + 'non_interac_icon/equal.png']
  276. ],
  277. sprite: [
  278. // Game Sprites
  279. ['kid_walk', medSrc + 'character/kid/walk.png', 26],
  280. // Navigation icons on the top of the page
  281. ['audio', medSrc + 'navig_icon/audio.png', 2],
  282. // Interactive icons
  283. ['select', medSrc + 'interac_icon/selectionBox.png', 2]
  284. ],
  285. audio: [
  286. // Sound effects
  287. ['beepSound', ['assets/audio/beep.ogg', 'assets/audio/beep.mp3']],
  288. ['okSound', ['assets/audio/ok.ogg', 'assets/audio/ok.mp3']],
  289. ['errorSound', ['assets/audio/error.ogg', 'assets/audio/error.mp3']]
  290. ]
  291. },
  292. menu: {
  293. image: [
  294. // Game
  295. ['game0', medSrc + 'levels/squareOne.png'], // Square I
  296. ['game1', medSrc + 'levels/circleOne.png'], // Circle I
  297. ['game2', medSrc + 'levels/squareTwo.png'], // Square II
  298. // Level
  299. ['level0', medSrc + 'levels/squareOne_1.png'], // Square I : A
  300. ['level1', medSrc + 'levels/squareOne_2.png'], // Square I : B
  301. ['level2', medSrc + 'levels/circleOne_1.png'], // Circle I : A
  302. ['level3', medSrc + 'levels/circleOne_2.png'], // Circle I : B
  303. ['level4', medSrc + 'levels/squareTwo.png'], // Square II : C
  304. // Sublevel
  305. ['sublevel_right', medSrc + 'levels/sublevel_right.png'], // Square/circle I : right
  306. ['sublevel_left', medSrc + 'levels/sublevel_left.png'], // Square/circle I : left
  307. ['sublevel_mixed', medSrc + 'levels/sublevel_mixed.png'], // Circle I : mixed
  308. ['sublevel_top', medSrc + 'levels/sublevel_top.png'], // Square II : top
  309. ['sublevel_bottom', medSrc + 'levels/sublevel_bottom.png'] // Square II : bottom
  310. ],
  311. sprite: [],
  312. audio: []
  313. },
  314. squareOne: {
  315. image: [
  316. // Scene
  317. ['farm', medSrc + 'scene/farm.png'],
  318. ['garage', medSrc + 'scene/garage.png']
  319. ],
  320. sprite: [
  321. // Game sprites
  322. ['tractor', medSrc + 'character/tractor/tractor.png', 15]
  323. ],
  324. audio: []
  325. },
  326. squareTwo: {
  327. image: [
  328. // Scene
  329. ['house', medSrc + 'scene/house.png'],
  330. ['school', medSrc + 'scene/school.png']
  331. ],
  332. sprite: [
  333. // Game sprites
  334. ['kid_standing', medSrc + 'character/kid/lost.png', 6],
  335. ['kid_run', medSrc + 'character/kid/run.png', 12]
  336. ],
  337. audio: []
  338. },
  339. circleOne: {
  340. image: [
  341. // Scene
  342. ['house', medSrc + 'scene/house.png'],
  343. ['school', medSrc + 'scene/school.png'],
  344. // Game images
  345. ['balloon', medSrc + 'character/balloon/airballoon_upper.png'],
  346. ['balloon_basket', medSrc + 'character/balloon/airballoon_base.png']
  347. ],
  348. sprite: [
  349. // Game sprites
  350. ['kid_run', medSrc + 'character/kid/run.png', 12]
  351. ],
  352. audio: []
  353. },
  354. };
  355. /**
  356. * Manages navigation icons on the top of the screen
  357. * @namespace
  358. */
  359. const navigationIcons = {
  360. /**
  361. * Add navigation icons.<br>
  362. * The icons on the left are ordered from left to right.
  363. * The icons on the right are ordered from right to left.
  364. *
  365. * @param {boolean} leftIcon0 1st left icon
  366. * @param {boolean} leftIcon1 2nd left icon
  367. * @param {boolean} leftIcon2 3rd left icon
  368. * @param {boolean} rightIcon0 1st right icon
  369. * @param {boolean} rightIcon1 2nd right icon
  370. * @param {string} level state to be called by the 'back' button
  371. * @param {function} help function in the current game state that display correct answer
  372. */
  373. func_addIcons: function (leftIcon0, leftIcon1, leftIcon2, rightIcon0, rightIcon1, level, help) {
  374. this.level = level;
  375. this.help = help;
  376. let left_x = 10;
  377. let right_x = defaultWidth - 50 - 10;
  378. this.iconsList = [];
  379. // 'Descriptive labels' for the navigation icons
  380. this.left_text = game.add.text(left_x, 73, '', textStyles.h4_brown, 'left');
  381. this.right_text = game.add.text(right_x + 50, 73, '', textStyles.h4_brown, 'right');
  382. // 'Icons' on the LEFT side of the page
  383. if (leftIcon0) { // Return to select difficulty screen
  384. const icon_back = game.add.image(left_x, 10, 'back');
  385. this.iconsList.push(icon_back);
  386. left_x += 50; // Offsets value of x for next icon
  387. }
  388. if (leftIcon1) { // Return to main menu screen
  389. const icon_list = game.add.image(left_x, 10, 'menu');
  390. this.iconsList.push(icon_list);
  391. left_x += 50; // Offsets value of x for next icon
  392. }
  393. if (leftIcon2) { // In some levels, shows solution to the game
  394. const icon_help = game.add.image(left_x, 10, 'help');
  395. this.iconsList.push(icon_help);
  396. left_x += 50; // Offsets value of x for next icon
  397. }
  398. // 'Icons' on the RIGHT side of the page
  399. if (rightIcon0) { // Turns game audio on/off
  400. this.icon_audio = game.add.sprite(right_x, 10, 'audio', 1);
  401. audioStatus ? this.icon_audio.curFrame = 0 : this.icon_audio.curFrame = 1;
  402. this.iconsList.push(this.icon_audio);
  403. right_x -= 50; // Offsets value of x for next icon
  404. }
  405. if (rightIcon1) { // Return to select language screen
  406. icon_world = game.add.image(right_x, 10, 'language');
  407. this.iconsList.push(icon_world);
  408. right_x -= 50; // Offsets value of x for next icon
  409. }
  410. },
  411. /**
  412. * When 'back' icon is clicked go to this state
  413. *
  414. * @param {string} state name of the next state
  415. */
  416. func_CallState: function (state) {
  417. if (audioStatus) game.audio.beepSound.play();
  418. game.event.clear(self);
  419. game.state.start(state);
  420. },
  421. /**
  422. * Called by mouse click event
  423. *
  424. * @param {number} x contains the mouse x coordinate
  425. * @param {number} y contains the mouse y coordinate
  426. */
  427. func_onInputDown: function (x, y) {
  428. navigationIcons.iconsList.forEach(cur => {
  429. if (game.math.isOverIcon(x, y, cur)) {
  430. const name = cur.name;
  431. switch (name) {
  432. case 'back': navigationIcons.func_CallState(navigationIcons.level); break;
  433. case 'menu': navigationIcons.func_CallState('menu'); break;
  434. case 'help': navigationIcons.help(); break;
  435. case 'language': navigationIcons.func_CallState('lang'); break;
  436. case 'audio':
  437. if (audioStatus) {
  438. audioStatus = false;
  439. navigationIcons.icon_audio.curFrame = 1;
  440. } else {
  441. audioStatus = true;
  442. if (audioStatus) game.audio.beepSound.play();
  443. navigationIcons.icon_audio.curFrame = 0;
  444. }
  445. game.render.all();
  446. break;
  447. default: console.log('Game error: error in navigation icon');
  448. }
  449. }
  450. });
  451. },
  452. /**
  453. * Called by mouse move event
  454. *
  455. * @param {number} x contains the mouse x coordinate
  456. * @param {number} y contains the mouse y coordinate
  457. */
  458. func_onInputOver: function (x, y) {
  459. let flag = false;
  460. navigationIcons.iconsList.forEach(cur => {
  461. if (game.math.isOverIcon(x, y, cur)) {
  462. flag = true;
  463. if (cur.name == 'back') navigationIcons.left_text.name = game.lang.menu_back;
  464. else if (cur.name == 'menu') navigationIcons.left_text.name = game.lang.menu_list;
  465. else if (cur.name == 'help') navigationIcons.left_text.name = game.lang.menu_help;
  466. else if (cur.name == 'language') navigationIcons.right_text.name = game.lang.menu_world;
  467. else if (cur.name == 'audio') navigationIcons.right_text.name = game.lang.audio;
  468. }
  469. });
  470. if (!flag) {
  471. navigationIcons.left_text.name = '';
  472. navigationIcons.right_text.name = '';
  473. } else {
  474. document.body.style.cursor = 'pointer';
  475. }
  476. }
  477. };
  478. /**
  479. * Sends game information to database
  480. *
  481. * @param {string} extraData player information for the current game
  482. */
  483. const sendToDB = function (extraData) {
  484. // Create some variables we need to send to our PHP file
  485. // Attention: this names must be compactible to data table (MySQL server)
  486. // @see php/save.php
  487. const data = 'line_ip=143.107.45.11' // INSERT database server IP
  488. + '&line_name=' + playerName
  489. + '&line_lang=' + langstring
  490. + extraData;
  491. const url = 'php/save.php';
  492. const hr = new XMLHttpRequest();
  493. hr.open('POST', url, true);
  494. hr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  495. hr.onreadystatechange = function () {
  496. if (debugMode) console.log(hr);
  497. if (hr.readyState == 4 && hr.status == 200) {
  498. if (debugMode) console.log(hr.responseText);
  499. }
  500. }
  501. hr.send(data); // Actually execute the request
  502. if (debugMode) {
  503. console.log('processing...');
  504. console.log(data);
  505. }
  506. };