preMenu_name.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /******************************
  2. * This file holds game states.
  3. ******************************/
  4. /** [NAME STATE] Screen that asks for the user's name.
  5. *
  6. * @namespace
  7. */
  8. const nameState = {
  9. /**
  10. * Main code
  11. */
  12. create: function () {
  13. renderBackground('plain');
  14. // Set title and warning text
  15. game.add.text(
  16. context.canvas.width / 2,
  17. context.canvas.height / 2 - 150,
  18. game.lang.insert_name,
  19. { ...textStyles.h1_, fill: colors.green }
  20. );
  21. this.warningEmptyName = game.add.text(
  22. context.canvas.width / 2,
  23. context.canvas.height / 2 - 80,
  24. '',
  25. { ...textStyles.p_, fill: colors.red }
  26. );
  27. this.okBtn = game.add.geom.rect(
  28. context.canvas.width / 2,
  29. context.canvas.height / 2 + 93 + 44,
  30. 300,
  31. 100,
  32. colors.green
  33. );
  34. this.okBtn.anchor(0.5, 0.5);
  35. // Set button Text
  36. this.okBtnText = game.add.text(
  37. context.canvas.width / 2,
  38. context.canvas.height / 2 + 152, //112,
  39. game.lang.ready,
  40. textStyles.btn
  41. );
  42. // Makes text field visible
  43. document.querySelector('.ifr-input__container').style.visibility =
  44. 'visible';
  45. // Does the same as the button click when the player presses 'enter'
  46. document
  47. .querySelector('.ifr-input')
  48. .addEventListener('keypress', function (e) {
  49. const keycode = e.key || e.code;
  50. if (keycode == 'Enter') {
  51. if (self.checkEmptyName()) self.saveName();
  52. game.render.all(); // Can show empty name
  53. }
  54. });
  55. game.event.add('click', this.onInputDown);
  56. game.event.add('mousemove', this.onInputOver);
  57. if (isDebugMode && debugState.name.skip) {
  58. // programmatically select a user name
  59. document.querySelector('.ifr-input').value =
  60. debugState.name.name || 'My User Name';
  61. this.saveName();
  62. }
  63. },
  64. /**
  65. * Checks if player entered name in text box
  66. *
  67. * @returns {boolean} false is textBox is emptys
  68. */
  69. checkEmptyName: function () {
  70. // If text field is empty displays error message
  71. if (document.querySelector('.ifr-input').value == '') {
  72. self.warningEmptyName.name = game.lang.empty_name;
  73. return false;
  74. }
  75. return true;
  76. },
  77. /**
  78. * Saves player name and calls next state
  79. */
  80. saveName: function () {
  81. // Saves player's input in global variable 'playerName'
  82. playerName = document.querySelector('.ifr-input').value;
  83. // Hides and clears text field
  84. document.querySelector('.ifr-input__container').style.visibility = 'hidden';
  85. document.querySelector('.ifr-input').value = '';
  86. if (audioStatus) game.audio.popSound.play();
  87. if (isDebugMode) console.log('Username: ' + playerName);
  88. // FOR MOODLE
  89. // Calls 'menu' state
  90. if (!moodle) game.state.start('menu');
  91. },
  92. /**
  93. * Called by mouse click event
  94. *
  95. * @param {object} mouseEvent contains the mouse click coordinates
  96. */
  97. onInputDown: function (mouseEvent) {
  98. const x = game.math.getMouse(mouseEvent).x;
  99. const y = game.math.getMouse(mouseEvent).y;
  100. const cur = self.okBtn;
  101. if (game.math.isOverIcon(x, y, cur)) {
  102. if (self.checkEmptyName()) {
  103. self.saveName();
  104. }
  105. }
  106. game.render.all();
  107. },
  108. /**
  109. * Called by mouse move event
  110. *
  111. * @param {object} mouseEvent contains the mouse move coordinates
  112. */
  113. onInputOver: function (mouseEvent) {
  114. const x = game.math.getMouse(mouseEvent).x;
  115. const y = game.math.getMouse(mouseEvent).y;
  116. const cur = self.okBtn;
  117. if (game.math.isOverIcon(x, y, cur)) {
  118. document.body.style.cursor = 'pointer';
  119. cur.scale = 1.1;
  120. self.okBtnText.style = textStyles.btnLg;
  121. } else {
  122. document.body.style.cursor = 'auto';
  123. cur.scale = 1;
  124. self.okBtnText.style = textStyles.btn;
  125. }
  126. game.render.all();
  127. },
  128. };