-
Notifications
You must be signed in to change notification settings - Fork 2
/
aframe-door.min.js
1 lines (1 loc) · 4.23 KB
/
aframe-door.min.js
1
!function(e){function t(o){if(n[o])return n[o].exports;var i=n[o]={exports:{},id:o,loaded:!1};return e[o].call(i.exports,i,i.exports,t),i.loaded=!0,i.exports}var n={};return t.m=e,t.c=n,t.p="",t(0)}([function(e,t){AFRAME.registerComponent("door",{schema:{width:{type:"number",default:1},height:{type:"number",default:2},depth:{type:"number",default:.25},color:{type:"color",default:"#AAA"},texture:{type:"string",default:""},type:{type:"string",default:"simple"},state:{type:"string",default:"closed"},open_direction:{type:"string",default:"right"},open_duration:{type:"number",default:3e3},close_duration:{type:"number",default:3e3},open_event:{type:"string",default:"open"},close_event:{type:"string",default:"close"},open_sound:{type:"string",default:""},close_sound:{type:"string",default:""},auto_distance:{type:"number",default:0}},init:function(){var e=this.data,t=this.el;this.createDoor(e,t),this.eventOpenCloseHandler=function(e){var n=e.type;t.querySelectorAll("[sound]").forEach(function(e){e.emit(n,null,!1)}),t.querySelectorAll("a-box").forEach(function(e){e.emit(n,null,!1)})},this.teleportOpenCloseHandler=function(n){var o=new THREE.Vector3;o.setFromMatrixPosition(t.object3D.matrixWorld);var i=o.distanceTo(n.detail.newPosition);i<e.auto_distance&&"closed"===e.state?(e.state="open",t.emit("open")):i>e.auto_distance&&"open"===e.state&&(e.state="closed",t.emit("close"))},t.addEventListener(e.open_event,this.eventOpenCloseHandler),t.addEventListener(e.close_event,this.eventOpenCloseHandler),t.closest("a-scene").addEventListener("teleport",this.teleportOpenCloseHandler)},update:function(e){var t=this.data,n=this.el;0!==Object.keys(e).length&&(t.width===e.width&&t.height===e.height&&t.depth===e.depth||(n.getObject3D("mesh").geometry=new THREE.BoxBufferGeometry(t.width,t.height,t.depth)),t.color!==e.color&&(n.getObject3D("mesh").material.color=t.color),e.open_event&&t.open_event!==e.open_event&&n.removeEventListener(e.open_event,this.eventOpenCloseHandler),t.open_event&&n.addEventListener(t.open_event,this.eventOpenCloseHandler),e.close_event&&t.close_event!==e.close_event&&n.removeEventListener(e.close_event,this.eventOpenCloseHandler),t.close_event&&n.addEventListener(t.close_event,this.eventOpenCloseHandler))},remove:function(){this.el.removeObject3D("mesh")},open:function(){this.el.emit("open")},close:function(){this.el.emit("close")},createDoor:function(e,t){if(""!==e.open_sound){var n=document.createElement("a-entity");n.setAttribute("sound","src: "+e.open_sound+"; on: "+e.open_event),t.appendChild(n)}if(""!==e.close_sound){var o=document.createElement("a-entity");o.setAttribute("sound","src: "+e.close_sound+"; on: "+e.close_event),t.appendChild(o)}for(var i="double"===e.type?2:1,r=0;r<i;r++){var s=document.createElement("a-box");s.setAttribute("color",e.color),s.setAttribute("depth",e.depth),s.setAttribute("src",e.texture);var a="property: position;",d="",l=0===r?-1:1;if("right"===e.open_direction||"left"===e.open_direction){var u=e.width/i;s.setAttribute("height",e.height),s.setAttribute("width",u);var p=0;2===i&&(p=u/2*l);var c=p+" 0 0";s.setAttribute("position",c);var h=p;"right"===e.open_direction&&1===i||1===r&&2===i?h=p+u:("left"===e.open_direction&&1===i||0===r&&2===i)&&(h=p-u);var v=h+" 0 0";a=this.getAnimation(c,v,e.open_event,e.open_duration,e.open_sound),d=this.getAnimation(v,c,e.close_event,e.close_duration,e.close_sound)}else if("up"===e.open_direction||"down"===e.open_direction){var _=e.height/i;s.setAttribute("height",_),s.setAttribute("width",e.width);var p=0;2===i&&(p=_/2*l);var c="0 "+p+" 0";s.setAttribute("position",c);var f=p;"up"===e.open_direction&&1===i||1===r&&2===i?f=p+_:("down"===e.open_direction&&1===i||0===r&&2===i)&&(f=p-_);var v="0 "+f+" 0";a=this.getAnimation(c,v,e.open_event,e.open_duration,e.open_sound),d=this.getAnimation(v,c,e.close_event,e.close_duration,e.close_sound)}s.setAttribute("animation__open",a),s.setAttribute("animation__close",d),t.appendChild(s)}},getAnimation:function(e,t,n,o,i){var r="property: position;";return r+=this.setAnimationDuration(i,o),r+=" from: "+e+";",r+=" to: "+t+";",r+=" startEvents: "+n+";"},setAnimationDuration:function(e,t){var n="";if(""!==e){var o=document.querySelector(e).duration;"undefined"!=typeof o&&(o=Math.trunc(1e3*o),n+=" dur: "+o+";")}else n+=" dur: "+t+";";return n}})}]);