LevelSelectState.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import { Button } from "../../libraries/components/Button.js";
  2. import { Point } from "../../libraries/spatial/Point.js";
  3. export class LevelSelectState {
  4. constructor(fsm) {
  5. this.stateMachine = fsm;
  6. this.boundMouseEvent = {};
  7. this.levelData = [];
  8. this.playerData = {};
  9. this.levelButtons = [];
  10. this.offsetX = 0;
  11. this.page = 0;
  12. this.maxPage = 0;
  13. this.canvasBounds = null;
  14. }
  15. init() {
  16. this.boundMouseEvent['mousedown'] = this.mouseDown.bind(this);
  17. this.boundMouseEvent['mousemove'] = this.mouseMove.bind(this);
  18. for(let key in this.boundMouseEvent) {
  19. window.addEventListener(key, this.boundMouseEvent[key]);
  20. }
  21. this.levelButtons = [];
  22. for(let i = 0; i <= this.levelData.length; i++) {
  23. let buttonText = i+1;
  24. if(i == this.levelData.length) {
  25. buttonText = "%";
  26. }
  27. let button = new Button(0,0, 50, 50, buttonText, {fontSize: 18, textStrokeStyle: null});
  28. button.hoverFunction((ctx) => {
  29. if(this.playerData.level > i+1) {
  30. ctx.fillStyle = "rgba(49, 141, 79, 0.8)";
  31. } else if(this.playerData.level < i+1) {
  32. ctx.fillStyle = "rgba(224, 104, 78, 0.8)";
  33. } else {
  34. ctx.fillStyle = "rgba(255,255,255,0.8)";
  35. }
  36. });
  37. button.backgroundDrawFunction((ctx, scaledCanvas) => {
  38. if(this.playerData.level > i+1) {
  39. ctx.fillStyle = "rgba(49, 141, 79, 0.5)";
  40. } else if(this.playerData.level < i+1) {
  41. ctx.fillStyle = "rgba(224, 104, 78, 0.5)";
  42. } else {
  43. ctx.fillStyle = "rgba(255,255,255,0.5)";
  44. }
  45. ctx.beginPath();
  46. ctx.rect(-button.bounds.width / 2, -button.bounds.height / 2, button.bounds.width, button.bounds.height);
  47. ctx.closePath();
  48. if(button.isHovered) {
  49. document.body.style.cursor = "pointer";
  50. button.whenHovered(ctx, scaledCanvas);
  51. }
  52. ctx.fill();
  53. });
  54. button.clickFunction((event) => {
  55. if(this.playerData.level >= i+1) {
  56. this.playerData.current = i+1;
  57. this.stateMachine.transitionTo("game");
  58. }
  59. })
  60. this.levelButtons.push(button);
  61. }
  62. this.nextPageButton = new Button(0, 0, 100, 50, "next", {fontSize: 28, textStrokeStyle: null});
  63. this.nextPageButton.clickFunction((event) => {
  64. this.page++;
  65. this.page = Math.min(this.page, this.maxPage);
  66. });
  67. this.nextPageButton.hoverFunction((ctx) => {
  68. ctx.fillStyle = "rgba(255,255,255,0.8)";
  69. });
  70. this.prevPageButton = new Button(0, 0, 100, 50, "prev",{fontSize: 28, textStrokeStyle: null});
  71. this.prevPageButton.clickFunction((event) => {
  72. this.page--;
  73. this.page = Math.max(0, this.page);
  74. })
  75. this.prevPageButton.hoverFunction((ctx) => {
  76. ctx.fillStyle = "rgba(255,255,255,0.8)";
  77. });
  78. }
  79. draw(ctx, scaledCanvas) {
  80. ctx.fillStyle = "white";
  81. ctx.font = "48px Luckiest Guy";
  82. ctx.textAlign = "center";
  83. ctx.lineWidth = 4;
  84. this.canvasBounds = scaledCanvas.bounds;
  85. let center = this.canvasBounds.center;
  86. ctx.fillText(`Level Select`, center.x, 48);
  87. ctx.strokeText(`Level Select`, center.x, 48);
  88. let levelGridWidth = this.canvasBounds.width - 40;
  89. let numberOfLevelButtonsCols = Math.floor(levelGridWidth / 70);
  90. let numberOfLevelButtonsRows = Math.floor(this.levelButtons.length / numberOfLevelButtonsCols)
  91. let maxRowsInHeight = Math.max(1, Math.floor((this.canvasBounds.height - 200) / 70))
  92. let numberOfPages = Math.max(1, Math.floor(numberOfLevelButtonsRows / maxRowsInHeight));
  93. this.maxPage = numberOfPages;
  94. this.offsetX = (levelGridWidth - ((numberOfLevelButtonsCols-1) * 70) + 50) / 2;
  95. let min = this.page * (numberOfLevelButtonsCols * maxRowsInHeight)
  96. let max = Math.min(this.levelButtons.length, (this.page + 1) * (numberOfLevelButtonsCols * maxRowsInHeight));
  97. for(let i = min; i < max; i++) {
  98. let button = this.levelButtons[i];
  99. let buttonPosition = new Point((i % numberOfLevelButtonsCols) * 70 + this.offsetX, Math.floor((i / numberOfLevelButtonsCols)) * 70 + 100 - (this.page * maxRowsInHeight * 70));
  100. if(buttonPosition.y > Math.max(100,this.canvasBounds.height - 100)) {
  101. break;
  102. }
  103. button.setPosition(buttonPosition.x, buttonPosition.y);
  104. button.draw(ctx, scaledCanvas);
  105. }
  106. this.nextPageButton.setPosition(center.x + 105, Math.max(170, this.canvasBounds.height - 100));
  107. this.nextPageButton.draw(ctx, scaledCanvas);
  108. this.prevPageButton.setPosition(center.x - 105, Math.max(170, this.canvasBounds.height - 100));
  109. this.prevPageButton.draw(ctx, scaledCanvas);
  110. }
  111. setLevelData(data) {
  112. this.levelData = data;
  113. }
  114. setPlayerData(data) {
  115. this.playerData = data;
  116. }
  117. update(delta) {
  118. }
  119. mouseDown(event) {
  120. let levelGridWidth = this.canvasBounds.width - 40;
  121. let numberOfLevelButtonsCols = Math.floor(levelGridWidth / 70);
  122. let maxRowsInHeight = Math.max(1, Math.floor((this.canvasBounds.height - 200) / 70))
  123. let min = this.page * (numberOfLevelButtonsCols * maxRowsInHeight)
  124. let max = Math.min(this.levelButtons.length, (this.page + 1) * (numberOfLevelButtonsCols * maxRowsInHeight));
  125. for(let i = min; i < max; i++) {
  126. let button = this.levelButtons[i];
  127. button.mouseDown(event);
  128. }
  129. this.nextPageButton.mouseDown(event);
  130. this.prevPageButton.mouseDown(event);
  131. }
  132. mouseMove(event) {
  133. document.body.style.cursor = "default";
  134. let levelGridWidth = this.canvasBounds.width - 40;
  135. let numberOfLevelButtonsCols = Math.floor(levelGridWidth / 70);
  136. let maxRowsInHeight = Math.max(1, Math.floor((this.canvasBounds.height - 200) / 70))
  137. let min = this.page * (numberOfLevelButtonsCols * maxRowsInHeight)
  138. let max = Math.min(this.levelButtons.length, (this.page + 1) * (numberOfLevelButtonsCols * maxRowsInHeight));
  139. for(let i = min; i < max; i++) {
  140. let button = this.levelButtons[i];
  141. button.mouseMove(event);
  142. }
  143. this.nextPageButton.mouseMove(event);
  144. this.prevPageButton.mouseMove(event);
  145. }
  146. enter() {
  147. this.init();
  148. }
  149. leave() {
  150. for(let key in this.boundMouseEvent) {
  151. window.removeEventListener(key, this.boundMouseEvent[key]);
  152. }
  153. }
  154. }