Skip to content
Snippets Groups Projects
duck.js 1.27 KiB
Newer Older
/* global CANNON */
/**
 * Floats a duck based on its scale.
 * @component duck
 */
AFRAME.registerComponent("duck", {
  schema: {
    initialForce: { default: 0 },
    maxForce: { default: 6.5 },
    maxScale: { default: 5 }
  },

  init: function() {
Kevin Lee's avatar
Kevin Lee committed
    this.physicsSystem = this.el.sceneEl.systems.physics;
    this.position = new CANNON.Vec3();
    this.force = new CANNON.Vec3(0, this.data.initialForce, 0);
    this.initialScale = this.el.object3D.scale.x;
    this.maxScale = this.data.maxScale * this.initialScale;
  },

  play: function() {
Kevin Lee's avatar
Kevin Lee committed
    this.physicsSystem.addComponent(this);
Kevin Lee's avatar
Kevin Lee committed
    this.physicsSystem.removeComponent(this);
    if (this.el.body && NAF.utils.isMine(this.el)) {
      const currentScale = this.el.object3D.scale.x;
      const ratio = Math.min(1, (currentScale - this.initialScale) / (this.maxScale - this.initialScale));
      const force = ratio * this.data.maxForce;
      if (force > 0) {
        const angle = Math.random() * Math.PI * 2;
        const x = Math.cos(angle);
        const z = Math.sin(angle);
        this.force.set(x, force, z);
        this.position.set(x * 0.01, 0, z * 0.01);
        this.el.body.applyForce(this.force, this.position);
      }
    }
  }
});