scene-ui.js 4.9 KB

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