RapierCuboid.re.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import RAPIER from '@dimforge/rapier3d-compat';
  2. import * as RE from 'rogue-engine';
  3. import * as THREE from 'three';
  4. import RogueRapier from '../../Lib/RogueRapier';
  5. import RapierCollider from './RapierCollider';
  6. export default class RapierCuboid extends RapierCollider {
  7. private _sizeOffsetX = 1;
  8. private _sizeOffsetY = 1;
  9. private _sizeOffsetZ = 1;
  10. @RE.props.num()
  11. get sizeOffsetX() {
  12. return this._sizeOffsetX;
  13. }
  14. set sizeOffsetX(value: number) {
  15. const oldValue = this._sizeOffsetX;
  16. this._sizeOffsetX = value;
  17. if (oldValue !== value && this.collider && RogueRapier.world) {
  18. RogueRapier.world.removeCollider(this.collider, false);
  19. this.init();
  20. }
  21. }
  22. @RE.props.num()
  23. get sizeOffsetY() {
  24. return this._sizeOffsetY;
  25. }
  26. set sizeOffsetY(value: number) {
  27. const oldValue = this._sizeOffsetY;
  28. this._sizeOffsetY = value;
  29. if (oldValue !== value && this.collider && RogueRapier.world) {
  30. RogueRapier.world.removeCollider(this.collider, false);
  31. this.init();
  32. }
  33. }
  34. @RE.props.num()
  35. get sizeOffsetZ() {
  36. return this._sizeOffsetZ;
  37. }
  38. set sizeOffsetZ(value: number) {
  39. const oldValue = this._sizeOffsetZ;
  40. this._sizeOffsetZ = value;
  41. if (oldValue !== value && this.collider && RogueRapier.world) {
  42. RogueRapier.world.removeCollider(this.collider, false);
  43. this.init();
  44. }
  45. }
  46. worldScale = new THREE.Vector3();
  47. protected createShape(): void {
  48. this.object3d.getWorldScale(this.worldScale);
  49. let colliderDesc = RAPIER.ColliderDesc.cuboid(
  50. this._sizeOffsetX * (this.worldScale.x/2),
  51. this._sizeOffsetY * (this.worldScale.y/2),
  52. this._sizeOffsetZ * (this.worldScale.z/2)
  53. );
  54. this.collider = RogueRapier.world.createCollider(colliderDesc, this.body);
  55. }
  56. }
  57. RE.registerComponent(RapierCuboid);