CannonBox.re.ts 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import * as RE from 'rogue-engine';
  2. import * as THREE from 'three';
  3. import * as CANNON from 'cannon-es';
  4. import CannonShape from './CannonShape';
  5. export default class CannonBox extends CannonShape {
  6. shape: CANNON.Box;
  7. @RE.props.vector3() sizeOffset: THREE.Vector3 = new THREE.Vector3(1, 1, 1);
  8. private _collisionResponse = true;
  9. @RE.props.checkbox()
  10. get collisionResponse() {
  11. return this._collisionResponse;
  12. };
  13. set collisionResponse(value: boolean) {
  14. this._collisionResponse = value;
  15. if (!this.shape) return;
  16. this.shape.collisionResponse = value;
  17. };
  18. worldScale = new THREE.Vector3();
  19. protected createShape() {
  20. this.object3d.getWorldScale(this.worldScale);
  21. this.shape = new CANNON.Box(
  22. new CANNON.Vec3(
  23. this.sizeOffset.x * (this.worldScale.x/2),
  24. this.sizeOffset.y * (this.worldScale.y/2),
  25. this.sizeOffset.z * (this.worldScale.z/2)
  26. )
  27. );
  28. this.shape.collisionResponse = this._collisionResponse;
  29. }
  30. }
  31. RE.registerComponent(CannonBox);