CannonVehicleController.re.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import * as RE from 'rogue-engine';
  2. import CannonRaycastVehicle from '../Objects/CannonRaycastVehicle.re';
  3. export default class CannonVehicleController extends RE.Component {
  4. @RE.props.num() maxForce = 200;
  5. @RE.props.num() reverseForce = 50;
  6. @RE.props.num() breakForce = 250;
  7. @RE.props.num() maxSteering = 0.5;
  8. raycastVehicle: CannonRaycastVehicle;
  9. start() {
  10. this.raycastVehicle = RE.getComponentByName("CannonRaycastVehicle", this.object3d) as CannonRaycastVehicle;
  11. }
  12. update() {
  13. if (!(this.raycastVehicle instanceof CannonRaycastVehicle)) return;
  14. // Acceleration
  15. if (RE.Input.keyboard.getKeyPressed("KeyW")) {
  16. this.releaseBreaks();
  17. this.raycastVehicle.vehicle.applyEngineForce(this.maxForce, 2);
  18. this.raycastVehicle.vehicle.applyEngineForce(this.maxForce, 3);
  19. }
  20. if (RE.Input.keyboard.getKeyPressed("KeyS")) {
  21. const speed = -this.raycastVehicle.vehicle.currentVehicleSpeedKmHour;
  22. if (speed <= 0 ) {
  23. this.releaseBreaks();
  24. this.raycastVehicle.vehicle.applyEngineForce(-this.reverseForce, 2);
  25. this.raycastVehicle.vehicle.applyEngineForce(-this.reverseForce, 3);
  26. }
  27. else if (speed > 0.05) {
  28. // this.releaseBreaks();
  29. this.raycastVehicle.vehicle.applyEngineForce(-this.maxForce, 2);
  30. this.raycastVehicle.vehicle.applyEngineForce(-this.maxForce, 3);
  31. // this.break();
  32. }
  33. // else if (speed > 0.1) {
  34. // this.break();
  35. // }
  36. }
  37. if (RE.Input.keyboard.getKeyUp("KeyW")) {
  38. this.raycastVehicle.vehicle.applyEngineForce(0, 2);
  39. this.raycastVehicle.vehicle.applyEngineForce(0, 3);
  40. }
  41. if (RE.Input.keyboard.getKeyUp("KeyS")) {
  42. const speed = Math.abs(this.raycastVehicle.vehicle.currentVehicleSpeedKmHour);
  43. this.raycastVehicle.vehicle.applyEngineForce(0, 2);
  44. this.raycastVehicle.vehicle.applyEngineForce(0, 3);
  45. if (speed < 1) {
  46. this.break();
  47. }
  48. else {
  49. this.releaseBreaks();
  50. }
  51. }
  52. // Steering
  53. if (RE.Input.keyboard.getKeyPressed("KeyA")) {
  54. this.raycastVehicle.vehicle.setSteeringValue(this.maxSteering, 0);
  55. this.raycastVehicle.vehicle.setSteeringValue(this.maxSteering, 1);
  56. }
  57. if (RE.Input.keyboard.getKeyPressed("KeyD")) {
  58. this.raycastVehicle.vehicle.setSteeringValue(-this.maxSteering, 0);
  59. this.raycastVehicle.vehicle.setSteeringValue(-this.maxSteering, 1);
  60. }
  61. if (RE.Input.keyboard.getKeyUp("KeyA")) {
  62. this.raycastVehicle.vehicle.setSteeringValue(0, 0);
  63. this.raycastVehicle.vehicle.setSteeringValue(0, 1);
  64. }
  65. if (RE.Input.keyboard.getKeyUp("KeyD")) {
  66. this.raycastVehicle.vehicle.setSteeringValue(0, 0);
  67. this.raycastVehicle.vehicle.setSteeringValue(0, 1);
  68. }
  69. }
  70. break() {
  71. this.raycastVehicle.vehicle.setBrake(this.breakForce, 0);
  72. this.raycastVehicle.vehicle.setBrake(this.breakForce, 1);
  73. this.raycastVehicle.vehicle.setBrake(this.breakForce, 2);
  74. this.raycastVehicle.vehicle.setBrake(this.breakForce, 3);
  75. }
  76. releaseBreaks() {
  77. this.raycastVehicle.vehicle.setBrake(0, 0);
  78. this.raycastVehicle.vehicle.setBrake(0, 1);
  79. this.raycastVehicle.vehicle.setBrake(0, 2);
  80. this.raycastVehicle.vehicle.setBrake(0, 3);
  81. }
  82. }
  83. RE.registerComponent(CannonVehicleController);