world_menu.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* main menu, with one start button */
  2. class WorldMenu extends World {
  3. /* assets that are available to this world */
  4. assets() {
  5. return [
  6. "images/button_start.png",
  7. "images/menu_background.png",
  8. "images/volume_yes.png",
  9. "images/volume_no.png",
  10. ];
  11. }
  12. /* assets are loaded, initialize interface */
  13. start() {
  14. /* array holding the buttons's sprites */
  15. this.buttons = new Object();
  16. /* start button */
  17. this.buttons["start"] = new PIXI.Sprite( loader.resources["images/button_start.png"].texture );
  18. this.buttons["start"].position.set( width/2 -this.buttons["start"].width/2,
  19. height -this.buttons["start"].height -20 );
  20. app.stage.addChild(this.buttons["start"]);
  21. /* mute button */
  22. this.buttons["mute"] = new PIXI.Sprite( loader.resources["images/volume_yes.png"].texture );
  23. this.buttons["mute"].position.set( 10, height -this.buttons["mute"].height -10 );
  24. this.update_mute_button();
  25. app.stage.addChild(this.buttons["mute"]);
  26. /* background image */
  27. this.bg = new PIXI.Sprite( loader.resources["images/menu_background.png"].texture );
  28. app.stage.addChildAt(this.bg, 0);
  29. /* title text */
  30. let style = new PIXI.TextStyle({
  31. fill: "#777777",
  32. stroke: "#210510",
  33. strokeThickness: 4,
  34. fontSize: 70,
  35. fontWeight: "bold",
  36. });
  37. this.text = new PIXI.Text("The Collector", style);
  38. this.text.position.set(width/2, 75);
  39. this.text.anchor = {x: 0.5, y: 0.5};
  40. app.stage.addChild(this.text);
  41. /* main theme, repeat endlessly */
  42. this.theme = new Audio("audio/collector_main.mp3");
  43. this.theme.addEventListener('ended', function() {
  44. this.currentTime = 0;
  45. this.play();
  46. }, false);
  47. if (!muted) {
  48. this.theme.play();
  49. }
  50. /* button sound */
  51. this.sound_button = new Audio("audio/button_click.mp3");
  52. /* closing animation */
  53. this.anim_close = new Animation(15);
  54. this.next_world = -1;
  55. /* rectangle to fade out animation */
  56. this.fade = new PIXI.Graphics();
  57. this.fade.beginFill(0x000000);
  58. this.fade.drawRect(0, 0, width, height);
  59. this.fade.endFill();
  60. this.fade.alpha = 1;
  61. app.stage.addChild(this.fade);
  62. this.anim_close.start();
  63. } /* start */
  64. /* called once per frame */
  65. update() {
  66. /* closing animation is running */
  67. if (this.anim_close.update()) {
  68. /* animation ended */
  69. if (!this.anim_close.is_running()) {
  70. /* was closing animation */
  71. if (this.anim_close.timer == 0) {
  72. /* stop theme song */
  73. this.theme.pause();
  74. /* return new world to be opened */
  75. return this.next_world;
  76. }
  77. /* was opening animation */
  78. else {
  79. /* hide rectangle */
  80. this.fade.visible = false;
  81. /* update nothing */
  82. return -1;
  83. }
  84. } /* animation end */
  85. /* make volume fade out and black screen fade in */
  86. this.theme.volume = this.anim_close.interpolate(1);
  87. this.fade.alpha = 1 -this.anim_close.interpolate(1);
  88. /* while world is closing, input and updating are both ignored */
  89. return -1;
  90. } /* closing animation */
  91. /* handle input */
  92. for (let i = 0; i < input.length; i++) {
  93. /* left button was clicked */
  94. if (input[i].which == 1) {
  95. /* point of click */
  96. let click = new PIXI.Point(input[i].offsetX, input[i].offsetY);
  97. /* start button was clicked */
  98. if (this.buttons["start"].containsPoint(click)) {
  99. /* make sound */
  100. if (!muted) {
  101. this.sound_button.play();
  102. }
  103. /* start closing animation and save WorldGame as next world */
  104. this.anim_close.start();
  105. this.fade.visible = true;
  106. this.next_world = world_list.indexOf(WorldGame);
  107. /* every input can only trigger one button */
  108. continue;
  109. } /* start button clicked */
  110. /* mute button clicked - toggle mute */
  111. if (this.buttons["mute"].containsPoint(click)){
  112. if (muted) {
  113. this.theme.play();
  114. muted = false;
  115. }
  116. else {
  117. this.theme.pause();
  118. muted = true;
  119. }
  120. /* update mute button graphic */
  121. this.update_mute_button();
  122. /* each input can only trigger one button */
  123. continue;
  124. } /* mute button clicked */
  125. } /* left click event */
  126. } /* for every input */
  127. } /* update */
  128. /* changes appearence of mute button based on if the game
  129. * is muted or not
  130. */
  131. update_mute_button() {
  132. if (muted) {
  133. this.buttons["mute"].texture =
  134. loader.resources["images/volume_no.png"].texture;
  135. }
  136. else {
  137. this.buttons["mute"].texture =
  138. loader.resources["images/volume_yes.png"].texture;
  139. }
  140. } /* update mute button */
  141. }