preMenu_name.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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_green
  20. );
  21. this.warningEmptyName = game.add.text(
  22. context.canvas.width / 2,
  23. context.canvas.height / 2 - 80,
  24. '',
  25. textStyles.h4_brown
  26. );
  27. this.okBtn = game.add.image(
  28. context.canvas.width / 2,
  29. context.canvas.height / 2 + 93 + 44,
  30. 'button',
  31. 1.5
  32. );
  33. this.okBtn.anchor(0.5, 0.5);
  34. // Set button Text
  35. game.add.text(
  36. context.canvas.width / 2,
  37. context.canvas.height / 2 + 152, //112,
  38. game.lang.ready,
  39. textStyles.h1_white
  40. );
  41. // Makes text field visible
  42. document.querySelector('.ifr-input__container').style.visibility =
  43. 'visible';
  44. // Does the same as the button click when the player presses 'enter'
  45. document
  46. .querySelector('.ifr-input')
  47. .addEventListener('keypress', function (e) {
  48. const keycode = e.key || e.code;
  49. if (keycode == 'Enter') {
  50. if (self.checkEmptyName()) self.saveName();
  51. game.render.all(); // Can show empty name
  52. }
  53. });
  54. game.event.add('click', this.onInputDown);
  55. game.event.add('mousemove', this.onInputOver);
  56. // console.log('DEBUG');
  57. document.querySelector('.ifr-input').value = 'Laira';
  58. this.saveName();
  59. },
  60. /**
  61. * Checks if player entered name in text box
  62. *
  63. * @returns {boolean} false is textBox is emptys
  64. */
  65. checkEmptyName: function () {
  66. // If text field is empty displays error message
  67. if (document.querySelector('.ifr-input').value == '') {
  68. self.warningEmptyName.name = game.lang.empty_name;
  69. return false;
  70. }
  71. return true;
  72. },
  73. /**
  74. * Saves player name and calls next state
  75. */
  76. saveName: function () {
  77. // Saves player's input in global variable 'playerName'
  78. playerName = document.querySelector('.ifr-input').value;
  79. // Hides and clears text field
  80. document.querySelector('.ifr-input__container').style.visibility = 'hidden';
  81. document.querySelector('.ifr-input').value = '';
  82. if (audioStatus) game.audio.popSound.play();
  83. if (debugMode) console.log('Username: ' + playerName);
  84. // FOR MOODLE
  85. // Calls 'menu' state
  86. if (!moodle) game.state.start('menu');
  87. },
  88. /**
  89. * Called by mouse click event
  90. *
  91. * @param {object} mouseEvent contains the mouse click coordinates
  92. */
  93. onInputDown: function (mouseEvent) {
  94. const x = game.math.getMouse(mouseEvent).x;
  95. const y = game.math.getMouse(mouseEvent).y;
  96. const cur = self.okBtn;
  97. if (game.math.isOverIcon(x, y, cur)) {
  98. if (self.checkEmptyName()) {
  99. self.saveName();
  100. }
  101. }
  102. game.render.all();
  103. },
  104. /**
  105. * Called by mouse move event
  106. *
  107. * @param {object} mouseEvent contains the mouse move coordinates
  108. */
  109. onInputOver: function (mouseEvent) {
  110. const x = game.math.getMouse(mouseEvent).x;
  111. const y = game.math.getMouse(mouseEvent).y;
  112. const cur = self.okBtn;
  113. if (game.math.isOverIcon(x, y, cur)) {
  114. document.body.style.cursor = 'pointer';
  115. cur.alpha = 0.8;
  116. } else {
  117. document.body.style.cursor = 'auto';
  118. cur.alpha = 1;
  119. }
  120. game.render.all();
  121. },
  122. };