mainmenu.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. include("mouseinput.js");
  2. include("curtain.js");
  3. include("button.js");
  4. function MainMenu(game, camera) {
  5. this.system = null;
  6. this.canvas = null;
  7. this.keyboard = null;
  8. this.mouse = null;
  9. this.curtain = null;
  10. this.playButton = null;
  11. this.textMetrics = null;
  12. this.showError = false;
  13. this.init = function(systemObject, canvas) {
  14. var stage = this;
  15. this.system = systemObject;
  16. this.canvas = canvas;
  17. this.mouse = new MouseInput();
  18. this.mouse.attach(canvas, this);
  19. this.curtain = new Curtain();
  20. this.curtain.init(systemObject, canvas);
  21. this.curtain.open(700, function() {
  22. stage.isStarted = true;
  23. });
  24. this.playButton = new Button(canvas);
  25. this.playButton.addClickEvent(this.joinGame);
  26. this.showError = false;
  27. this.handleResize(canvas);
  28. };
  29. this.handleResize = function(canvas) {
  30. var buttonOptions = {position: {x: this.canvas.width / 2, y: this.canvas.height / 2}, font:{color:"#DDDDDD", stroke: "#333333", size: 72, family: "sans-serif"}};
  31. if(this.canvas.width < 768) {
  32. buttonOptions.font.size = 36;
  33. }
  34. this.playButton.init("Join", buttonOptions);
  35. }
  36. this.end = function(callback) {
  37. this.mouse.detatch();
  38. this.closeCurtain(callback);
  39. }
  40. this.closeCurtain = function(callback) {
  41. system.theater.stage.curtain.close(700, function() {
  42. callback();
  43. });
  44. }
  45. this.joinGame = function() {
  46. system.theater.socketConnect(function() {
  47. system.theater.changeStage("world");
  48. },
  49. function() {
  50. system.theater.stage.showError = true;
  51. },
  52. function() {
  53. system.theater.changeStage("mainmenu");
  54. system.theater.playerSocket = null;
  55. });
  56. }
  57. this.updateDelta = function(delta, canvas) {
  58. if(system.keyboard.isPressed(Keys.Space) || system.keyboard.isPressed(Keys.Enter)) {
  59. this.joinGame();
  60. }
  61. this.curtain.updateDelta(delta);
  62. };
  63. this.draw = function(context) {
  64. var title = "Veridian Wander";
  65. context.font = "72px sans-serif";
  66. context.textAlign = "center";
  67. context.textBaseline = "middle";
  68. var textWidth = context.measureText(title).width;
  69. context.fillStyle = "#DDDDDD";
  70. context.strokeStyle = "#333333";
  71. context.lineWidth = 2;
  72. if(this.canvas.width < 768) {
  73. context.font = "36px sans-serif";
  74. }
  75. context.fillText(title, this.canvas.width /2 , 100);
  76. this.playButton.draw(context);
  77. if(this.showError) {
  78. var errorMessage = "Unable to connect to the server. Please try again later.";
  79. context.font = "16px sans-serif";
  80. context.textAlign = "center";
  81. context.textBaseline = "middle";
  82. var textWidth = context.measureText(errorMessage).width;
  83. context.fillStyle = "#DD3333";
  84. context.strokeStyle = "#DD3333";
  85. context.lineWidth = 2;
  86. if(this.canvas.width < 768) {
  87. context.font = "12px sans-serif";
  88. }
  89. context.fillText(errorMessage, this.canvas.width /2 , this.canvas.height - 32);
  90. }
  91. this.curtain.draw(context);
  92. };
  93. this.mouseMove = function(canvas, x, y) {
  94. this.playButton.mouseMove(x, y);
  95. }
  96. this.touchMove = function(canvas, x, y) {
  97. this.playButton.touchMove(x, y);
  98. }
  99. this.mouseDown = function(canvas, button, x, y) {
  100. this.playButton.mouseDown(button, x, y);
  101. }
  102. this.touchStart = function(canvas, x, y) {
  103. this.playButton.touchStart(x, y);
  104. };
  105. };