scene-ui.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 { position } from "./position.js";
  6. class scene_ui {
  7. #box;
  8. #worker;
  9. #last_mouse_position;
  10. #step_left;
  11. #step_right;
  12. #step_front;
  13. #step_back;
  14. #rotate_clockwise;
  15. #rotate_countclockwise;
  16. constructor(worker) {
  17. if (!(worker instanceof scene)) {
  18. throw new TypeError("Worker must be instance of scene.");
  19. }
  20. this.#last_mouse_position = undefined;
  21. this.#worker = worker;
  22. this.#step_front = this.#create_push(
  23. "front", "arrow_drop_up",
  24. (player) => { player.movement.add_front(); }
  25. );
  26. this.#step_back = this.#create_push(
  27. "back", "arrow_drop_down",
  28. (player) => { player.movement.add_back(); }
  29. );
  30. this.#step_left = this.#create_push(
  31. "left", "arrow_left",
  32. (player) => { player.movement.add_left(); }
  33. );
  34. this.#step_right = this.#create_push(
  35. "right", "arrow_right",
  36. (player) => { player.movement.add_right(); }
  37. );
  38. this.#rotate_clockwise = this.#create_push(
  39. "clockwise", "rotate_right",
  40. (player) => { player.rotation.add_right(); }
  41. );
  42. this.#rotate_countclockwise = this.#create_push(
  43. "countclockwise", "rotate_left",
  44. (player) => { player.rotation.add_left(); }
  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.movement.add_front();
  70. break;
  71. case "s":
  72. this.#worker.player.movement.add_back();
  73. break;
  74. case "d":
  75. this.#worker.player.movement.add_right();
  76. break;
  77. case "a":
  78. this.#worker.player.movement.add_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. this.#worker.player.movement.stop_front_back();
  89. break;
  90. case "a":
  91. case "d":
  92. this.#worker.player.movement.stop_left_right();
  93. break;
  94. default:
  95. break;
  96. }
  97. });
  98. }
  99. #setup_mousebind() {
  100. document.addEventListener("mouseout", (action) => {
  101. this.#last_mouse_position = undefined;
  102. });
  103. document.addEventListener("mousemove", (action) => {
  104. const from_mouse = () => {
  105. return new position(action.clientX, action.clientY);
  106. };
  107. if (this.#last_mouse_position === undefined) {
  108. this.#last_mouse_position = from_mouse();
  109. return;
  110. }
  111. const current = from_mouse();
  112. const difference = current.compare(this.#last_mouse_position);
  113. const size = new position(window.innerWidth, window.innerHeight);
  114. this.#last_mouse_position = current;
  115. this.#worker.player.rotate_cursor(difference, size);
  116. });
  117. }
  118. #setup_stopers() {
  119. const stoper = () => {
  120. this.#worker.player.movement.stop();
  121. this.#worker.player.rotation.stop();
  122. };
  123. document.addEventListener("mouseup", stoper);
  124. document.addEventListener("mouseover", stoper);
  125. }
  126. #create_push(name, icon, move) {
  127. return push(name, null, (target) => {
  128. target.innerText = "";
  129. target.appendChild(material_icon(icon));
  130. target.addEventListener("mousedown", () => {
  131. move(this.#worker.player);
  132. });
  133. });
  134. }
  135. }
  136. export { scene_ui };