CannonMaterial.re.ts 1017 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import * as RE from 'rogue-engine';
  2. import * as CANNON from 'cannon-es';
  3. import CannonBody from '../CannonBody.re';
  4. import * as RogueCannon from '../../Lib/RogueCannon';
  5. export default class CannonMaterial extends RE.Component {
  6. material: CANNON.Material;
  7. @RE.Prop("Number") friction: number;
  8. @RE.Prop("Number") restitution: number;
  9. awake() {
  10. this.createMaterial();
  11. }
  12. start() {
  13. this.setMaterial();
  14. }
  15. protected createMaterial() {
  16. this.material = new CANNON.Material(this.name);
  17. // if (this.friction < 0)
  18. this.material.friction = this.friction;
  19. // if (this.restitution < 0)
  20. this.material.restitution = this.restitution;
  21. RogueCannon.getWorld().addMaterial(this.material);
  22. }
  23. private setMaterial() {
  24. const cannonBody = RE.getComponent(CannonBody, this.object3d);
  25. if (cannonBody) {
  26. cannonBody.body.shapes.forEach(shape => shape.material = this.material);
  27. }
  28. }
  29. }
  30. RE.registerComponent(CannonMaterial);