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