-
Notifications
You must be signed in to change notification settings - Fork 0
/
aframe-alongpath-component.min.js
1 lines (1 loc) · 3.01 KB
/
aframe-alongpath-component.min.js
1
!function(t){function e(r){if(i[r])return i[r].exports;var a=i[r]={exports:{},id:r,loaded:!1};return t[r].call(a.exports,a,a.exports,e),a.loaded=!0,a.exports}var i={};return e.m=t,e.c=i,e.p="",e(0)}([function(t,e){if("undefined"==typeof AFRAME)throw new Error("Component attempted to register before AFRAME was available.");AFRAME.registerComponent("alongpath",{schema:{curve:{default:""},triggers:{default:"a-curve-point"},triggerRadius:{type:"number",default:.01},dur:{default:1e3},delay:{default:0},loop:{default:!1},rotate:{default:!1},resetonplay:{default:!0}},init:function(){},update:function(t){this.curve=document.querySelector(this.data.curve),this.triggers=this.curve.querySelectorAll(this.data.triggers),this.curve?this.initialPosition=this.el.object3D.position:console.warn("Curve not found. Can't follow anything..."),this.reset()},reset:function(){this.interval=0,this.el.removeState("endofpath"),this.el.removeState("moveonpath"),this.activeTrigger&&(this.activeTrigger.removeState("alongpath-active-trigger"),this.activeTrigger=null)},getI_:function(t,e,i){var r=0;return r=t-e>=i?1:t-e<0?0:(t-e)/i},tick:function(t,e){var i=this.curve.components.curve?this.curve.components.curve.curve:null;if(i){if(!this.el.is("endofpath")){this.interval=this.interval+e;var r=this.getI_(this.interval,this.data.delay,this.data.dur);if(this.data.loop===!1&&r>=1)this.el.setAttribute("position",i.points[i.points.length-1]),this.el.removeState("moveonpath"),this.el.addState("endofpath"),this.el.emit("movingended");else if(this.data.loop===!0&&r>=1)this.el.emit("movingended"),this.interval=this.data.delay;else{this.el.is("moveonpath")||(this.el.addState("moveonpath"),this.el.emit("movingstarted"));var a=i.getPoint(r);this.el.setAttribute("position",a)}if(this.data.rotate===!0){var s=this.interval+e,o=i.getPoint(this.getI_(s,this.data.delay,this.data.dur));this.el.object3D.lookAt(o)}this.triggers&&this.triggers.length>0&&this.updateActiveTrigger()}}else console.error("The entity associated with the curve property has no curve component.")},play:function(){this.data.resetonplay&&this.reset()},remove:function(){this.el.object3D.position.copy(this.initialPosition)},updateActiveTrigger:function(){for(var t=0;t<this.triggers.length;t++)if(this.triggers[t].object3D){if(this.triggers[t].object3D.position.distanceTo(this.el.object3D.position)<=this.data.triggerRadius){this.activeTrigger&&this.activeTrigger!=this.triggers[t]?(this.activeTrigger.removeState("alongpath-active-trigger"),this.activeTrigger.emit("alongpath-trigger-deactivated"),this.activeTrigger=this.triggers[t],this.activeTrigger.addState("alongpath-active-trigger"),this.activeTrigger.emit("alongpath-trigger-activated")):this.activeTrigger||(this.activeTrigger=this.triggers[t],this.activeTrigger.addState("alongpath-active-trigger"),this.activeTrigger.emit("alongpath-trigger-activated"));break}this.activeTrigger&&this.activeTrigger==this.triggers[t]&&(this.activeTrigger.removeState("alongpath-active-trigger"),this.activeTrigger.emit("alongpath-trigger-deactivated"),this.activeTrigger=null)}}})}]);