CannonContactMaterial.re.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import * as RE from 'rogue-engine';
  2. import * as CANNON from 'cannon-es';
  3. import * as RogueCannon from '../../Lib/RogueCannon';
  4. export default class CannonContactMaterial extends RE.Component {
  5. contactMaterial: CANNON.ContactMaterial;
  6. @RE.props.text() materialA: string;
  7. @RE.props.text() materialB: string;
  8. @RE.props.num() friction: number;
  9. @RE.props.num() restitution: number;
  10. start() {
  11. this.createContactMaterial();
  12. }
  13. private getMaterial(materialName: string) {
  14. return RogueCannon.getWorld().materials.find(material => material.name === materialName)
  15. }
  16. private createContactMaterial() {
  17. const cannonMaterialA = this.getMaterial(this.materialA);
  18. const cannonMaterialB = this.getMaterial(this.materialB);
  19. if (!cannonMaterialA || !cannonMaterialB) return;
  20. this.contactMaterial = new CANNON.ContactMaterial(cannonMaterialA, cannonMaterialB, {
  21. friction: this.friction,
  22. restitution: this.restitution,
  23. });
  24. this.contactMaterial.friction = this.friction;
  25. this.contactMaterial.restitution = this.restitution;
  26. RogueCannon.getWorld().addContactMaterial(this.contactMaterial);
  27. }
  28. }
  29. RE.registerComponent(CannonContactMaterial);