phaser3-rex-plugins
Version:
2 lines (1 loc) • 16.6 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).rexgraphplugin=e();}(undefined,(function(){function t(e){return t="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},t(e)}function e(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function n(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,(i=r.key,s=void 0,"symbol"==typeof(s=function(t,e){if("object"!=typeof t||null===t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var r=n.call(t,e||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return ("string"===e?String:Number)(t)}(i,"string"))?s:String(s)),r);}var i,s;}function r(t,e,r){return e&&n(t.prototype,e),r&&n(t,r),Object.defineProperty(t,"prototype",{writable:!1}),t}function i(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),Object.defineProperty(t,"prototype",{writable:!1}),e&&o(t,e);}function s(t){return s=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},s(t)}function o(t,e){return o=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},o(t,e)}function u(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}function h(t){var e=function(){if("undefined"==typeof Reflect||!Reflect.construct)return !1;if(Reflect.construct.sham)return !1;if("function"==typeof Proxy)return !0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return !1}}();return function(){var n,r=s(t);if(e){var i=s(this).constructor;n=Reflect.construct(r,arguments,i);}else n=r.apply(this,arguments);return function(t,e){if(e&&("object"==typeof e||"function"==typeof e))return e;if(void 0!==e)throw new TypeError("Derived constructors may only return object or undefined");return u(t)}(this,n)}}function a(){return a="undefined"!=typeof Reflect&&Reflect.get?Reflect.get.bind():function(t,e,n){var r=function(t,e){for(;!Object.prototype.hasOwnProperty.call(t,e)&&null!==(t=s(t)););return t}(t,e);if(r){var i=Object.getOwnPropertyDescriptor(r,e);return i.get?i.get.call(arguments.length<3?t:n):i.value}},a.apply(this,arguments)}var f=function(){function t(n){e(this,t),this.scene=n;}return r(t,null,[{key:"register",value:function(e,n){t.prototype[e]=n;}}]),t}();function c(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}var v={exports:{}};!function(t){var e=Object.prototype.hasOwnProperty,n="~";function r(){}function i(t,e,n){this.fn=t,this.context=e,this.once=n||!1;}function s(t,e,r,s,o){if("function"!=typeof r)throw new TypeError("The listener must be a function");var u=new i(r,s||t,o),h=n?n+e:e;return t._events[h]?t._events[h].fn?t._events[h]=[t._events[h],u]:t._events[h].push(u):(t._events[h]=u,t._eventsCount++),t}function o(t,e){0==--t._eventsCount?t._events=new r:delete t._events[e];}function u(){this._events=new r,this._eventsCount=0;}Object.create&&(r.prototype=Object.create(null),(new r).__proto__||(n=!1)),u.prototype.eventNames=function(){var t,r,i=[];if(0===this._eventsCount)return i;for(r in t=this._events)e.call(t,r)&&i.push(n?r.slice(1):r);return Object.getOwnPropertySymbols?i.concat(Object.getOwnPropertySymbols(t)):i},u.prototype.listeners=function(t){var e=n?n+t:t,r=this._events[e];if(!r)return [];if(r.fn)return [r.fn];for(var i=0,s=r.length,o=new Array(s);i<s;i++)o[i]=r[i].fn;return o},u.prototype.listenerCount=function(t){var e=n?n+t:t,r=this._events[e];return r?r.fn?1:r.length:0},u.prototype.emit=function(t,e,r,i,s,o){var u=n?n+t:t;if(!this._events[u])return !1;var h,a,f=this._events[u],c=arguments.length;if(f.fn){switch(f.once&&this.removeListener(t,f.fn,void 0,!0),c){case 1:return f.fn.call(f.context),!0;case 2:return f.fn.call(f.context,e),!0;case 3:return f.fn.call(f.context,e,r),!0;case 4:return f.fn.call(f.context,e,r,i),!0;case 5:return f.fn.call(f.context,e,r,i,s),!0;case 6:return f.fn.call(f.context,e,r,i,s,o),!0}for(a=1,h=new Array(c-1);a<c;a++)h[a-1]=arguments[a];f.fn.apply(f.context,h);}else {var v,l=f.length;for(a=0;a<l;a++)switch(f[a].once&&this.removeListener(t,f[a].fn,void 0,!0),c){case 1:f[a].fn.call(f[a].context);break;case 2:f[a].fn.call(f[a].context,e);break;case 3:f[a].fn.call(f[a].context,e,r);break;case 4:f[a].fn.call(f[a].context,e,r,i);break;default:if(!h)for(v=1,h=new Array(c-1);v<c;v++)h[v-1]=arguments[v];f[a].fn.apply(f[a].context,h);}}return !0},u.prototype.on=function(t,e,n){return s(this,t,e,n,!1)},u.prototype.once=function(t,e,n){return s(this,t,e,n,!0)},u.prototype.removeListener=function(t,e,r,i){var s=n?n+t:t;if(!this._events[s])return this;if(!e)return o(this,s),this;var u=this._events[s];if(u.fn)u.fn!==e||i&&!u.once||r&&u.context!==r||o(this,s);else {for(var h=0,a=[],f=u.length;h<f;h++)(u[h].fn!==e||i&&!u[h].once||r&&u[h].context!==r)&&a.push(u[h]);a.length?this._events[s]=1===a.length?a[0]:a:o(this,s);}return this},u.prototype.removeAllListeners=function(t){var e;return t?(e=n?n+t:t,this._events[e]&&o(this,e)):(this._events=new r,this._eventsCount=0),this},u.prototype.off=u.prototype.removeListener,u.prototype.addListener=u.prototype.on,u.prefixed=n,u.EventEmitter=u,t.exports=u;}(v);var l=function(t){i(s,t);var n=h(s);function s(){return e(this,s),n.apply(this,arguments)}return r(s,[{key:"shutdown",value:function(){this.removeAllListeners();}},{key:"destroy",value:function(){this.removeAllListeners();}}]),s}(c(v.exports)),p=function(t,e,n){if(t&&"number"!=typeof t){if(t.hasOwnProperty(e))return t[e];if(-1!==e.indexOf(".")){for(var r=e.split("."),i=t,s=n,o=0;o<r.length;o++){if(!i.hasOwnProperty(r[o])){s=n;break}s=i[r[o]],i=i[r[o]];}return s}return n}return n},y=function(){function t(n){e(this,t),this.nextId=p(n,"start",1),this.uidKey=p(n,"uidKey","$uid"),this.autoRemove=p(n,"remove",!0),this.refs={},this.count=0;}return r(t,[{key:"add",value:function(t,e){var n=this.refs,r=this.uidKey;if(r&&null!=(e=t[r]))return this;if(null==e)do{e=this.nextId,this.nextId++;}while(n.hasOwnProperty(e));return n.hasOwnProperty(e)?e=null:(n[e]=t,this.count++,r&&(t[r]=e),this.autoRemove&&t.on&&t.once("destroy",(function(){this.remove(e);}),this)),r?this:e}},{key:"addMultiple",value:function(t){for(var e=0,n=t.length;e<n;e++)this.add(t[e]);return this}},{key:"get",value:function(t){return this.refs[t]}},{key:"has",value:function(t){return this.refs.hasOwnProperty(t)}},{key:"remove",value:function(t){var e=this.refs;if(e.hasOwnProperty(t)){if(this.uidKey)e[t][this.uidKey]=void 0;delete e[t],this.count--;}return this}},{key:"forEach",value:function(t,e){var n,r=this.refs;for(var i in r)n=r[i],e?t.call(e,n,i):t(n,i);}},{key:"clear",value:function(){this.forEach((function(t){this.remove(t);}),this);}}]),t}(),d=new y({uidKey:"$uid",remove:!1}),g={setEventEmitter:function(t,e){return void 0===e&&(e=Phaser.Events.EventEmitter),this._privateEE=!0===t||void 0===t,this._eventEmitter=this._privateEE?new e:t,this},destroyEventEmitter:function(){return this._eventEmitter&&this._privateEE&&this._eventEmitter.shutdown(),this},getEventEmitter:function(){return this._eventEmitter},on:function(){return this._eventEmitter&&this._eventEmitter.on.apply(this._eventEmitter,arguments),this},once:function(){return this._eventEmitter&&this._eventEmitter.once.apply(this._eventEmitter,arguments),this},off:function(){return this._eventEmitter&&this._eventEmitter.off.apply(this._eventEmitter,arguments),this},emit:function(t){return this._eventEmitter&&t&&this._eventEmitter.emit.apply(this._eventEmitter,arguments),this},addListener:function(){return this._eventEmitter&&this._eventEmitter.addListener.apply(this._eventEmitter,arguments),this},removeListener:function(){return this._eventEmitter&&this._eventEmitter.removeListener.apply(this._eventEmitter,arguments),this},removeAllListeners:function(){return this._eventEmitter&&this._eventEmitter.removeAllListeners.apply(this._eventEmitter,arguments),this},listenerCount:function(){return this._eventEmitter?this._eventEmitter.listenerCount.apply(this._eventEmitter,arguments):0},listeners:function(){return this._eventEmitter?this._eventEmitter.listeners.apply(this._eventEmitter,arguments):[]},eventNames:function(){return this._eventEmitter?this._eventEmitter.eventNames.apply(this._eventEmitter,arguments):[]}},m=Phaser.Scene,b=function(t){return t instanceof m},E=Phaser.Game,w=function(t){return t instanceof E},_=Phaser.Utils.Objects.GetValue,x=function(){function n(t,r){e(this,n),this.setParent(t),this.isShutdown=!1,this.setEventEmitter(_(r,"eventEmitter",!0)),this.parent&&(this.parent===this.scene?this.scene.sys.events.once("shutdown",this.onEnvDestroy,this):this.parent===this.game?this.game.events.once("shutdown",this.onEnvDestroy,this):this.parent.once&&this.parent.once("destroy",this.onParentDestroy,this));}return r(n,[{key:"shutdown",value:function(t){this.isShutdown||(this.parent&&(this.parent===this.scene?this.scene.sys.events.off("shutdown",this.onEnvDestroy,this):this.parent===this.game?this.game.events.off("shutdown",this.onEnvDestroy,this):this.parent.once&&this.parent.off("destroy",this.onParentDestroy,this)),this.destroyEventEmitter(),this.parent=void 0,this.scene=void 0,this.game=void 0,this.isShutdown=!0);}},{key:"destroy",value:function(t){this.shutdown(t);}},{key:"onEnvDestroy",value:function(){this.destroy(!0);}},{key:"onParentDestroy",value:function(t,e){this.destroy(e);}},{key:"setParent",value:function(e){var n;return this.parent=e,this.scene=null==(n=e)||"object"!==t(n)?null:b(n)?n:n.scene&&b(n.scene)?n.scene:n.parent&&n.parent.scene&&b(n.parent.scene)?n.parent.scene:null,this.game=function(e){return null==e||"object"!==t(e)?null:w(e)?e:w(e.game)?e.game:b(e)?e.sys.game:b(e.scene)?e.scene.sys.game:void 0}(e),this}}]),n}();Object.assign(x.prototype,g);var O=d.uidKey,j=function(t){i(o,t);var n=h(o);function o(t,r){var i;return e(this,o),i=n.call(this,t,{eventEmitter:!1}),d.add(u(i),r),i.graph=null,i.type=void 0,i}return r(o,[{key:"shutdown",value:function(t){this.isShutdown||(this.graph&&this.graph.remove(this[O]),d.remove(this[O]),this.setGraph(null),a(s(o.prototype),"shutdown",this).call(this,t));}},{key:"setGraph",value:function(t){return this.graph=t,t||this.setType(void 0),this}},{key:"setType",value:function(t){return "string"==typeof t&&(t=P[t]),this.type=t,this}},{key:"isVertex",get:function(){return !!this.graph&&0===this.type}},{key:"isEdge",get:function(){return !!this.graph&&1===this.type}}]),o}(x);Object.assign(j.prototype,{});var P={vertex:0,edge:1},D=function(e){var n=t(e);return "number"===n||"string"===n},V=function(t){return D(t)?d.get(t):(t.hasOwnProperty("rexGraphItem")||(t.rexGraphItem=new j(t)),t.rexGraphItem)},k={"->":1,"<-":2,"<->":3},I=function(t){return null==t?null:d.get(t).parent},S={"breadth-first":0,bfs:0,"depth-first":1,dfs:1},C={getEdgeData:function(t,e){void 0===e&&(e=!1);var n=this.getObjUID(t);return e&&!this.edges.hasOwnProperty(n)&&(this.edges[n]={}),this.edges[n]},isEdge:function(t){var e=this.getObjUID(t);return this.edges.hasOwnProperty(e)},addEdge:function(t,e,n,r){if(this.isEdge(t))return this;void 0===r&&(r=3);var i=this.getObjUID(t),s=this.getEdgeData(i,!0);s.dir=r,s.vA=this.getObjUID(e),s.vB=this.getObjUID(n),V(t).setGraph(this),this.edgeCount++,this.addVertex(e).addVertex(n);var o=this.getVertexData(e,!0),u=this.getVertexData(n,!0);return "string"==typeof r&&(r=k(r)),1&r&&(o[i]=s.vB),2&r&&(u[i]=s.vA),this},removeEdge:function(t,e){if(this.isEdge(t))return this;void 0===e&&(e=!1);var n=this.getObjUID(t);return delete this.edges[n],this.edgeCount--,V(t).setGraph(null),e&&t.destroy&&gameObject.destroy(),this},getAllEdges:function(t){var e;for(var n in void 0===t&&(t=[]),this.edges)(e=I(n))&&t.push(e);return t},getEdgesOfVertex:function(t,e){void 0===e&&(e=[]);var n,r=this.getVertexData(t);if(!r)return e;for(var i in r)(n=I(i))&&e.push(n);return e},getEdgeLength:function(t){var e=this.getEdgeData(t);if(!e)return 0;var n,r,i,s,o,u,h=I(e.vA),a=I(e.vB);return h&&a?(n=h.x,r=h.y,i=a.x,s=a.y,o=n-i,u=r-s,Math.sqrt(o*o+u*u)):0},isInLoop:function(t){if(!this.isVertex(t))return !1;for(var e,n,r,i,s,o=this.getObjUID(t),u=[[o,null]],h={};u.length>0;){if(n=(e=u.pop())[0],r=e[1],n===o&&null!==r)return !0;for(r in null!==r&&(h[r]=!0),i=this.getVertexData(n))h.hasOwnProperty(r)||(s=i[r],u.push([s,r]));}return !1},getVertexData:function(t,e){void 0===e&&(e=!1);var n=this.getObjUID(t);return e&&!this.vertices.hasOwnProperty(n)&&(this.vertices[n]={}),this.vertices[n]},isVertex:function(t){var e=this.getObjUID(t);return this.vertices.hasOwnProperty(e)},addVertex:function(t){return this.isVertex(t)||(this.getVertexData(t,!0),V(t).setGraph(this),this.vertexCount++),this},addVertices:function(t){for(var e=0,n=t.length;e<n;e++)this.addVertex(t[e]);return this},removeVertex:function(t,e,n){if(!this.isVertex(t))return this;void 0===e&&(e=!1),void 0===n&&(n=!0);var r=this.getObjUID(t);if(n){var i=this.getVertexData(r);for(var s in i)this.removeEdge(s,e);}return delete this.vertices[r],this.vertexCount--,V(t).setGraph(null),e&&t.destroy&&gameObject.destroy(),this},removeAllVertices:function(t){for(var e in this.vertices)this.removeVertex(e,t);return this},getAllVertices:function(t){var e;for(var n in void 0===t&&(t=[]),this.vertices)(e=I(n))&&t.push(e);return t},getVerticesOfEdge:function(t,e){void 0===e&&(e=[]);var n,r=this.getEdgeData(t);return r?((n=I(r.vA))&&e.push(n),(n=I(r.vB))&&e.push(n),e):e},getOppositeVertex:function(t,e){var n=this.getVertexData(t);if(n){var r=this.getObjUID(e);if(r)return I(n[r])}},getAllConnectedVertices:function(t,e,n){if(void 0===e&&(e=[]),"string"==typeof n&&(n=S[n]),void 0===n&&(n=0),!this.isVertex(t))return e;for(var r,i,s=this.getObjUID(t),o=0===n,u=[s],h={};u.length>0;)if(r=o?u.shift():u.pop(),!h.hasOwnProperty(r))for(var a in h[r]=!0,r!==s&&e.push(I(r)),this.getVertexData(r))i=this.getOppositeVertex(r,a),h.hasOwnProperty(i)||u.push(i);return e},getNeighborVertices:function(t,e){void 0===e&&(e=[]);var n,r=this.getVertexData(t);if(r)for(var i in r)(n=I(r[i]))&&e.push(n);return e},areNeighborVertices:function(t,e){var n=this.getObjUID(t),r=this.getObjUID(e);if(null!=n&&null!=r){var i=this.getVertexData(t);for(var s in r=parseInt(r),i)if(i[s]===r)return !0}return !1}},A=d.uidKey,L=function(t){i(o,t);var n=h(o);function o(t){var r;return e(this,o),(r=n.call(this)).isShutdown=!1,r.scene=t,r.vertices={},r.edges={},r.vertexCount=0,r.edgeCount=0,r.boot(),r}return r(o,[{key:"boot",value:function(){this.scene&&this.scene.sys.events.once("shutdown",this.destroy,this);}},{key:"shutdown",value:function(t){if(!this.isShutdown)return this.scene&&this.scene.sys.events.off("shutdown",this.destroy,this),this.clear(),a(s(o.prototype),"shutdown",this).call(this),this.scene=void 0,this.vertices=void 0,this.edges=void 0,this.vertexCount=0,this.edgeCount=0,this.isShutdown=!0,this}},{key:"destroy",value:function(t){this.isShutdown||(this.emit("destroy"),this.shutdown(t));}},{key:"exists",value:function(t){return this.isEdge(t)||this.isVertex(t)}},{key:"remove",value:function(t){return this.isEdge(t)?this.removeEdge(t):this.isVertex(t)&&this.removeVertex(t),this}},{key:"clear",value:function(t){return void 0===t&&(t=!0),this.removeAllVertices(t),this}},{key:"getObjUID",value:function(t){return function(t){return D(t)?t:V(t)[A]}(t)}}]),o}(l);Object.assign(L.prototype,C);var U=function(t){return null==t||""===t||0===t.length};return f.register("graph",(function(t){return new L(this.scene,t)})),function(e,n,r,i){if(void 0===i&&(i="."),"object"===t(e)){if(U(n)){if(null==r)return;"object"===t(r)&&(e=r);}else {"string"==typeof n&&(n=n.split(i));var s=n.pop(),o=function(e,n,r){var i=e;if(U(n));else {var s;"string"==typeof n&&(n=n.split("."));for(var o=0,u=n.length;o<u;o++){var h;null!=i[s=n[o]]&&"object"===t(i[s])||(h=o===u-1?void 0===r?{}:r:{},i[s]=h),i=i[s];}}return i}(e,n);o[s]=r;}}}(window,"RexPlugins.Graph.Graph",L),function(t){i(s,Phaser.Plugins.ScenePlugin);var n=h(s);function s(t,r){var i;return e(this,s),(i=n.call(this,t,r)).add=new f(t),i}return r(s)}()}));