preMenu.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. // Only called once
  2. const boot = {
  3. preload: function () {
  4. info.start();
  5. document.body.style.cursor = "auto";
  6. game.loop.stop();
  7. game.event.clear();
  8. game.animation.clear();
  9. self = this;
  10. // LOADING MEDIA
  11. game.load.audio(url.boot.audio);
  12. game.load.image(url.boot.image);
  13. game.load.sprite(url.boot.sprite);
  14. },
  15. create: function () {
  16. // Calls first screen seen by the player
  17. langScreen.preload();
  18. }
  19. };
  20. // LANGUAGE SCREEN: the player can choose a preferred language for the game text to be displayed
  21. const langScreen = {
  22. preload: function () {
  23. document.body.style.cursor = "auto";
  24. game.loop.stop();
  25. game.event.clear();
  26. game.animation.clear();
  27. self = this;
  28. // NOTHING TO LOAD HERE
  29. langScreen.create();
  30. },
  31. create: function () {
  32. game.render.clear();
  33. // Background color
  34. game.add.graphic.rect(0, 0, 900, 600, colors.white, 0, colors.blueBckg, 1);
  35. // Parameters for the elements on the screen
  36. langScreen.listOfFlags = [];
  37. langScreen.langs = {
  38. text: ['FRAÇÕES ', 'FRAZIONI ', 'FRACTIONS ', 'FRACCIONES ', 'FRACTIONS '], // Language names
  39. flag: ['flag_BR', 'flag_IT', 'flag_US', 'flag_PE', 'flag_FR'], // Icon names
  40. lang: ['pt_BR', 'it_IT', 'en_US', 'es_PE', 'fr_FR'], // Parameters sent for language object
  41. x: [-220, -220, -220, 200, 200],
  42. y: [-180, 0, 180, -100, 100]
  43. };
  44. // Create elements on screen
  45. for (let i in this.langs.flag) {
  46. // Add text for language names
  47. game.add.text(defaultWidth / 2 + this.langs.x[i], defaultHeight / 2 + this.langs.y[i], this.langs.text[i], textStyles.h2_green, 'right');
  48. // Add icons for flags
  49. const flag = game.add.image(defaultWidth / 2 + this.langs.x[i] + 100, defaultHeight / 2 + this.langs.y[i], this.langs.flag[i]);
  50. flag.anchor(0.5, 0.5);
  51. this.listOfFlags.push(flag);
  52. }
  53. game.event.add("click", this.func_onInputDown);
  54. game.event.add("mousemove", this.func_onInputOver);
  55. game.render.all();
  56. },
  57. /* EVENT HANDLER*/
  58. func_onInputDown: function (mouseEvent) {
  59. const x = mouseEvent.offsetX;
  60. const y = mouseEvent.offsetY;
  61. langScreen.listOfFlags.forEach(cur => {
  62. const valid = y >= cur.yWithAnchor && y <= (cur.yWithAnchor + cur.height * cur.scale) &&
  63. (x >= cur.xWithAnchor && x <= (cur.xWithAnchor + cur.width * cur.scale));
  64. if (valid) {
  65. for (let i in langScreen.langs.flag) {
  66. if (langScreen.langs.flag[i] == cur.name) {
  67. langScreen.func_setLang(self.langs.lang[i]);
  68. }
  69. }
  70. }
  71. });
  72. },
  73. func_onInputOver: function (mouseEvent) {
  74. const x = mouseEvent.offsetX;
  75. const y = mouseEvent.offsetY;
  76. let flag = false;
  77. langScreen.listOfFlags.forEach(cur => {
  78. const valid = y >= cur.yWithAnchor && y <= (cur.yWithAnchor + cur.height * cur.scale) &&
  79. (x >= cur.xWithAnchor && x <= (cur.xWithAnchor + cur.width * cur.scale));
  80. if (valid) {
  81. flag = true;
  82. cur.scale = cur.scale = 1.05;
  83. } else {
  84. cur.scale = cur.scale = 1;
  85. }
  86. });
  87. if (flag) document.body.style.cursor = "pointer";
  88. else document.body.style.cursor = "auto";
  89. game.render.all();
  90. },
  91. /* GAME FUNCTIONS */
  92. // Calls loading screen while loads language
  93. func_setLang: function (selectedLang) {
  94. // Saves language name e.g 'pt_BR'
  95. langString = selectedLang;
  96. // Calls loading screen
  97. loadLang.preload();
  98. }
  99. };
  100. // Loads selected language to be able to translate the game text
  101. const loadLang = {
  102. preload: function () {
  103. game.loop.stop();
  104. game.event.clear();
  105. game.animation.clear();
  106. self = this;
  107. // LOADING MEDIA : selected language
  108. game.load.lang('assets/lang/' + langString);
  109. },
  110. create: function () {
  111. if (debugMode) console.log("Language: " + langString);
  112. // Make sure to only ask for player name on the first time oppening the game
  113. if (this.firstTime == undefined) {
  114. this.firstTime = false;
  115. nameScreen.preload(); // first time opening ifractions ('language' >> 'name' >> 'menu')
  116. } else {
  117. menuScreen.preload(); // if changing language during the game ('language' >>>> 'menu')
  118. }
  119. }
  120. };
  121. // NAME SCREEN: asks for player's name
  122. const nameScreen = {
  123. preload: function () {
  124. document.body.style.cursor = "auto";
  125. game.loop.stop();
  126. game.event.clear();
  127. game.animation.clear();
  128. self = this;
  129. // NOTHING TO LOAD HERE
  130. nameScreen.create();
  131. },
  132. create: function () {
  133. game.render.clear();
  134. // Background color
  135. game.add.graphic.rect(0, 0, 900, 600, colors.white, 0, colors.blueBckg, 1);
  136. // Set title and warning text
  137. game.add.text(defaultWidth / 2, defaultHeight / 2 - 100, game.lang.insert_name, textStyles.h1_green);
  138. this.warningEmptyName = game.add.text(defaultWidth / 2, defaultHeight / 2 - 70, "", textStyles.h4_brown);
  139. // Set 'ok' button that gets player's information
  140. this.okBtn = game.add.graphic.rect(defaultWidth / 2 - 84, defaultHeight / 2 + 70, 168, 60, undefined, 0, colors.gray, 0.6);
  141. // Set button Text
  142. game.add.text(defaultWidth / 2 + 1, defaultHeight / 2 + 112, game.lang.ready, textStyles.h1_white);
  143. // Makes text field visible
  144. document.getElementById("textbox").style.visibility = "visible";
  145. // Does the same as the button click when the player presses "enter"
  146. document.getElementById("textbox-content").addEventListener('keypress', function (e) {
  147. const keycode = e.key || e.code;
  148. if (keycode == 'Enter') {
  149. if (self.func_checkEmptyName()) self.func_saveName();
  150. game.render.all(); // can show empty name
  151. }
  152. });
  153. game.event.add("click", this.func_onInputDown);
  154. game.event.add("mousemove", this.func_onInputOver);
  155. game.render.all();
  156. },
  157. /* EVENT HANDLER*/
  158. func_onInputDown: function (mouseEvent) {
  159. const x = mouseEvent.offsetX;
  160. const y = mouseEvent.offsetY;
  161. const cur = self.okBtn;
  162. const valid = y >= cur.yWithAnchor && y <= (cur.yWithAnchor + cur.height * cur.scale) &&
  163. (x >= cur.xWithAnchor && x <= (cur.xWithAnchor + cur.width * cur.scale));
  164. if (valid) {
  165. if (self.func_checkEmptyName()) {
  166. self.func_saveName();
  167. }
  168. }
  169. game.render.all();
  170. },
  171. func_onInputOver: function (mouseEvent) {
  172. const x = mouseEvent.offsetX;
  173. const y = mouseEvent.offsetY;
  174. const cur = self.okBtn;
  175. const valid = y >= cur.yWithAnchor && y <= (cur.yWithAnchor + cur.height * cur.scale) &&
  176. (x >= cur.xWithAnchor && x <= (cur.xWithAnchor + cur.width * cur.scale));
  177. if (valid) {
  178. document.body.style.cursor = "pointer";
  179. cur.alpha = 0.4;
  180. } else {
  181. document.body.style.cursor = "auto";
  182. cur.alpha = 0.6;
  183. }
  184. game.render.all();
  185. },
  186. /* GAME FUNCTIONS */
  187. func_checkEmptyName: function () {
  188. // If text field is empty displays error message
  189. if (document.getElementById("textbox-content").value == "") {
  190. self.warningEmptyName.name = game.lang.empty_name;
  191. return false;
  192. }
  193. return true;
  194. },
  195. func_saveName: function () {
  196. // Saves player's input in global variable 'playerName'
  197. playerName = document.getElementById("textbox-content").value;
  198. // Hides and clears text field
  199. document.getElementById("textbox").style.visibility = "hidden";
  200. document.getElementById("textbox-content").value = "";
  201. if (audioStatus) game.audio.beepSound.play();
  202. if (debugMode) console.log("Username: " + playerName);
  203. // Calls 'menu' state
  204. menuScreen.preload();
  205. }
  206. };