globals.js 16 KB

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