globals.js 18 KB

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