MainMenu.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. require('MouseInput');
  2. require('Curtain');
  3. class MainMenu {
  4. constructor(game, camera) {
  5. this.system = null;
  6. this.canvas = null;
  7. this.keyboard = null;
  8. this.mouse = null;
  9. this.curtain = null;
  10. this.buttonText = "Enter";
  11. this.textMetrics = null;
  12. this.isHovered = false;
  13. }
  14. init(systemObject, canvas) {
  15. var stage = this;
  16. this.system = systemObject;
  17. this.canvas = canvas;
  18. this.mouse = new MouseInput();
  19. this.mouse.attach(canvas, this);
  20. this.curtain = new Curtain();
  21. this.curtain.init(systemObject, canvas);
  22. this.curtain.open(700, function () {
  23. stage.isStarted = true;
  24. });
  25. }
  26. end() {
  27. this.mouse.detach();
  28. }
  29. updateDelta(delta, canvas) {
  30. if (this.system.keyboard.isDown(Keys.Space) || this.system.keyboard.isDown(Keys.Enter)) {
  31. this.transitionToWorld();
  32. }
  33. this.curtain.updateDelta(delta);
  34. }
  35. transitionToWorld() {
  36. this.curtain.close(1000, function () {
  37. this.system.theater.changeStage("world");
  38. this.canvas.style.cursor = "auto";
  39. });
  40. this.isHovered = false;
  41. }
  42. draw(context) {
  43. this.canvas.style.cursor = "auto";
  44. if (this.isHovered) {
  45. this.canvas.style.cursor = "pointer";
  46. }
  47. var title = "Back";
  48. context.font = "72px sans-serif";
  49. context.textAlign = "center";
  50. context.textBaseline = "middle";
  51. var textWidth = context.measureText(title).width;
  52. context.fillStyle = "#DDDDDD";
  53. context.strokeStyle = "#333333";
  54. context.lineWidth = 2;
  55. context.fillText(title, this.canvas.width / 2, 100);
  56. this.textMetrics = context.measureText(this.buttonText);
  57. var textWidth = this.textMetrics.width;
  58. context.save();
  59. context.translate(this.canvas.width / 2, this.canvas.height / 2);
  60. var message = "Play";
  61. context.font = "72px sans-serif";
  62. context.textAlign = "center";
  63. context.textBaseline = "middle";
  64. if (this.isHovered) {
  65. context.fillStyle = "#333333";
  66. } else {
  67. context.fillStyle = "#777777";
  68. }
  69. context.beginPath();
  70. context.rect(-(textWidth / 2) - 10, -72 / 2, textWidth + 20, 82);
  71. context.fill();
  72. context.fillStyle = "#DDDDDD";
  73. context.strokeStyle = "#333333";
  74. context.lineWidth = 2;
  75. context.fillText(message, 0, 0);
  76. context.restore();
  77. this.curtain.draw(context);
  78. }
  79. mouseMove(canvas, x, y) {
  80. this.isHovered = false;
  81. var topLeft = {};
  82. topLeft.x = (this.canvas.width / 2) - (this.textMetrics.width / 2) - 10;
  83. topLeft.y = (this.canvas.height / 2) - (72 / 2);
  84. var bottomRight = {};
  85. bottomRight.x = (this.canvas.width / 2) + this.textMetrics.width + 20;
  86. bottomRight.y = (this.canvas.height / 2) + 72;
  87. if (x > topLeft.x && x < bottomRight.x) {
  88. if (y > topLeft.y && y < bottomRight.y) {
  89. this.isHovered = true;
  90. }
  91. }
  92. }
  93. touchMove(canvas, x, y) {
  94. this.isHovered = false;
  95. var topLeft = {};
  96. topLeft.x = (this.canvas.width / 2) - (this.textMetrics.width / 2) - 10;
  97. topLeft.y = (this.canvas.height / 2) - (72 / 2);
  98. var bottomRight = {};
  99. bottomRight.x = (this.canvas.width / 2) + (this.textMetrics.width + 20) / 2;
  100. bottomRight.y = (this.canvas.height / 2) + (72 / 2);
  101. if (x > topLeft.x && x < bottomRight.x) {
  102. if (y > topLeft.y && y < bottomRight.y) {
  103. this.isHovered = true;
  104. }
  105. }
  106. }
  107. mouseDown(canvas, button, x, y) {
  108. if (this.isHovered) {
  109. this.transitionToWorld();
  110. }
  111. }
  112. touchStart(canvas, x, y) {
  113. if (this.isHovered) {
  114. this.transitionToWorld();
  115. }
  116. }
  117. };