globals.js 15 KB

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