RapierMovementController.re.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import GetForwardVector from 'Assets/Library/GetForwardVector';
  2. import * as RE from 'rogue-engine';
  3. import * as THREE from 'three'
  4. import FloorCheckComponent from './FloorCheckComponent.re';
  5. import RAPIER from '@dimforge/rapier3d-compat';
  6. import RapierBody from '@RE/RogueEngine/rogue-rapier/Components/RapierBody.re';
  7. export default class RapierMovementController extends RE.Component {
  8. @RE.props.num() speed: number = 1
  9. @RE.props.num() jumpStrength: number = 10
  10. vectorCalculator: GetForwardVector
  11. bodyComponent: RapierBody
  12. private jumpVector: RAPIER.Vector3 = new RAPIER.Vector3(0,1,0)
  13. private scaledVelocity: THREE.Vector3 = new THREE.Vector3(0,0,0)
  14. private rapierScaledVelocity: RAPIER.Vector3 = new RAPIER.Vector3(0,0,0)
  15. awake() {
  16. this.bodyComponent = RE.getComponent(RapierBody, this.object3d) as RapierBody
  17. }
  18. start() {
  19. this.vectorCalculator = new GetForwardVector(RE.Runtime.camera)
  20. }
  21. update() {
  22. let direction = {x:0, y: 0, z: 0}
  23. if (RE.Input.keyboard.getKeyPressed("KeyW")) {
  24. direction.x += 1
  25. }
  26. if (RE.Input.keyboard.getKeyPressed("KeyA")) {
  27. direction.y += -1
  28. }
  29. if (RE.Input.keyboard.getKeyPressed("KeyS")) {
  30. direction.x += -1
  31. }
  32. if (RE.Input.keyboard.getKeyPressed("KeyD")) {
  33. direction.y += 1
  34. }
  35. if (RE.Input.keyboard.getKeyPressed("Space")) {
  36. direction.z = 1
  37. }
  38. if(direction.x != 0) {
  39. this.moveForward(direction.x * this.speed)
  40. }
  41. if(direction.y != 0) {
  42. this.moveRight(direction.y * this.speed)
  43. }
  44. if(direction.z != 0) {
  45. const floorCheckComponent = RE.getComponent(FloorCheckComponent, this.object3d)
  46. if(floorCheckComponent) {
  47. if(floorCheckComponent.isOnFloor) {
  48. this.jumpVector.y = this.jumpStrength
  49. this.bodyComponent.body.applyImpulse(this.jumpVector, true)
  50. }
  51. }
  52. }
  53. }
  54. moveForward(distance) {
  55. this.scaledVelocity.set(0,0,0)
  56. this.scaledVelocity.addScaledVector(this.vectorCalculator.getForward(), distance)
  57. this.rapierScaledVelocity.x = this.scaledVelocity.x
  58. this.rapierScaledVelocity.y = this.scaledVelocity.y
  59. this.rapierScaledVelocity.z = this.scaledVelocity.z
  60. this.bodyComponent.body.applyImpulse(this.rapierScaledVelocity, true)
  61. }
  62. moveRight(distance) {
  63. this.scaledVelocity.set(0,0,0)
  64. this.scaledVelocity.addScaledVector(this.vectorCalculator.getRight(), distance)
  65. this.rapierScaledVelocity.x = this.scaledVelocity.x
  66. this.rapierScaledVelocity.y = this.scaledVelocity.y
  67. this.rapierScaledVelocity.z = this.scaledVelocity.z
  68. this.bodyComponent.body.applyImpulse(this.rapierScaledVelocity, true)
  69. }
  70. }
  71. RE.registerComponent(RapierMovementController);