scene-ui.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import { scene } from "./scene.js";
  2. import { push } from "./push.js";
  3. import { material_icon } from "./icons.js";
  4. import { container } from "./container.js";
  5. import { moving_directions } from "./directions.js";
  6. import { rotating_directions } from "./directions.js";
  7. import { is_moving_direction } from "./directions.js";
  8. import { is_rotating_direction } from "./directions.js";
  9. import { position } from "./position.js";
  10. class scene_ui {
  11. #box;
  12. #worker;
  13. #last_mouse_position;
  14. #step_left;
  15. #step_right;
  16. #step_front;
  17. #step_back;
  18. #rotate_clockwise;
  19. #rotate_countclockwise;
  20. constructor(worker) {
  21. if (!(worker instanceof scene)) {
  22. throw new TypeError("Worker must be instance of scene.");
  23. }
  24. this.#last_mouse_position = undefined;
  25. this.#worker = worker;
  26. this.#step_front = this.#create_push(
  27. "front", "arrow_drop_up", moving_directions.front
  28. );
  29. this.#step_back = this.#create_push(
  30. "back", "arrow_drop_down", moving_directions.back
  31. );
  32. this.#step_left = this.#create_push(
  33. "left", "arrow_left", moving_directions.left
  34. );
  35. this.#step_right = this.#create_push(
  36. "right", "arrow_right", moving_directions.right
  37. );
  38. this.#rotate_clockwise = this.#create_push(
  39. "clockwise", "rotate_right",
  40. rotating_directions.clockwise
  41. );
  42. this.#rotate_countclockwise = this.#create_push(
  43. "countclockwise", "rotate_left",
  44. rotating_directions.counterclockwise
  45. );
  46. this.#setup_stopers();
  47. this.#setup_keybind();
  48. this.#setup_mousebind();
  49. this.#box = container("controls", (root) => {
  50. root.appendChild(container("top", (top) => {
  51. top.appendChild(this.#rotate_countclockwise);
  52. top.appendChild(this.#step_front);
  53. top.appendChild(this.#rotate_clockwise);
  54. }));
  55. root.appendChild(container("bottom", (bottom) => {
  56. bottom.appendChild(this.#step_left);
  57. bottom.appendChild(this.#step_back);
  58. bottom.appendChild(this.#step_right);
  59. }));
  60. });
  61. }
  62. get box() {
  63. return this.#box;
  64. }
  65. #setup_keybind() {
  66. document.addEventListener("keydown", (action) => {
  67. switch (action.key) {
  68. case "w":
  69. this.#worker.player.moving = moving_directions.front;
  70. break;
  71. case "s":
  72. this.#worker.player.moving = moving_directions.back;
  73. break;
  74. case "d":
  75. this.#worker.player.moving = moving_directions.right;
  76. break;
  77. case "a":
  78. this.#worker.player.moving = moving_directions.left;
  79. break;
  80. default:
  81. break;
  82. }
  83. });
  84. document.addEventListener("keyup", (action) => {
  85. switch (action.key) {
  86. case "w":
  87. case "s":
  88. case "a":
  89. case "d":
  90. this.#worker.player.moving = moving_directions.stop;
  91. break;
  92. default:
  93. break;
  94. }
  95. });
  96. }
  97. #setup_mousebind() {
  98. document.addEventListener("mouseout", (action) => {
  99. this.#last_mouse_position = undefined;
  100. });
  101. document.addEventListener("mousemove", (action) => {
  102. const from_mouse = () => {
  103. return new position(action.clientX, action.clientY);
  104. };
  105. if (this.#last_mouse_position === undefined) {
  106. this.#last_mouse_position = from_mouse();
  107. return;
  108. }
  109. const current = from_mouse();
  110. const difference = current.compare(this.#last_mouse_position);
  111. const size = new position(window.innerWidth, window.innerHeight);
  112. this.#last_mouse_position = current;
  113. this.#worker.player.rotate_cursor(difference, size);
  114. });
  115. }
  116. #setup_stopers() {
  117. const stoper = () => {
  118. this.#worker.player.moving = moving_directions.stop;
  119. this.#worker.player.rotating = rotating_directions.stop;
  120. };
  121. document.addEventListener("mouseup", stoper);
  122. document.addEventListener("mouseover", stoper);
  123. }
  124. #create_push(name, icon, move) {
  125. if (!is_moving_direction(move) && !is_rotating_direction(move)) {
  126. throw new TypeError("Move muse be an direction.");
  127. }
  128. return push(name, null, (target) => {
  129. target.innerText = "";
  130. target.appendChild(material_icon(icon));
  131. if (is_moving_direction(move)) {
  132. target.addEventListener("mousedown", () => {
  133. this.#worker.player.moving = move;
  134. });
  135. } else if (is_rotating_direction(move)) {
  136. target.addEventListener("mousedown", () => {
  137. this.#worker.player.rotating = move;
  138. });
  139. }
  140. });
  141. }
  142. }
  143. export { scene_ui };