UNPKG

ooomap-simulate-navi

Version:

ooomap simulate navigation plugin

2 lines (1 loc) 9.52 kB
var t,e;t=this,e=function(t){"use strict";var e=function(){this.callbacks=[],this.alldoneArr=[],this.type="OMPromise",this.__done=!1,this.id=Math.random().toString().slice(2)};function n(t,e){var n,i=Math.sign(t);n=e?["前",["左前","右前"],["左","右"],["左后","右后"],"后"]:["北",["西北","东北"],["西","东"],["西南","东南"],"南"];var o=Math.abs(t)+22.5,r=Math.floor(o/45),s=n[r];return{text:Array.isArray(s)?s[i>0?0:1]:s,index:r*(r>0&&r<4?i>0?1:-1:1)}}e.prototype.delay=function(t){var e=new this.constructor;return setTimeout((function(){e.resolve()}),t),e},e.prototype.addAlldone=function(t){this.alldoneArr.push(t)},e.prototype.alldone=function(t){this.alldoneArr.forEach((function(e){e(t)}))},e.prototype.destroy=function(){this.callbacks.length=this.alldoneArr.length=0},e.prototype.then=function(t){return this.callbacks.push(t),this},e.prototype._hasIndex=function(t){return void 0!==this.__index&&(t.__index=this.__index),t},e.prototype.resolve=function(t){var e=this,n=null,i=!1;setTimeout((function(){var o=e._hasIndex(n||t);if(!e.callbacks.length)return e.alldone(o),e.destroy(),void(e.__done=!0);for(var r=0;r<e.callbacks.length;r++){var s=e.callbacks[r](n||t);if(s instanceof e.constructor){for(i=!0,r++;r<e.callbacks.length;r++)s.then(e.callbacks[r]);e.alldoneArr.length&&(s.alldoneArr=e.alldoneArr),n=null}else n=s}i||(o=e._hasIndex(n||t),e.alldone(o),e.destroy(),e.__done=!0)}),0)},e.empty=function(t){var n=new e;return setTimeout((function(){n.resolve(t)}),0),n},e.delay=function(t){var n=new e;return setTimeout((function(){n.resolve()}),t),n},e.all=function(t,n){t=t.filter((function(t){return!t.__done}));var prom=new e,i=0,o=[];function r(e){i++,o.push(e),i===t.length&&(n&&(o.sort((function(t,e){return void 0!==t.__index&&void 0!==e.__index?t.__index-e.__index:0})),o.forEach((function(t){return delete t.__index}))),prom.resolve(o))}return t.forEach((function(t){t.addAlldone(r)})),prom};var i=function(t,e,n){this.map=t,this.config=Object.assign({startMarker:null,endMarker:null,node:null,speed:5,followPosition:!0,followDirection:!1,followPolar:30,changePolar:60,followZoom:260,changeZoom:800,offsetHeight:.3,offsetRotateX:-Math.PI/2,offsetRotateZ:Math.PI/2,resumingTime:2e3,backToStart:!0},e),function(t,e){e={};var n={};t.setEventState=function(t,e){n[t]=e},t.getEventState=function(t){return!1!==n[t]},t._tmpEventMode=!1,Object.defineProperties(t,{tmpEventMode:{get:function(){return t._tmpEventMode},set:function(e){t._tmpEventMode=e,e||t.tmpEvents&&(t.tmpEvents.forEach((function(t){return t()})),t.tmpEvents.length=0,delete t.tmpEvents)}}}),t.on=function(n,i){var o=[].concat(n);function r(){o.forEach((function(e){t.off(e,i)}))}return o.forEach((function(t){(e[t]=e[t]||[]).push(i)})),t.tmpEventMode&&(t.tmpEvents||(t.tmpEvents=[]),t.tmpEvents.push(r)),r},t.clearHandlers=function(t){t?(e[t]={},delete n[t]):(e={},n={})},t.once=function(e,n){function i(){n.apply(t.off(e,i),arguments)}return i.h=n,t.on(e,i)},t.off=function(i,o){for(var r=e[i],s=0;o&&r&&r[s];s++)r[s]!=o&&r[s].h!=o||r.splice(s--,1);return o||(delete e[i],delete n[i]),t},t.emit=function(i){var o=arguments;if(!1===n[i])return t;t.__events||(t.__events={}),t.__events[i]=i;for(var r=e[i],s=0;r&&r[s];)r[s++].apply(t,r.slice.call(o,1));return t}}(this),this._starting=!1,this._isPicking=!1,this.routeDescription=[],this.Vector3=this.config.building.entity.position.constructor,this._north=new this.Vector3(0,1,0),this._handleEvent()};i.prototype._gotoStart=function(t){var e=this,n=this._getLocation();this.config.building.getFloorByFloorNumber(this._currentFloorNumber).then((function(i){var o=i.getHeight();n.point.z=o,e._currentHeight=o,e.map.view.moveTo({viewCenter:n.point,polarAngle:e.config.followPolar,zoom:e.config.followZoom}).then((function(){t||(e._starting=!0)}))}))},i.prototype._changeFloor=function(t){var e=this;this._starting=!1;var n=this._rr[this._simulateData.lineIndex];this._currentFloorNumber=n.floorNumber;var i=!1;this.config.building.currentFocus[0]!==this._currentFloorNumber&&(i=!0),i?this.config.building.focusFloors(this._currentFloorNumber,{zoom:this.config.changeZoom,polarAngle:this.config.changePolar}).then((function(n){e.emit("changeFloor",n[0]),e._gotoStart(t)})):this._gotoStart(t)},i.prototype._handleEvent=function(){var t=this;this.on("start",(function(){t._changeFloor()})),this._offUpdate=this.map.on("update",this._update.bind(this)),this._offPick=this.map.on("startPick",(function(){t._isPicking=!0,clearTimeout(t._id)})),this._offPick2=this.map.on("endPick",(function(){t._id=setTimeout((function(){t._isPicking=!1}),t.config.resumingTime)}))},i.prototype._update=function(t){if(this._starting){this._simulateData.len+=t*this.config.speed;var e=this._rr[this._simulateData.lineIndex].lines[0];if(this._simulateData.len>=e.length)return this._simulateData.lineIndex++,this._simulateData.lineIndex===this._rr.length?(this._starting=!1,this.emit("complete"),void this.stop()):(this._simulateData.len=0,void this._changeFloor());var n=this._getLocation();n.point.z=this._currentHeight+this.config.offsetHeight;var i=this.config.node;if(i){if(n.angle=this.getAngleWithDirection(n.direction),"PlaneMarkerNode"===i.type?i.angle=n.angle:"OMModel"===i.type&&(i.entity.lookAt(n.point.clone().add(n.direction)),i.entity.rotateX(this.config.offsetRotateX),i.entity.rotateZ(this.config.offsetRotateZ)),i.position.copy(n.point),this.emit("update",n),this._isPicking)return;this.config.followPosition&&(this.map.view.viewCenter=n.point,this.config.followDirection&&(this.map.view.viewAngle=n.angle))}}},i.prototype.analyzeRoute=function(t){return this.routeDescription=function(t,e){for(var i=(e=[].concat(e)).filter((function(t){return"crossLines"!==t.type})),o=i.map((function(e){return function(t,e){var i=t.vectors[0];if(!(i.length<2)){var o=null,r=0,s="",a=0,c=[];return i.reduce((function(t,l,u){var h,f=l.clone().sub(t),d=f.length(),p=f.normalize();return o?0===(h=n(e.getAngleWithDirection(p,o),!0)).index?r+=d:(c.push({text:s,distance:r,index:a}),s=h.text,r=d,a=u-1):(h=n(e.getAngleWithDirection(p)),s=h.text,r=d,a=u-1),u===i.length-1&&c.push({text:s,distance:r,index:a}),o=p,l})),c}}(e,t)})),r=0,s=o.length;r<s;r++)r<s-1?o[r].push({text:i[r+1].floorNumber,floorNumber:i[r+1].floorNumber,index:i[r].vectors[0].length-1,type:"changeFloor"}):o[r].push({text:"终点",index:i[r].vectors[0].length-1,type:"terminal"});return o}(this,t),this.routeDescription},i.prototype.getDescriptionTexts=function(t){var e=t,n=[],i="向 "+e.desc.text+" 行驶 "+e.desc.distance.toFixed(1)+" 米";switch(n.push(i),e.nextDesc.type){case"changeFloor":i=e.disToNext.toFixed(1)+" 米后乘梯前往 "+e.nextDesc.floorNumber+" 层";break;case"terminal":i=e.disToNext.toFixed(1)+" 米后到达终点";break;default:i=e.disToNext.toFixed(1)+" 米后向 "+e.nextDesc.text+" 行驶"}return n.push(i),n},i.prototype._getLocation=function(){var t=this.routeDescription[this._simulateData.lineIndex],e=this._rr[this._simulateData.lineIndex],n=e.lines[0].getPointByLength(this._simulateData.len),i=this._getVecIndexByLength(this._simulateData.len),o=i[0],r=i[1],s=this._getDescription(t,o),a=s.description,c=s.index,l=t[c+1];return l&&(n.disToNext=this._getDisTo(e.vectors[0],o,l.index,r)),n.desc=a,n.descIndex=c,n.nextDesc=l,n},i.prototype._getDisTo=function(t,e,n,i){if(!(e+1>n)){for(var o=0,r=e+1,s=n-1;r<s;r++)o+=t[r].distanceTo(t[r+1]);return o+i}},i.prototype._getDescription=function(t,e){for(var n=0,i=0;i<t.length;i++)e>=t[i].index&&(n=i);return{description:t[n],index:n}},i.prototype._getVecIndexByLength=function(t){for(var e=this._rr[this._simulateData.lineIndex].vectors[0],n=0,i=1;i<e.length;i++)if((n+=e[i].distanceTo(e[i-1]))>=t)return[i-1,n-t];return[e.length-1,0]},i.prototype.getAngleWithDirection=function(t,e){e=e||this._north;var n=Math.sign(e.clone().cross(t).z);return 0===n&&(n=1),180*e.angleTo(t)/Math.PI*n},i.prototype.findPath=function(t){var e=this,n=t.building||this.config.building;if(n&&(t.from&&t.to||t.points)){var i;t.points?i=t.points:t.from&&t.to&&(i=[t.from,t.to]);var o=this.config.startMarker,r=this.config.endMarker;if(o&&r){var s=i[0],a=i[i.length-1];n.getFloorByFloorNumber(s.floorNumber).then((function(t){t.add(o),o.position.copy(s.point),o.show=!0})),n.getFloorByFloorNumber(a.floorNumber).then((function(t){t.add(r),r.position.copy(a.point),r.show=!0}))}return n.findPath.apply(n,i).then((function(t){return e.analyzeRoute(t),t}))}},i.prototype.start=function(t){var n=this,i=null;if(Object.assign(this.config,t),t.routeResult)i=e.empty(t.routeResult);else{if(!t.from||!t.to)return;i=this.findPath(t)}i&&i.then((function(t){t=[].concat(t),n._rr=t.filter((function(t){return"crossLines"!==t.type})),n._rr.length&&(n._simulateData={time:0,lineIndex:0,len:0},n.routeDescription||n.analyzeRoute(t),n.emit("start",n.routeDescription))}))},i.prototype.stop=function(){this._starting=!1,this._simulateData&&(this._simulateData.len=0,this._simulateData.lineIndex=0),this.config.backToStart&&this._changeFloor(!0),this.emit("stop")},i.prototype.clearRoutes=function(){this.config.building&&(this.config.building.clearRoutes(),this.config.startMarker&&(this.config.startMarker.show=!0),this.config.endMarker&&(this.config.endMarker.show=!0))},t.SimulateNavi=i,Object.defineProperty(t,"__esModule",{value:!0})},"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).om=(t="undefined"!=typeof globalThis?globalThis:t||self).om||{});