preMenu.js 8.8 KB

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