CannonMovementController.re.ts 2.5 KB

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