mainmenu.js 3.2 KB

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