globals.js 15 KB

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