FPSController.re.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import * as RE from "rogue-engine";
  2. import { Object3D, PerspectiveCamera, Vector3 } from "three";
  3. const fwdDirection = new Vector3(0, 0, -1);
  4. const bwdDirection = new Vector3(0, 0, 1);
  5. const leftDirection = new Vector3(-1, 0, 0);
  6. const rightDirection = new Vector3(1, 0, 0);
  7. export default class FPSController extends RE.Component {
  8. @RE.props.num() rotSpeed: number = 1;
  9. @RE.props.num(-Math.PI/2, 0) minCameraRotY: number = -1.5708;
  10. @RE.props.num(0, Math.PI/2) maxCameraRotY: number = 1.5708;
  11. @RE.props.num() cameraHeight: number = 0.4;
  12. @RE.props.num() walkSpeed: number = 2;
  13. @RE.props.num() fwdSpeedMultiplier: number = 1.3;
  14. @RE.props.num() runSpeedMultiplier: number = 1.8;
  15. @RE.props.object3d() character: Object3D;
  16. camera: PerspectiveCamera;
  17. awake() {
  18. RE.Input.mouse.lock();
  19. this.camera = new PerspectiveCamera();
  20. RE.App.currentScene.add(this.camera);
  21. RE.App.activeCamera = this.camera.uuid;
  22. this.camera.position.copy(this.character.position);
  23. this.camera.position.y += this.cameraHeight;
  24. this.camera.rotation.copy(this.character.rotation);
  25. const appContainer = document.getElementById("rogue-app");
  26. if (!appContainer) return;
  27. appContainer.onmousedown = (e) => {
  28. RE.Input.mouse.lock();
  29. };
  30. RE.Runtime.onStop(() => {
  31. if (!appContainer) return;
  32. appContainer.onmousedown = null;
  33. });
  34. this.camera.rotation.order = "YXZ";
  35. this.character.rotation.order = "YXZ";
  36. this.camera.up.set(0, 1, 0);
  37. }
  38. update() {
  39. const deltaTime = RE.Runtime.deltaTime;
  40. if (RE.Input.mouse.isMoving && document.pointerLockElement) {
  41. const mouseDeltaX = RE.Input.mouse.movementX * this.rotSpeed * deltaTime;
  42. const mouseDeltaY = RE.Input.mouse.movementY * this.rotSpeed * deltaTime;
  43. this.character.rotation.set(
  44. this.character.rotation.x,
  45. this.character.rotation.y - mouseDeltaX,
  46. this.character.rotation.z
  47. );
  48. this.camera.rotation.set(
  49. this.camera.rotation.x - mouseDeltaY,
  50. this.character.rotation.y,
  51. this.camera.rotation.z
  52. );
  53. this.camera.rotation.x = Math.max(
  54. this.minCameraRotY,
  55. Math.min(this.maxCameraRotY, this.camera.rotation.x)
  56. );
  57. }
  58. RE.Input.keyboard.getKeyPressed("Escape") && RE.Input.mouse.unlock();
  59. let actualSpeed = this.walkSpeed;
  60. let onlyFwd = true;
  61. const movementVector = new Vector3();
  62. if (RE.Input.keyboard.getKeyDown("Space")) {
  63. //jump logic here
  64. }
  65. if (RE.Input.keyboard.getKeyPressed("KeyW")) {
  66. movementVector.add(fwdDirection);
  67. } else if (RE.Input.keyboard.getKeyPressed("KeyS")) {
  68. movementVector.add(bwdDirection);
  69. onlyFwd = false;
  70. }
  71. if (RE.Input.keyboard.getKeyPressed("KeyA")) {
  72. movementVector.add(leftDirection);
  73. onlyFwd = false;
  74. } else if (RE.Input.keyboard.getKeyPressed("KeyD")) {
  75. movementVector.add(rightDirection);
  76. onlyFwd = false;
  77. }
  78. if (onlyFwd) {
  79. if (RE.Input.keyboard.getKeyPressed("ShiftLeft")) {
  80. actualSpeed *= this.runSpeedMultiplier;
  81. } else {
  82. actualSpeed *= this.fwdSpeedMultiplier;
  83. }
  84. }
  85. movementVector.normalize();
  86. if (movementVector.length() > 0) {
  87. movementVector.copy(
  88. movementVector
  89. .transformDirection(this.character.matrix)
  90. .multiplyScalar(actualSpeed * deltaTime)
  91. );
  92. this.character.position.x += movementVector.x;
  93. this.character.position.z += movementVector.z;
  94. }
  95. this.camera.position.set(
  96. this.character.position.x,
  97. this.character.position.y + this.cameraHeight,
  98. this.character.position.z
  99. );
  100. }
  101. }
  102. RE.registerComponent(FPSController);