globals.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. const defaultWidth = 900;
  2. const defaultHeight = 600;
  3. const medSrc = "assets/img/";
  4. const debugMode = false; // Turns console messages ON/OFF
  5. let audioStatus = false; // Turns game audio ON/OFF
  6. let fractionLabel = true; // Turns showing fractions in levels ON/OFF
  7. let playerName;
  8. let langString; // String that contains the selected language
  9. let self; // Current state
  10. let mapPosition; // character position in the map (1..4 valid, 5 end)
  11. let mapMove; // When true, the character can move to next position in the map
  12. let completedLevels; // Number of finished levels in the map
  13. /*
  14. ....................................................
  15. .............square.................circle.......... } } (gameShape)
  16. .........../........\.................|............. } game (gameType)
  17. ........One..........Two.............One............ }
  18. ......./...\..........|............./...\...........
  19. ......A.....B.........C............A.....B.......... } level (levelType)
  20. .(floor)..(stack)..(equal).....(floor).(circle)..... }
  21. .......\./............|..............\./............
  22. ........|.............|...............|.............
  23. ......./.\........../.|.\.........../.|.\...........
  24. ...Plus...Minus....A..B..C.....Plus.Minus.Mixed..... } sublevel (sublevelType)
  25. .......\./..........\.|./...........\.|./...........
  26. ........|.............|...............|.............
  27. ......1,2,3.......1,2,3,4,5.......1,2,3,4,5......... } difficulty (gameDifficulty)
  28. ....................................................
  29. */
  30. const TESTE = {
  31. gameShape: [
  32. 'Square',
  33. 'Square',
  34. 'Circle'
  35. ],
  36. gameType: [
  37. 'squareOne',
  38. 'squareTwo',
  39. 'circleOne'
  40. ],
  41. levelType: [
  42. ['A', 'B'],
  43. ['C'],
  44. ['A', 'B']
  45. ],
  46. sublevelType: [
  47. ['Plus', 'Minus'],
  48. ['A', 'B', 'C'],
  49. ['Plus', 'Minus', 'Mixed']
  50. ],
  51. gameDifficulty: [
  52. 3,
  53. 5,
  54. 5
  55. ],
  56. };
  57. /* GAME TYPE (in menu screen)
  58. * can be: squareOne, 'SquareTwo' or 'CircleOne' */
  59. let gameType, gameTypeString;
  60. let gameShape; // can be: 'circle' or 'square'
  61. /* LEVEL TYPE (in menu screen)
  62. * in squareOne/circleOne can be: 'A' (click on the floor) or 'B' (click on the amount to go/stacked figures)
  63. * in squareTwo can be: 'C' (comparing fractions) */
  64. let levelType;
  65. /* SUBLEVEL TYPE (in difficulty screen)
  66. * in squareOne can be: 'Plus' or 'Minus'
  67. * in circleOne can be: 'Plus', 'Minus' or 'Mixed'
  68. * in squareTwo can be: 'A', 'B' or 'C' */
  69. let sublevelType;
  70. /* GAME DIFFICULTY
  71. * in squareOne can be: 1..3
  72. * in circleOne/squareTwo can be: 1..5 */
  73. let gameDifficulty;
  74. // Colors
  75. const colors = {
  76. white: "#fff",
  77. black: "#000",
  78. gray: "#708090",
  79. // used in text
  80. green: "#00804d",
  81. blue: "#003cb3",
  82. darkRed: "#330000",
  83. mediumBlue: "#000080",
  84. // difficulty stairs
  85. diffBlue: "#99b3ff",
  86. diffRed: "#ff6666",
  87. diffPurple: "#b366ff",
  88. // Background color
  89. blueBckg: "#cce5ff", // default
  90. blueBckgLevel: "#a8c0e6", // in squareOne (used for floor gap)
  91. // ok button in name State
  92. teal: "#3d5c5c",
  93. // difficulty symbols and game color identifier
  94. red: "#b30000",
  95. darkBlue: "#183780",
  96. lightBlue: "#efeff5",
  97. // squareTwo
  98. darkRed: "#330000",
  99. lightRed: "#d27979",
  100. lighterRed: "#2d9d9",
  101. darkGreen: "#1e2f2f",
  102. lightGreen: "#83afaf",
  103. lighterGreen: "#e0ebeb",
  104. };
  105. // Text styles
  106. const textStyles = {
  107. dafault: { font: "12px Arial", fill: colors.black, align: "center" },
  108. // titles
  109. title1: { font: "32px Arial", fill: colors.green, align: "center" },
  110. title2: { font: "27px Arial", fill: colors.green, align: "center" },
  111. title2right: { font: "27px Arial", fill: colors.green, align: 'right' },
  112. overtitle: { font: "20px Arial", fill: colors.darkRed, align: "center" },
  113. overtitlel: { font: "20px Arial", fill: colors.darkRed, align: "left" },
  114. overtitler: { font: "20px Arial", fill: colors.darkRed, align: "right" },
  115. subtitle1: { font: "27px Arial", fill: colors.blue, align: "center" },
  116. subtitle2: { font: "27px Arial", fill: colors.black, align: "center" },
  117. subtitle2l: { font: "27px Arial", fill: colors.black, align: "left" },
  118. subtitle2r: { font: "27px Arial", fill: colors.black, align: "right" },
  119. // button labels
  120. buttonLabel: { font: '34px Arial', fill: colors.white, align: 'center' },
  121. difficultyLabel: { font: '25px Arial', fill: colors.white, align: 'center' },
  122. // in game labels
  123. valueLabelBlue1: { font: '26px Arial', fill: colors.mediumBlue, align: 'center' },
  124. valueLabelBlue2: { font: '20px Arial', fill: colors.mediumBlue, align: 'center' }, // numbers in squareTwo
  125. valueLabelBlue3: { font: '15px Arial', fill: colors.mediumBlue, align: 'center' }, // fractions numbers in squareOne
  126. };
  127. // Navigation icons on the top of the screen
  128. const navigationIcons = {
  129. // Add navigation icons on the top of the screen based on parameters
  130. func_addIcons: function (left_btn0, left_btn1, left_btn2, // first 3 icon spaces
  131. right_btn0, right_btn1, // last 2 icon spaces
  132. level, helpBtn) { // auxiliar variables
  133. this.level = level;
  134. this.helpBtn = helpBtn;
  135. let left_x = 10;
  136. let right_x = defaultWidth - 50 - 10;
  137. this.iconsList = [];
  138. // 'Descriptive labels' for the navigation icons
  139. this.left_text = game.add.text(left_x, 73, "", textStyles.overtitlel);
  140. this.right_text = game.add.text(right_x + 50, 73, "", textStyles.overtitler);
  141. // 'Icons' on the LEFT side of the page
  142. if (left_btn0) { // Return to select difficulty screen
  143. const icon_back = game.add.image(left_x, 10, 'back');
  144. this.iconsList.push(icon_back);
  145. left_x += 50; // Offsets value of x for next icon
  146. }
  147. if (left_btn1) { // Return to main menu screen
  148. const icon_list = game.add.image(left_x, 10, 'menu');
  149. this.iconsList.push(icon_list);
  150. left_x += 50; // Offsets value of x for next icon
  151. }
  152. if (left_btn2) { // In some levels, shows solution to the game
  153. const icon_help = game.add.image(left_x, 10, 'help');
  154. this.iconsList.push(icon_help);
  155. left_x += 50; // Offsets value of x for next icon
  156. }
  157. // 'Icons' on the RIGHT side of the page
  158. if (right_btn0) { // Turns game audio on/off
  159. this.icon_audio = game.add.sprite(right_x, 10, 'audio', 1);
  160. audioStatus ? this.icon_audio.curFrame = 0 : this.icon_audio.curFrame = 1;
  161. this.iconsList.push(this.icon_audio);
  162. right_x -= 50; // Offsets value of x for next icon
  163. }
  164. if (right_btn1) { // Return to select language screen
  165. icon_world = game.add.image(right_x, 10, 'language');
  166. this.iconsList.push(icon_world);
  167. right_x -= 50; // Offsets value of x for next icon
  168. }
  169. },
  170. func_CallScreen: function (screen) {
  171. if (audioStatus) game.audio.beepSound.play();
  172. game.event.clear(self);
  173. screen.preload();
  174. },
  175. func_onInputDown: function (x, y) {
  176. navigationIcons.iconsList.forEach(cur => {
  177. const valid = y >= cur.yWithAnchor && y <= (cur.yWithAnchor + cur.height * cur.scaleHeight) &&
  178. (x >= cur.xWithAnchor && x <= (cur.xWithAnchor + cur.width * cur.scaleWidth));
  179. if (valid) {
  180. if (cur.name == 'back') navigationIcons.func_CallScreen(navigationIcons.level);
  181. else if (cur.name == 'menu') navigationIcons.func_CallScreen(menuScreen);
  182. else if (cur.name == 'help') navigationIcons.helpBtn();
  183. else if (cur.name == 'language') navigationIcons.func_CallScreen(langScreen);
  184. else if (cur.name == 'audio') {
  185. if (audioStatus) {
  186. audioStatus = false;
  187. navigationIcons.icon_audio.curFrame = 1;
  188. } else {
  189. audioStatus = true;
  190. navigationIcons.icon_audio.curFrame = 0;
  191. }
  192. game.render.all();
  193. }
  194. }
  195. });
  196. },
  197. func_onInputOver: function (x, y) {
  198. let flag = false;
  199. navigationIcons.iconsList.forEach(cur => {
  200. const valid = y >= cur.yWithAnchor && y <= (cur.yWithAnchor + cur.height * cur.scaleHeight) &&
  201. (x >= cur.xWithAnchor && x <= (cur.xWithAnchor + cur.width * cur.scaleWidth));
  202. if (valid) {
  203. flag = true;
  204. if (cur.name == 'back') navigationIcons.left_text.name = game.lang.menu_back;
  205. else if (cur.name == 'menu') navigationIcons.left_text.name = game.lang.menu_list;
  206. else if (cur.name == 'help') navigationIcons.left_text.name = game.lang.menu_help;
  207. else if (cur.name == 'language') navigationIcons.right_text.name = game.lang.menu_world;
  208. else if (cur.name == 'audio') navigationIcons.right_text.name = game.lang.audio;
  209. }
  210. });
  211. if (!flag) {
  212. navigationIcons.left_text.name = "";
  213. navigationIcons.right_text.name = "";
  214. } else {
  215. document.body.style.cursor = "pointer";
  216. }
  217. }
  218. };
  219. // Send game information to database
  220. const postScore = function (extraData) {
  221. // Create some variables we need to send to our PHP file
  222. const data = "s_ip=143.107.45.11"
  223. + "&s_name=" + playerName
  224. + "&s_lang=" + langString
  225. + extraData;
  226. const url = "php/save.php";
  227. const hr = new XMLHttpRequest();
  228. hr.open("POST", url, true);
  229. hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  230. hr.onreadystatechange = function () {
  231. if (debugMode) console.log(hr);
  232. if (hr.readyState == 4 && hr.status == 200) {
  233. if (debugMode) console.log(hr.responseText);
  234. }
  235. }
  236. // Send the data to PHP now... and wait for response to update the status div
  237. hr.send(data); // Actually execute the request
  238. if (debugMode) {
  239. console.log("processing...");
  240. console.log(data);
  241. }
  242. };