globals.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. const imgsrc = "assets/img/"
  2. const defaultWidth = 900;
  3. const defaultHeight = 600;
  4. const debugMode = false; // Turns console messages ON/OFF
  5. let audioStatus = false; // Turns game audio ON/OFF
  6. let lang, langString; // Object and String that contain the selected language
  7. let playerName;
  8. let levelLabel = true; // Turns explicitly showing the fractions in levels ON/OFF
  9. let currentGameState; // Name of the current selected 'game' state
  10. let self;
  11. let mapPosition; // character position in the map
  12. let mapCanMove; // When true the character can move to next position in the map
  13. let completedLevels; // Number of finished levels in the map
  14. let levelShape; // Can be 'circle' or 'square'
  15. let levelDifficulty; // A value from 1..3 or 1..5 that defines the current level difficulty
  16. /* LEVEL TYPE (the ones in the menu screen)
  17. * in squareOne/circleOne can be: 'A' (click on the floor) or 'B' (click on the amount to go/stacked figures)
  18. * in squareTwo can be: 'C' (comparing fractions) */
  19. let levelType;
  20. /* SUBLEVEL TYPE (the ones in the difficulty screen)
  21. * in squareOne levels can be: 'Plus' or 'Minus'
  22. * in circleOne levels can be: 'Plus', 'Minus' or 'Mixed'
  23. * in squareTwo levels can be: 'A', 'B' or 'C' */
  24. let sublevelType;
  25. let beepSound, okSound, errorSound; // output game sounds
  26. let timer, totalTime; // Counts time spent in each game
  27. const hip = "143.107.45.11"; // Host ip
  28. // Colors available
  29. const colors = {
  30. // used in text
  31. green: "#00804d",
  32. darkRed: "#330000",
  33. blue: "#003cb3",
  34. mediumBlue: "#000080",
  35. black: "#000000",
  36. almostWhite: "#f0f5f5",
  37. // difficulty stairs
  38. diffBlue: "0x99b3ff",
  39. diffRed: "0xff6666",
  40. diffPurple: "0xb366ff",
  41. // Background color
  42. blueBckg: 0xcce5ff, // default
  43. blueBckgLevel: 0xa8c0e6, // in gameSquareOne (used for floor gap)
  44. // ok button in name State
  45. teal: 0x3d5c5c,
  46. // difficulty symbols and game color identifier
  47. darkBlue: 0x31314e,
  48. red: 0xb30000,
  49. lightBlue: 0xefeff5,
  50. // gameSquareTwo
  51. darkRed_: 0x330000,
  52. lightRed: 0xd27979,
  53. lighterRed: 0xf2d9d9,
  54. darkGreen: 0x1e2f2f,
  55. lightGreen: 0x83afaf,
  56. lighterGreen: 0xe0ebeb,
  57. };
  58. // Text styles available
  59. const textStyles = {
  60. // titles
  61. title1: { font: "32px Arial", fill: colors.green, align: "center" },
  62. title2: { font: "27px Arial", fill: colors.green, align: "center" },
  63. overtitle: { font: "20px Arial", fill: colors.darkRed, align: "center" },
  64. subtitle1: { font: "27px Arial", fill: colors.blue, align: "center" },
  65. subtitle2: { font: "27px Arial", fill: colors.black, align: "center" },
  66. // button labels
  67. buttonLabel: { font: '34px Arial', fill: colors.almostWhite, align: 'center' },
  68. difficultyLabel: { font: '25px Arial', fill: colors.almostWhite, align: 'center' },
  69. // in game labels
  70. valueLabelBlue1: { font: '26px Arial', fill: colors.mediumBlue, align: 'center' },
  71. valueLabelBlue2: { font: '20px Arial', fill: colors.mediumBlue, align: 'center' }, // numbers in gameSquareTwo
  72. valueLabelBlue3: { font: '15px Arial', fill: colors.mediumBlue, align: 'center' }, // fractions numbers in gameSquareOne
  73. };
  74. // images, spritesheets and audio files
  75. const media = {
  76. boot : function ( type ) {
  77. image = [
  78. // scenario
  79. ['birch', imgsrc+'scenario/birch.png'],
  80. ['bgimage', imgsrc+'scenario/bg.jpg'],
  81. ['bgmap', imgsrc+'scenario/bg_map.png'],
  82. ['cloud', imgsrc+'scenario/cloud.png'],
  83. ['floor', imgsrc+'scenario/floor.png'],
  84. ['place_a', imgsrc+'scenario/place_a.png'],
  85. ['place_b', imgsrc+'scenario/place_b.png'],
  86. ['rock', imgsrc+'scenario/rock.png'],
  87. ['road', imgsrc+'scenario/road.png'],
  88. ['sign', imgsrc+'scenario/sign.png'],
  89. ['tree1', imgsrc+'scenario/tree.png'],
  90. ['tree2', imgsrc+'scenario/tree2.png'],
  91. ['tree3', imgsrc+'scenario/tree3.png'],
  92. ['tree4', imgsrc+'scenario/tree4.png'],
  93. // flags
  94. ['flag_BR', imgsrc+'flag/BRAZ.jpg'],
  95. ['flag_FR', imgsrc+'flag/FRAN.jpg'],
  96. ['flag_IT', imgsrc+'flag/ITAL.png'],
  97. ['flag_PE', imgsrc+'flag/PERU.jpg'],
  98. ['flag_US', imgsrc+'flag/UNST.jpg'],
  99. // Navigation icons on the top of the page
  100. ['back', imgsrc+'navIcon/back.png'],
  101. ['block', imgsrc+'navIcon/block.png'],
  102. ['help', imgsrc+'navIcon/help.png'],
  103. ['home', imgsrc+'navIcon/home.png'],
  104. ['world', imgsrc+'navIcon/language.png'],
  105. ['list', imgsrc+'navIcon/menu.png'],
  106. ['pgbar', imgsrc+'navIcon/progressBar.png'],
  107. // mathematical operators
  108. ['equal', imgsrc+'operator/equal.png'],
  109. ['fractionLine', imgsrc+'operator/fractionLine.png'],
  110. //feedback options
  111. ['h_arrow', imgsrc+'help/arrow.png'],
  112. ['h_double', imgsrc+'help/double.png'],
  113. ['down', imgsrc+'help/down.png'],
  114. ['h_error', imgsrc+'help/error.png'],
  115. ['h_ok', imgsrc+'help/ok.png'],
  116. ['pointer', imgsrc+'help/pointer.png'],
  117. ];
  118. spritesheet = [
  119. // Game Sprites
  120. ['kid_walk', imgsrc+'character/kid/walk.png', 78, 175, 26],
  121. // Menu icons n the top of the page
  122. ['audio', imgsrc+'navIcon/audio_48x48.png',48, 48, 2],
  123. // feedback options
  124. ['select', imgsrc+'help/selectionBox.png', 310, 310, 2],
  125. ];
  126. audio = [
  127. // Sound effects
  128. ['sound_beep', ['assets/audio/beep.ogg', 'assets/audio/beep.mp3'] ],
  129. ['sound_error', ['assets/audio/error.ogg', 'assets/audio/error.mp3'] ],
  130. ['sound_ok', ['assets/audio/ok.ogg', 'assets/audio/ok.mp3'] ],
  131. ];
  132. return media.returnType(type);
  133. },
  134. gameSquareOne : function ( type ){
  135. image = [
  136. // scenario
  137. ['farm', imgsrc+'scenario/farm.png'],
  138. ['garage', imgsrc+'scenario/garage.png'],
  139. ];
  140. spritesheet = [
  141. // Game sprites
  142. ['tractor', imgsrc+'character/tractor/frame.png', 201, 144, 10]
  143. ];
  144. audio = [];
  145. return media.returnType(type);
  146. },
  147. gameSquareTwo : function ( type ){
  148. image = [
  149. // scenario
  150. ['house', imgsrc+'scenario/house.png'],
  151. ['school', imgsrc+'scenario/school.png'],
  152. ];
  153. spritesheet = [
  154. ['kid_lost', imgsrc+'character/kid/lost.png', 72, 170, 6],
  155. ['kid_run', imgsrc+'character/kid/run.png', 82, 178, 12],
  156. ];
  157. audio = [];
  158. return media.returnType(type);
  159. },
  160. gameCircleOne : function ( type ){
  161. image = [
  162. // scenario
  163. ['house', imgsrc+'scenario/house.png'],
  164. ['school', imgsrc+'scenario/school.png'],
  165. // Game Sprites
  166. ['balloon', imgsrc+'character/balloon/airballoon_upper.png'],
  167. ['balloon_basket', imgsrc+'character/balloon/airballoon_base.png'],
  168. ];
  169. spritesheet = [
  170. ['kid_run', imgsrc+'character/kid/run.png', 82, 178, 12],
  171. ];
  172. audio = [];
  173. return media.returnType(type);
  174. },
  175. menu : function (type) {
  176. image = [
  177. // level Icrons
  178. ['', imgsrc+'game/squareOne_1.png', 'Square', 'A'], // Square I
  179. ['', imgsrc+'game/squareOne_2.png', 'Square', 'B'], // Square II
  180. ['', imgsrc+'game/circleOne_1.png', 'Circle', 'A'], // Circle I
  181. ['', imgsrc+'game/circleOne_2.png', 'Circle', 'B'], // Circle II
  182. ['', imgsrc+'game/squareTwo_3.png', 'Square', 'C'] // Square III
  183. ];
  184. if (debugMode) {
  185. for (let i = 0; i < 8; i++) {
  186. image.push(['', imgsrc+'game/squareTwo_3.png', 'Square', 'C']);
  187. }
  188. }
  189. audio = [];
  190. spritesheet = [];
  191. return media.returnType(type);
  192. },
  193. returnType : function (type) {
  194. if (type == 'image') return image;
  195. else if (type == 'spritesheet') return spritesheet;
  196. else if (type == 'audio') return audio;
  197. },
  198. };
  199. // Control navigation icons on the top of the screen
  200. let iconSettings = {
  201. // Add navigation icons on the top of the screen based on parameters
  202. func_addIcons: function (left_side, right_side, // icon side
  203. left_btn0, left_btn1, left_btn2, // first 3 icon spaces
  204. right_btn0, right_btn1, // last 2 icon spaces
  205. level, helpBtn) { // auxiliar variables
  206. let left_x = 10;
  207. let right_x = defaultWidth - 50 - 10;
  208. // 'Descriptive labels' for the navigation icons
  209. if (left_side) this.left_text = game.add.text(left_x, 53, "", textStyles.overtitle);
  210. if (right_side) {
  211. this.right_text = game.add.text(right_x + 50, 53, "", textStyles.overtitle);
  212. this.right_text.anchor.setTo(1, 0.02);
  213. }
  214. // 'Icons' on the LEFT side of the page
  215. if (left_btn0) { // Return to select difficulty screen
  216. const icon_back = game.add.sprite(left_x, 10, 'back');
  217. icon_back.inputEnabled = true;
  218. icon_back.input.useHandCursor = true;
  219. icon_back.events.onInputDown.add(this.func_callState, { state: level });
  220. icon_back.events.onInputOver.add(function () { this.left_text.text = lang.menu_back }, { left_text: this.left_text });
  221. icon_back.events.onInputOut.add(function () { this.left_text.text = "" }, { left_text: this.left_text });
  222. // Offsets value of x for next icon
  223. left_x += 50;
  224. }
  225. if (left_btn1) { // Return to main menu screen
  226. const icon_list = game.add.sprite(left_x, 10, 'list');
  227. icon_list.inputEnabled = true;
  228. icon_list.input.useHandCursor = true;
  229. icon_list.events.onInputDown.add(this.func_callState, { state: "menu" });
  230. icon_list.events.onInputOver.add(function () { this.left_text.text = lang.menu_list }, { left_text: this.left_text });
  231. icon_list.events.onInputOut.add(function () { this.left_text.text = "" }, { left_text: this.left_text });
  232. // Offsets value of x for next icon
  233. left_x += 50;
  234. }
  235. if (left_btn2) { // In some levels, shows solution to the game
  236. const icon_help = game.add.sprite(left_x, 10, 'help');
  237. icon_help.inputEnabled = true;
  238. icon_help.input.useHandCursor = true;
  239. icon_help.events.onInputDown.add(helpBtn);
  240. icon_help.events.onInputOver.add(function () { this.left_text.text = lang.menu_help }, { left_text: this.left_text });
  241. icon_help.events.onInputOut.add(function () { this.left_text.text = "" }, { left_text: this.left_text });
  242. // Offsets value of x for next icon
  243. left_x += 50;
  244. }
  245. // 'Icons' on the RIGHT side of the page
  246. if (right_btn0) { // Turns game audio on/off
  247. this.icon_audio = game.add.sprite(right_x, 10, 'audio');
  248. audioStatus ? this.icon_audio.frame = 0 : this.icon_audio.frame = 1;
  249. this.icon_audio.inputEnabled = true;
  250. this.icon_audio.input.useHandCursor = true;
  251. this.icon_audio.events.onInputDown.add(function () { if (audioStatus) { audioStatus = false; this.icon_audio.frame = 1; } else { audioStatus = true; this.icon_audio.frame = 0; } }, { icon_audio: this.icon_audio });
  252. this.icon_audio.events.onInputOver.add(function () { this.right_text.text = lang.audio }, { right_text: this.right_text });
  253. this.icon_audio.events.onInputOut.add(function () { this.right_text.text = "" }, { right_text: this.right_text });
  254. // Offsets value of x for next icon
  255. right_x -= 50;
  256. }
  257. if (right_btn1) { // Return to select language screen
  258. this.icon_world = game.add.sprite(right_x, 10, 'world');
  259. this.icon_world.inputEnabled = true;
  260. this.icon_world.input.useHandCursor = true;
  261. this.icon_world.events.onInputDown.add(this.func_callState, { state: "language" });
  262. this.icon_world.events.onInputOver.add(function () { this.right_text.text = lang.menu_world }, { right_text: this.right_text });
  263. this.icon_world.events.onInputOut.add(function () { this.right_text.text = "" }, { right_text: this.right_text });
  264. // Offsets value of x for next icon
  265. right_x -= 50;
  266. }
  267. },
  268. // refresh values of x for icons when menu screen move sideways
  269. func_refreshRightIcons_x: function (newWidth) {
  270. this.right_text.x = newWidth - 10;
  271. this.icon_audio.x = newWidth - 50 - 10;
  272. this.icon_world.x = newWidth - 50 - 50 - 10;
  273. },
  274. func_callState: function () {
  275. if (audioStatus) beepSound.play();
  276. game.state.start(this.state);
  277. }
  278. };