mudlet-map-renderer 0.6.4-konva → 0.6.6-konva
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ExitRenderer.d.ts +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +229 -236
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/ExitRenderer.d.ts
CHANGED
|
@@ -21,6 +21,6 @@ export default class ExitRenderer {
|
|
|
21
21
|
stroke: string;
|
|
22
22
|
strokeWidth: number;
|
|
23
23
|
}>[];
|
|
24
|
-
renderInnerExits(room: MapData.Room
|
|
24
|
+
renderInnerExits(room: MapData.Room): import('konva/lib/Group').Group[];
|
|
25
25
|
renderDoor(points: number[], type: 1 | 2 | 3, color?: string): import('konva/lib/shapes/Rect').Rect;
|
|
26
26
|
}
|
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p=require("konva"),_=["north","south","east","west","northeast","northwest","southeast","southwest"],C={north:"n",south:"s",east:"e",west:"w",northeast:"ne",northwest:"nw",southeast:"se",southwest:"sw",up:"u",down:"d",in:"i",out:"o"},N={north:{x:0,y:-1},south:{x:0,y:1},east:{x:1,y:0},west:{x:-1,y:0},northeast:{x:1,y:-1},northwest:{x:-1,y:-1},southeast:{x:1,y:1},southwest:{x:-1,y:1}},P=["north","south","east","west","northeast","northwest","southeast","southwest"],G={north:"south",south:"north",east:"west",west:"east",northeast:"southwest",northwest:"southeast",southeast:"northwest",southwest:"northeast"};function F(l){return l?Object.prototype.hasOwnProperty.call(N,l):!1}function f(l,e,t,s=1){if(!F(t))return{x:l,y:e};const r=N[t];return{x:l+r.x*s,y:e+r.y*s}}const E={OPEN_DOOR:"rgb(10, 155, 10)",CLOSED_DOOR:"rgb(226, 205, 59)",LOCKED_DOOR:"rgb(155, 10, 10)"},B={1:"north",2:"northeast",3:"northwest",4:"east",5:"west",6:"south",7:"southeast",8:"southwest",9:"up",10:"down",11:"in",12:"out"},H=["up","down","in","out"];function q(l){switch(l){case 1:return E.OPEN_DOOR;case 2:return E.CLOSED_DOOR;default:return E.LOCKED_DOOR}}class K{constructor(e){this.mapReader=e}render(e){return this.renderWithColor(e,c.lineColor)}renderWithColor(e,t){return e.aDir&&e.bDir?this.renderTwoWayExit(e,t):this.renderOneWayExit(e,t)}renderTwoWayExit(e,t){const s=this.mapReader.getRoom(e.a),r=this.mapReader.getRoom(e.b);if(!s||!r||!e.aDir||!e.bDir)return;const i=new p.Group,n=[];if(n.push(...Object.values(f(s.x,s.y,e.aDir,c.roomSize/2))),n.push(...Object.values(f(r.x,r.y,e.bDir,c.roomSize/2))),s.doors[C[e.aDir]]||r.doors[C[e.bDir]]){const h=this.renderDoor(n,s.doors[C[e.aDir]]??r.doors[C[e.bDir]],t);i.add(h)}const o=new p.Line({points:n,stroke:t,strokeWidth:.025});return i.add(o),i}renderOneWayExit(e,t){const s=e.aDir?this.mapReader.getRoom(e.a):this.mapReader.getRoom(e.b),r=e.aDir?this.mapReader.getRoom(e.b):this.mapReader.getRoom(e.a),i=e.aDir?e.aDir:e.bDir;if(!s||!r)return;if(s.area!=r.area&&i)return this.renderAreaExit(s,i,t);let n={x:r.x,y:r.y};(r.area!==s.area||r.z!==s.z)&&(n=f(s.x,s.y,i,c.roomSize/2));const o=f(s.x,s.y,i,.3),h=o.x-(o.x-n.x)/2,g=o.y-(o.y-n.y)/2,u=new p.Group,a=[];a.push(...Object.values(f(s.x,s.y,i,c.roomSize/2))),a.push(n.x,n.y);const d=new p.Line({points:a,stroke:t,strokeWidth:.025,dashEnabled:!0,dash:[.1,.05]});u.add(d);const m=new p.Arrow({points:[a[0],a[1],h,g],pointerLength:.5,pointerWidth:.35,strokeWidth:.035,stroke:t,fill:t,dashEnabled:!0,dash:[.1,.05]});return u.add(m),u}renderAreaExit(e,t,s){const r=f(e.x,e.y,t,c.roomSize/2),i=f(e.x,e.y,t,c.roomSize*1.5),n=s??this.mapReader.getColorValue(e.env);return new p.Arrow({points:[r.x,r.y,i.x,i.y],pointerLength:.3,pointerWidth:.3,strokeWidth:.035,stroke:n,fill:n})}renderSpecialExits(e,t){return Object.entries(e.customLines).map(([s,r])=>{const i=[e.x,e.y];r.points.reduce((u,a)=>(u.push(a.x,-a.y),u),i);const n=r.attributes.arrow?p.Arrow:p.Line,o=t??`rgb(${r.attributes.color.r}, ${r.attributes.color.g}, ${r.attributes.color.b})`,h=new n({points:i,strokeWidth:.025,stroke:o,fill:t??`rgb(${r.attributes.color.r}, ${r.attributes.color.g} , ${r.attributes.color.b})`,pointerLength:.3,pointerWidth:.2});let g=r.attributes.style;return g==="dot line"?(h.dash([.05,.05]),h.dashOffset(.1)):g==="dash line"?h.dash([.4,.2]):g==="solid line"||g!==void 0&&console.log("Brak opisu stylu: "+g),h})}renderStubs(e,t=c.lineColor){return e.stubs.map(s=>{const r=B[s],i=f(e.x,e.y,r,c.roomSize/2),n=f(e.x,e.y,r,c.roomSize/2+.5),o=[i.x,i.y,n.x,n.y];return new p.Line({points:o,stroke:t,strokeWidth:.025})})}renderInnerExits(e,t){return H.map(s=>{if(e.exits[s]){const r=new p.Group,i=new p.RegularPolygon({x:e.x,y:e.y,sides:3,fill:t?this.mapReader.getSymbolColor(e.env,.2):this.mapReader.getSymbolColor(e.env,.6),stroke:t??this.mapReader.getSymbolColor(e.env),strokeWidth:.025,radius:c.roomSize/5,scaleX:1.4,scaleY:.8});r.add(i);let n=e.doors[s];if(n!==void 0)if(t)i.stroke(t);else switch(n){case 1:i.stroke(E.OPEN_DOOR);break;case 2:i.stroke(E.CLOSED_DOOR);break;default:i.stroke(E.LOCKED_DOOR)}switch(s){case"up":i.position(f(e.x,e.y,"south",c.roomSize/4));break;case"down":i.rotation(180),i.position(f(e.x,e.y,"north",c.roomSize/4));break;case"in":const o=i.clone();o.rotation(-90),o.position(f(e.x,e.y,"east",c.roomSize/4)),r.add(o),i.rotation(90),i.position(f(e.x,e.y,"west",c.roomSize/4));break;case"out":const h=i.clone();h.rotation(90),h.position(f(e.x,e.y,"east",c.roomSize/4)),r.add(h),i.rotation(-90),i.position(f(e.x,e.y,"west",c.roomSize/4));break}return r}}).filter(s=>s!==void 0)}renderDoor(e,t,s){const r={x:e[0]+(e[2]-e[0])/2,y:e[1]+(e[3]-e[1])/2};return new p.Rect({x:r.x-c.roomSize/4,y:r.y-c.roomSize/4,width:c.roomSize/2,height:c.roomSize/2,stroke:s??q(t),strokeWidth:.025})}}class Y{constructor(e,t){this.rooms=[],this.labels=[],this.rooms=e,this.bounds=this.createBounds(),this.labels=t}getRooms(){return this.rooms}getLabels(){return this.labels}getBounds(){return this.bounds}createBounds(){return this.rooms.reduce((e,t)=>({minX:Math.min(e.minX,t.x),maxX:Math.max(e.maxX,t.x),minY:Math.min(e.minY,t.y),maxY:Math.max(e.maxY,t.y)}),{minX:Number.POSITIVE_INFINITY,maxX:Number.NEGATIVE_INFINITY,minY:Number.POSITIVE_INFINITY,maxY:Number.NEGATIVE_INFINITY})}}class L{constructor(e){this.planes={},this.exits=new Map,this.version=0,this.area=e,this.planes=this.createPlanes(),this.createExits()}getAreaName(){return this.area.areaName}getAreaId(){return parseInt(this.area.areaId)}getVersion(){return this.version}markDirty(){this.version++}getPlane(e){return this.planes[e]}getPlanes(){return Object.values(this.planes)}getRooms(){return this.area.rooms}getLinkExits(e){return Array.from(this.exits.values()).filter(t=>t.zIndex.includes(e))}createPlanes(){const e=this.area.rooms.reduce((t,s)=>(t[s.z]||(t[s.z]=[]),t[s.z].push(s),t),{});return Object.entries(e).reduce((t,[s,r])=>(t[+s]=new Y(r,this.area.labels.filter(i=>i.Z===+s)),t),{})}createExits(){this.area.rooms.forEach(e=>{Object.entries(e.exits).filter(([t,s])=>_.indexOf(t)>-1&&!e.customLines.hasOwnProperty(C[t])).forEach(([t,s])=>this.createHalfExit(e.id,s,e.z,t))})}createHalfExit(e,t,s,r){if(e===t)return;const i=Math.min(e,t),n=Math.max(e,t),o=`${i}-${n}`;let h=this.exits.get(o);h||(h={a:i,b:n,zIndex:[s]}),i==e?h.aDir=r:h.bDir=r,h.zIndex.push(s),this.exits.set(o,h)}}class z extends Y{constructor(e,t){super(e.getRooms(),e.getLabels()),this.basePlane=e,this.visitedRooms=t}getRooms(){return this.basePlane.getRooms().filter(e=>this.visitedRooms.has(e.id))}getLabels(){return this.basePlane.getLabels()}getBounds(){const e=this.getRooms();return e.length?e.reduce((t,s)=>({minX:Math.min(t.minX,s.x),maxX:Math.max(t.maxX,s.x),minY:Math.min(t.minY,s.y),maxY:Math.max(t.maxY,s.y)}),{minX:Number.POSITIVE_INFINITY,maxX:Number.NEGATIVE_INFINITY,minY:Number.POSITIVE_INFINITY,maxY:Number.NEGATIVE_INFINITY}):this.basePlane.getBounds()}}class O extends L{constructor(e,t){super(e),this.planeCache=new WeakMap,this.visitedRooms=t instanceof Set?t:new Set(t??[]),this.areaRoomIds=new Set(e.rooms.map(s=>s.id))}getPlane(e){const t=super.getPlane(e);if(!t)return t;let s=this.planeCache.get(t);return s||(s=new z(t,this.visitedRooms),this.planeCache.set(t,s)),s}getPlanes(){return super.getPlanes().map(e=>{let t=this.planeCache.get(e);return t||(t=new z(e,this.visitedRooms),this.planeCache.set(e,t)),t})}getLinkExits(e){return super.getLinkExits(e).filter(t=>this.visitedRooms.has(t.a)||this.visitedRooms.has(t.b))}getVisitedRoomCount(){return super.getRooms().reduce((e,t)=>e+(this.visitedRooms.has(t.id)?1:0),0)}getTotalRoomCount(){return this.areaRoomIds.size}hasVisitedRoom(e){return this.areaRoomIds.has(e)&&this.visitedRooms.has(e)}getVisitedRoomIds(){return super.getRooms().filter(e=>this.visitedRooms.has(e.id)).map(e=>e.id)}addVisitedRoom(e){const t=this.visitedRooms.has(e);this.visitedRooms.add(e);const s=!t&&this.areaRoomIds.has(e);return s&&this.markDirty(),s}addVisitedRooms(e){let t=0;for(const s of e){const r=this.visitedRooms.has(s);this.visitedRooms.add(s),!r&&this.areaRoomIds.has(s)&&t++}return t>0&&this.markDirty(),t}}class Q{constructor(e,t){this.paths=[],this.mapReader=e,this.overlayLayer=t}renderPath(e,t,s,r="#66E64D"){if(t===void 0||s===void 0)return;const i=e.map(a=>this.mapReader.getRoom(a)).filter(a=>a!==void 0),n=[];let o=null;const h=()=>{o&&(o.length<4&&n.pop(),o=null)},g=()=>(o||(o=[],n.push(o)),o);i.forEach((a,d)=>{if(!this.isRoomVisible(a,t,s))return;const m=d>0?i[d-1]:void 0,y=d<i.length-1?i[d+1]:void 0;if(this.isRoomVisible(m,t,s))g();else{h();const R=g();if(m){const b=this.getDirectionTowards(a,m);if(b){const k=f(a.x,a.y,b,c.roomSize);R.push(k.x,k.y)}}}if(o==null||o.push(a.x,a.y),!this.isRoomVisible(y,t,s)&&y){const R=this.getDirectionTowards(a,y);if(R){const b=f(a.x,a.y,R,c.roomSize);o==null||o.push(b.x,b.y)}h()}}),h();const u=n.filter(a=>a.length>=4).map(a=>new p.Line({points:a,stroke:r,strokeWidth:.1}));return u.forEach(a=>{this.overlayLayer.add(a),this.paths.push(a)}),u[0]}clearPaths(){this.paths.forEach(e=>{e.destroy()}),this.paths=[]}isRoomVisible(e,t,s){return e?e.area===t&&e.z===s:!1}getDirectionTowards(e,t){for(const s of P)if(e.exits[s]===t.id)return s;for(const s of P)if(t.exits[s]===e.id)return G[s]}}const W=.6,S=75,J="rgb(225, 255, 225)",D="rgb(120, 72, 0)",v=class v{};v.roomSize=W,v.lineColor=J,v.instantMapMove=!1,v.highlightCurrentRoom=!0;let c=v;class U{constructor(e,t){this.highlights=new Map,this.currentZoom=1,this.currentRoomOverlay=[],this.stage=new p.Stage({container:e,width:e.clientWidth,height:e.clientHeight,draggable:!0}),window.addEventListener("resize",()=>{this.onResize(e)}),e.addEventListener("resize",()=>{this.onResize(e)}),this.linkLayer=new p.Layer({listening:!1}),this.stage.add(this.linkLayer),this.roomLayer=new p.Layer,this.stage.add(this.roomLayer),this.overlayLayer=new p.Layer({listening:!1}),this.stage.add(this.overlayLayer),this.positionLayer=new p.Layer({listening:!1}),this.stage.add(this.positionLayer),this.mapReader=t,this.exitRenderer=new K(t),this.pathRenderer=new Q(t,this.overlayLayer),this.initScaling(1.1)}onResize(e){this.stage.width(e.clientWidth),this.stage.height(e.clientHeight),this.currentRoomId&&this.centerOnRoom(this.mapReader.getRoom(this.currentRoomId),!1),this.stage.batchDraw()}initScaling(e){p.hitOnDragEnabled=!0;let t,s=!1,r=!1;this.stage.on("touchstart",i=>{const n=i.evt.touches;n&&n.length>1?(r=!0,this.stage.isDragging()&&(this.stage.stopDrag(),s=!0),this.stage.draggable(!1)):(r=!1,this.stage.draggable(!0))}),this.stage.on("touchend touchcancel",i=>{t=void 0;const n=i.evt.touches;(!n||n.length<=1)&&(r=!1,this.stage.draggable(!0))}),this.stage.on("wheel",i=>{i.evt.preventDefault();const n=this.stage.scaleX(),o=this.stage.getPointerPosition();if(!o)return;const h={x:(o.x-this.stage.x())/n,y:(o.y-this.stage.y())/n};let g=i.evt.deltaY>0?-1:1;i.evt.ctrlKey&&(g=-g);const u=g>0?this.currentZoom*e:this.currentZoom/e,a=u*S,d=this.setZoom(u),m={x:o.x-h.x*a,y:o.y-h.y*a};this.stage.position(m),d&&this.emitZoomChangeEvent()}),this.stage.on("touchmove",i=>{const n=i.evt.touches,o=n==null?void 0:n[0],h=n==null?void 0:n[1];if(h||r&&(r=!1,this.stage.draggable(!0)),o&&!h&&s&&!this.stage.isDragging()&&(this.stage.startDrag(),s=!1),!o||!h){t=void 0;return}i.evt.preventDefault(),this.stage.isDragging()&&(this.stage.stopDrag(),s=!0),r||(r=!0,this.stage.draggable(!1));const g=this.stage.container().getBoundingClientRect(),u={x:o.clientX-g.left,y:o.clientY-g.top},a={x:h.clientX-g.left,y:h.clientY-g.top},d=Math.hypot(u.x-a.x,u.y-a.y);if(t===void 0){t=d;return}if(t===0)return;const m=this.stage.scaleX(),y=this.stage.x(),w=this.stage.y(),x={x:this.stage.width()/2,y:this.stage.height()/2},R={x:(x.x-y)/m,y:(x.y-w)/m},b=this.currentZoom*(d/t),k=this.setZoom(b),I=this.stage.scaleX(),j={x:x.x-R.x*I,y:x.y-R.y*I};this.stage.position(j),this.stage.batchDraw(),t=d,k&&this.emitZoomChangeEvent()})}drawArea(e,t){const s=this.mapReader.getArea(e);if(!s)return;const r=s.getPlane(t);r&&(this.currentArea=e,this.currentAreaInstance=s,this.currentZIndex=t,this.currentAreaVersion=s.getVersion(),this.clearCurrentRoomOverlay(),this.roomLayer.destroyChildren(),this.linkLayer.destroyChildren(),this.stage.scale({x:S*this.currentZoom,y:S*this.currentZoom}),this.renderLabels(r.getLabels()),this.renderExits(s.getLinkExits(t)),this.renderRooms(r.getRooms()??[]),this.refreshHighlights(),this.stage.batchDraw())}emitRoomContextEvent(e,t,s){const r=this.stage.container(),i=r.getBoundingClientRect(),n={roomId:e,position:{x:t-i.left,y:s-i.top}},o=new CustomEvent("roomcontextmenu",{detail:n});r.dispatchEvent(o)}emitZoomChangeEvent(){const e=new CustomEvent("zoom",{detail:{zoom:this.currentZoom}});this.stage.container().dispatchEvent(e)}setZoom(e){return this.currentZoom===e?!1:(this.currentZoom=e,this.stage.scale({x:S*e,y:S*e}),!0)}getZoom(){return this.currentZoom}getCurrentArea(){return this.currentArea?this.mapReader.getArea(this.currentArea):void 0}setPosition(e){const t=this.mapReader.getRoom(e);if(!t)return;const s=this.mapReader.getArea(t.area),r=s==null?void 0:s.getVersion();let i=this.currentArea!==t.area||this.currentZIndex!==t.z;(this.currentArea!==t.area||this.currentZIndex!==t.z||r!==void 0&&this.currentAreaVersion!==r||s!==void 0&&this.currentAreaInstance!==s)&&this.drawArea(t.area,t.z),this.positionRender||(this.positionRender=new p.Circle({x:t.x,y:t.y,radius:W*.85,stroke:"rgb(0, 229, 178)",strokeWidth:.1,dash:[.05,.05],dashEnabled:!0}),this.positionLayer.add(this.positionRender)),this.centerOnRoom(t,i),this.updateCurrentRoomOverlay(t)}renderPath(e,t){return this.pathRenderer.renderPath(e,this.currentArea,this.currentZIndex,t)}clearPaths(){this.pathRenderer.clearPaths()}renderHighlight(e,t){const s=this.mapReader.getRoom(e);if(!s)return;const r=this.highlights.get(e);r!=null&&r.shape&&(r.shape.destroy(),delete r.shape);const i={color:t,area:s.area,z:s.z};if(this.highlights.set(e,i),s.area===this.currentArea&&s.z===this.currentZIndex){const n=this.createHighlightShape(s,t);return this.overlayLayer.add(n),i.shape=n,this.overlayLayer.batchDraw(),n}return i.shape}clearHighlights(){this.highlights.forEach(({shape:e})=>e==null?void 0:e.destroy()),this.highlights.clear(),this.overlayLayer.batchDraw()}refreshHighlights(){this.highlights.forEach((e,t)=>{var i;if((i=e.shape)==null||i.destroy(),delete e.shape,e.area!==this.currentArea||e.z!==this.currentZIndex)return;const s=this.mapReader.getRoom(t);if(!s)return;const r=this.createHighlightShape(s,e.color);this.overlayLayer.add(r),e.shape=r}),this.overlayLayer.batchDraw()}createHighlightShape(e,t){return new p.Circle({x:e.x,y:e.y,radius:c.roomSize*.9,stroke:t,strokeWidth:.15,dash:[.1,.05],dashEnabled:!0,listening:!1})}centerOnRoom(e,t=!1){var g;this.currentRoomId=e.id;const s={x:e.x,y:e.y};(g=this.positionRender)==null||g.position(e);const i=this.stage.getAbsoluteTransform().point(s),n={x:this.stage.width()/2,y:this.stage.height()/2},o=n.x-i.x,h=n.y-i.y;this.currentTransition&&(this.currentTransition.pause(),this.currentTransition.destroy(),delete this.currentTransition),console.log(c.instantMapMove),t||c.instantMapMove?this.stage.position({x:this.stage.x()+o,y:this.stage.y()+h}):(this.currentTransition=new p.Tween({node:this.stage,x:this.stage.x()+o,y:this.stage.y()+h,duration:.2,easing:p.Easings.EaseInOut}),this.currentTransition.play())}renderRooms(e){e.forEach(t=>{const s=new p.Group({x:t.x-c.roomSize/2,y:t.y-c.roomSize/2}),r=new p.Rect({x:0,y:0,width:c.roomSize,height:c.roomSize,fill:this.mapReader.getColorValue(t.env),strokeWidth:.025,stroke:c.lineColor}),i=(a,d)=>this.emitRoomContextEvent(t.id,a,d);s.on("mouseenter",()=>{this.stage.container().style.cursor="pointer"}),s.on("mouseleave",()=>{this.stage.container().style.cursor="auto"}),s.on("contextmenu",a=>{a.evt.preventDefault();const d=a.evt;i(d.clientX,d.clientY)});let n,o,h;const g=()=>{h!==void 0&&(this.stage.draggable(h),h=void 0)},u=()=>{n!==void 0&&(window.clearTimeout(n),n=void 0),o=void 0,g()};s.on("touchstart",a=>{var m;if(u(),a.evt.touches&&a.evt.touches.length>1)return;const d=(m=a.evt.touches)==null?void 0:m[0];d&&(o={clientX:d.clientX,clientY:d.clientY},h=this.stage.draggable(),this.stage.draggable(!1),n=window.setTimeout(()=>{o&&i(o.clientX,o.clientY),u()},500))}),s.on("touchend",u),s.on("touchmove",a=>{var R;if(!o)return;const d=(R=a.evt.touches)==null?void 0:R[0];if(!d){u();return}const m=d.clientX-o.clientX,y=d.clientY-o.clientY,w=m*m+y*y,x=10;if(w>x*x){const b=h;u(),b&&this.stage.startDrag()}}),s.on("touchcancel",u),s.add(r),this.renderSymbol(t,s),this.roomLayer.add(s),this.exitRenderer.renderSpecialExits(t).forEach(a=>{this.linkLayer.add(a)}),this.exitRenderer.renderStubs(t).forEach(a=>{this.linkLayer.add(a)}),this.exitRenderer.renderInnerExits(t).forEach(a=>{this.roomLayer.add(a)})})}clearCurrentRoomOverlay(){this.currentRoomOverlay.forEach(e=>e.destroy()),this.currentRoomOverlay=[],this.overlayLayer.batchDraw()}updateCurrentRoomOverlay(e){if(this.clearCurrentRoomOverlay(),e.area!==this.currentArea||e.z!==this.currentZIndex){this.overlayLayer.batchDraw();return}const t=new Map;t.set(e.id,e);const s=[],r=this.currentAreaInstance instanceof O?this.currentAreaInstance:void 0;this.currentAreaInstance&&this.currentZIndex!==void 0&&this.currentAreaInstance.getLinkExits(this.currentZIndex).filter(h=>h.a===e.id||h.b===e.id).forEach(h=>{const g=c.highlightCurrentRoom?this.exitRenderer.renderWithColor(h,D):this.exitRenderer.render(h);g&&s.push(g);const u=h.a===e.id?h.b:h.a,a=this.mapReader.getRoom(u),d=!r||r.hasVisitedRoom(u);a&&a.area===this.currentArea&&a.z===this.currentZIndex&&d&&t.set(a.id,a)});const i=c.highlightCurrentRoom?D:void 0;this.exitRenderer.renderSpecialExits(e,i).forEach(o=>{s.push(o)}),(c.highlightCurrentRoom?this.exitRenderer.renderStubs(e,D):this.exitRenderer.renderStubs(e)).forEach(o=>{s.push(o)}),Object.values(e.specialExits).forEach(o=>{const h=this.mapReader.getRoom(o);t.set(o,h)}),s.forEach(o=>{this.overlayLayer.add(o),this.currentRoomOverlay.push(o)}),t.forEach((o,h)=>{const g=h===e.id,u=this.createOverlayRoomGroup(o,{stroke:g&&c.highlightCurrentRoom?D:c.lineColor,fillEnabled:!0,strokeEnabled:g?c.highlightCurrentRoom:!1});this.overlayLayer.add(u),this.currentRoomOverlay.push(u);const a=g&&c.highlightCurrentRoom?D:void 0;this.exitRenderer.renderInnerExits(o,a).forEach(d=>{this.overlayLayer.add(d),this.currentRoomOverlay.push(d)})}),this.overlayLayer.batchDraw()}createOverlayRoomGroup(e,t){const s=new p.Group({x:e.x-c.roomSize/2,y:e.y-c.roomSize/2,listening:!1}),r=new p.Rect({x:0,y:0,width:c.roomSize,height:c.roomSize,fill:this.mapReader.getColorValue(e.env),stroke:t.stroke,strokeWidth:.025,strokeEnabled:t.strokeEnabled});return t.fillEnabled||r.fillEnabled(!1),s.add(r),this.renderSymbol(e,s),s}renderSymbol(e,t){if(e.roomChar!==void 0){const s=new p.Text({x:0,y:0,text:e.roomChar,fontSize:.45,fontStyle:"bold",fill:this.mapReader.getSymbolColor(e.env),align:"center",verticalAlign:"middle",width:c.roomSize,height:c.roomSize});t.add(s)}}renderExits(e){e.forEach(t=>{const s=this.exitRenderer.render(t);s&&this.linkLayer.add(s)})}renderLabels(e){e.forEach(t=>{if(!t.pixMap)return;const s=new Image;s.src=`data:image/png;base64,${t.pixMap}`;const r=new p.Image({x:t.X,y:-t.Y,width:t.Width,height:t.Height,image:s});this.linkLayer.add(r)})}}const V={rgbValue:"rgb(114, 1, 0)",symbolColor:[225,225,225]};function M(l){const e=l[0]/255,t=l[1]/255,s=l[2]/255,r=Math.max(e,t,s),i=Math.min(e,t,s);return(r+i)/2}class ee{constructor(e,t){this.rooms={},this.areas={},this.areaSources={},this.explorationEnabled=!1,this.colors={},e.forEach(s=>{s.rooms.forEach(i=>{i.y=-i.y,this.rooms[i.id]=i});const r=parseInt(s.areaId);this.areas[r]=new L(s),this.areaSources[r]=s}),this.colors=t.reduce((s,r)=>({...s,[r.envId]:{rgb:r.colors,rgbValue:`rgb(${r.colors.join(",")}`,symbolColor:M(r.colors)>.41?[25,25,25]:[225,255,255],symbolColorValue:M(r.colors)>.41?"rgb(25,25,25)":"rgb(225,255,255)"}}),{})}getArea(e){return this.areas[e]}getExplorationArea(e){const t=this.areas[e];if(t instanceof O)return t}getAreas(){return Object.values(this.areas)}getRooms(){return Object.values(this.rooms)}getRoom(e){return this.rooms[e]}ensureVisitedRooms(){return this.visitedRooms||(this.visitedRooms=new Set),this.visitedRooms}applyExplorationDecoration(){this.visitedRooms&&Object.entries(this.areaSources).forEach(([e,t])=>{const s=parseInt(e,10);this.areas[s]=new O(t,this.visitedRooms)})}decorateWithExploration(e){return e!==void 0?this.setVisitedRooms(e):this.ensureVisitedRooms(),this.applyExplorationDecoration(),this.explorationEnabled=!0,this.visitedRooms}getVisitedRooms(){return this.visitedRooms}clearExplorationDecoration(){Object.entries(this.areaSources).forEach(([e,t])=>{const s=parseInt(e,10);this.areas[s]=new L(t)}),this.explorationEnabled=!1}isExplorationEnabled(){return this.explorationEnabled}setVisitedRooms(e){return this.visitedRooms=e instanceof Set?e:new Set(e),this.explorationEnabled&&this.applyExplorationDecoration(),this.visitedRooms}addVisitedRoom(e){if(this.explorationEnabled){const r=this.getRoom(e);if(r){const i=this.getExplorationArea(r.area);if(i)return i.addVisitedRoom(e)}}const t=this.ensureVisitedRooms(),s=t.has(e);return t.add(e),!s}addVisitedRooms(e){const t=this.ensureVisitedRooms();let s=0;for(const r of e){if(this.explorationEnabled){const n=this.getRoom(r);if(n){const o=this.getExplorationArea(n.area);if(o){o.addVisitedRoom(r)&&s++;continue}}}const i=t.has(r);t.add(r),i||s++}return s}hasVisitedRoom(e){var t;return((t=this.visitedRooms)==null?void 0:t.has(e))??!1}getColorValue(e){var t;return((t=this.colors[e])==null?void 0:t.rgbValue)??V.rgbValue}getSymbolColor(e,t){var n;const s=((n=this.colors[e])==null?void 0:n.symbolColor)??V.symbolColor,r=Math.min(Math.max(t??1,0),1),i=s.join(",");return r!=1?`rgba(${i}, ${r})`:`rgba(${i})`}}function te(l){return l&&l.__esModule&&Object.prototype.hasOwnProperty.call(l,"default")?l.default:l}class se{constructor(){this.keys=new Set,this.queue=[]}sort(){this.queue.sort((e,t)=>e.priority-t.priority)}set(e,t){const s=Number(t);if(isNaN(s))throw new TypeError('"priority" must be a number');return this.keys.has(e)?this.queue.map(r=>(r.key===e&&Object.assign(r,{priority:s}),r)):(this.keys.add(e),this.queue.push({key:e,priority:s})),this.sort(),this.queue.length}next(){const e=this.queue.shift();return this.keys.delete(e.key),e}isEmpty(){return this.queue.length===0}has(e){return this.keys.has(e)}get(e){return this.queue.find(t=>t.key===e)}}var re=se;function Z(l,e){const t=new Map;for(const[s,r]of l)s!==e&&r instanceof Map?t.set(s,Z(r,e)):s!==e&&t.set(s,r);return t}var ie=Z;function oe(l){const e=Number(l);return!(isNaN(e)||e<=0)}function X(l){const e=new Map;return Object.keys(l).forEach(s=>{const r=l[s];if(r!==null&&typeof r=="object"&&!Array.isArray(r))return e.set(s,X(r));if(!oe(r))throw new Error(`Could not add node at key "${s}", make sure it's a valid node`,r);return e.set(s,Number(r))}),e}var ne=X;function $(l){if(!(l instanceof Map))throw new Error(`Invalid graph: Expected Map instead found ${typeof l}`);l.forEach((e,t)=>{if(typeof e=="object"&&e instanceof Map){$(e);return}if(typeof e!="number"||e<=0)throw new Error(`Values must be numbers greater than 0. Found value ${e} at ${t}`)})}var ae=$;const he=re,ce=ie,A=ne,T=ae;class le{constructor(e){e instanceof Map?(T(e),this.graph=e):e?this.graph=A(e):this.graph=new Map}addNode(e,t){let s;return t instanceof Map?(T(t),s=t):s=A(t),this.graph.set(e,s),this}addVertex(e,t){return this.addNode(e,t)}removeNode(e){return this.graph=ce(this.graph,e),this}path(e,t,s={}){if(!this.graph.size)return s.cost?{path:null,cost:0}:null;const r=new Set,i=new he,n=new Map;let o=[],h=0,g=[];if(s.avoid&&(g=[].concat(s.avoid)),g.includes(e))throw new Error(`Starting node (${e}) cannot be avoided`);if(g.includes(t))throw new Error(`Ending node (${t}) cannot be avoided`);for(i.set(e,0);!i.isEmpty();){const u=i.next();if(u.key===t){h=u.priority;let d=u.key;for(;n.has(d);)o.push(d),d=n.get(d);break}r.add(u.key),(this.graph.get(u.key)||new Map).forEach((d,m)=>{if(r.has(m)||g.includes(m))return null;if(!i.has(m))return n.set(m,u.key),i.set(m,u.priority+d);const y=i.get(m).priority,w=u.priority+d;return w<y?(n.set(m,u.key),i.set(m,w)):null})}return o.length?(s.trim?o.shift():o=o.concat([e]),s.reverse||(o=o.reverse()),s.cost?{path:o,cost:h}:o):s.cost?{path:null,cost:0}:null}shortestPath(...e){return this.path(...e)}}var ue=le;const de=te(ue),ge={1:"north",2:"northeast",3:"northwest",4:"east",5:"west",6:"south",7:"southeast",8:"southwest",9:"up",10:"down",11:"in",12:"out"};class pe{constructor(e){this.cache=new Map,this.mapReader=e,this.graph=this.buildGraph()}buildGraph(){const e={};return this.mapReader.getRooms().forEach(t=>{const s={},r=new Set((t.exitLocks??[]).map(n=>ge[n]).filter(n=>!!n)),i=new Set(t.mSpecialExitLocks??[]);Object.entries(t.exits??{}).forEach(([n,o])=>{r.has(n)||this.mapReader.getRoom(o)&&(s[o.toString()]=1)}),Object.values(t.specialExits??{}).forEach(n=>{i.has(n)||this.mapReader.getRoom(n)&&(s[n.toString()]=1)}),e[t.id.toString()]=s}),new de(e)}findPath(e,t){const s=`${e}->${t}`;if(this.cache.has(s))return this.cache.get(s);if(e===t){const o=this.mapReader.getRoom(e)?[e]:null;return this.cache.set(s,o),o}if(!this.mapReader.getRoom(e)||!this.mapReader.getRoom(t))return this.cache.set(s,null),null;const r=this.graph.path(e.toString(),t.toString()),i=Array.isArray(r)?r:r==null?void 0:r.path,n=i?i.map(o=>Number(o)):null;return this.cache.set(s,n),n}}exports.ExplorationArea=O;exports.MapReader=ee;exports.PathFinder=pe;exports.Renderer=U;exports.Settings=c;
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p=require("konva"),_=["north","south","east","west","northeast","northwest","southeast","southwest"],D={north:"n",south:"s",east:"e",west:"w",northeast:"ne",northwest:"nw",southeast:"se",southwest:"sw",up:"u",down:"d",in:"i",out:"o"},N={north:{x:0,y:-1},south:{x:0,y:1},east:{x:1,y:0},west:{x:-1,y:0},northeast:{x:1,y:-1},northwest:{x:-1,y:-1},southeast:{x:1,y:1},southwest:{x:-1,y:1}},P=["north","south","east","west","northeast","northwest","southeast","southwest"],G={north:"south",south:"north",east:"west",west:"east",northeast:"southwest",northwest:"southeast",southeast:"northwest",southwest:"northeast"};function F(l){return l?Object.prototype.hasOwnProperty.call(N,l):!1}function m(l,e,t,s=1){if(!F(t))return{x:l,y:e};const r=N[t];return{x:l+r.x*s,y:e+r.y*s}}const E={OPEN_DOOR:"rgb(10, 155, 10)",CLOSED_DOOR:"rgb(226, 205, 59)",LOCKED_DOOR:"rgb(155, 10, 10)"},B={1:"north",2:"northeast",3:"northwest",4:"east",5:"west",6:"south",7:"southeast",8:"southwest",9:"up",10:"down",11:"in",12:"out"},H=["up","down","in","out"];function q(l){switch(l){case 1:return E.OPEN_DOOR;case 2:return E.CLOSED_DOOR;default:return E.LOCKED_DOOR}}class K{constructor(e){this.mapReader=e}render(e){return this.renderWithColor(e,c.lineColor)}renderWithColor(e,t){return e.aDir&&e.bDir?this.renderTwoWayExit(e,t):this.renderOneWayExit(e,t)}renderTwoWayExit(e,t){const s=this.mapReader.getRoom(e.a),r=this.mapReader.getRoom(e.b);if(!s||!r||!e.aDir||!e.bDir)return;const o=new p.Group,n=[];if(n.push(...Object.values(m(s.x,s.y,e.aDir,c.roomSize/2))),n.push(...Object.values(m(r.x,r.y,e.bDir,c.roomSize/2))),s.doors[D[e.aDir]]||r.doors[D[e.bDir]]){const h=this.renderDoor(n,s.doors[D[e.aDir]]??r.doors[D[e.bDir]],t);o.add(h)}const i=new p.Line({points:n,stroke:t,strokeWidth:.025});return o.add(i),o}renderOneWayExit(e,t){const s=e.aDir?this.mapReader.getRoom(e.a):this.mapReader.getRoom(e.b),r=e.aDir?this.mapReader.getRoom(e.b):this.mapReader.getRoom(e.a),o=e.aDir?e.aDir:e.bDir;if(!s||!r)return;if(s.area!=r.area&&o)return this.renderAreaExit(s,o,t);let n={x:r.x,y:r.y};(r.area!==s.area||r.z!==s.z)&&(n=m(s.x,s.y,o,c.roomSize/2));const i=m(s.x,s.y,o,.3),h=i.x-(i.x-n.x)/2,u=i.y-(i.y-n.y)/2,d=new p.Group,a=[];a.push(...Object.values(m(s.x,s.y,o,c.roomSize/2))),a.push(n.x,n.y);const g=new p.Line({points:a,stroke:t,strokeWidth:.025,dashEnabled:!0,dash:[.1,.05]});d.add(g);const f=new p.Arrow({points:[a[0],a[1],h,u],pointerLength:.5,pointerWidth:.35,strokeWidth:.035,stroke:t,fill:t,dashEnabled:!0,dash:[.1,.05]});return d.add(f),d}renderAreaExit(e,t,s){const r=m(e.x,e.y,t,c.roomSize/2),o=m(e.x,e.y,t,c.roomSize*1.5),n=s??this.mapReader.getColorValue(e.env);return new p.Arrow({points:[r.x,r.y,o.x,o.y],pointerLength:.3,pointerWidth:.3,strokeWidth:.035,stroke:n,fill:n})}renderSpecialExits(e,t){return Object.entries(e.customLines).map(([s,r])=>{const o=[e.x,e.y];r.points.reduce((d,a)=>(d.push(a.x,-a.y),d),o);const n=r.attributes.arrow?p.Arrow:p.Line,i=t??`rgb(${r.attributes.color.r}, ${r.attributes.color.g}, ${r.attributes.color.b})`,h=new n({points:o,strokeWidth:.025,stroke:i,fill:t??`rgb(${r.attributes.color.r}, ${r.attributes.color.g} , ${r.attributes.color.b})`,pointerLength:.3,pointerWidth:.2});let u=r.attributes.style;return u==="dot line"?(h.dash([.05,.05]),h.dashOffset(.1)):u==="dash line"?h.dash([.4,.2]):u==="solid line"||u!==void 0&&console.log("Brak opisu stylu: "+u),h})}renderStubs(e,t=c.lineColor){return e.stubs.map(s=>{const r=B[s],o=m(e.x,e.y,r,c.roomSize/2),n=m(e.x,e.y,r,c.roomSize/2+.5),i=[o.x,o.y,n.x,n.y];return new p.Line({points:i,stroke:t,strokeWidth:.025})})}renderInnerExits(e){return H.map(t=>{if(e.exits[t]){const s=new p.Group,r=new p.RegularPolygon({x:e.x,y:e.y,sides:3,fill:this.mapReader.getSymbolColor(e.env,.6),stroke:this.mapReader.getSymbolColor(e.env),strokeWidth:.025,radius:c.roomSize/5,scaleX:1.4,scaleY:.8});s.add(r);let o=e.doors[t];if(o!==void 0)switch(o){case 1:r.stroke(E.OPEN_DOOR);break;case 2:r.stroke(E.CLOSED_DOOR);break;default:r.stroke(E.LOCKED_DOOR)}switch(t){case"up":r.position(m(e.x,e.y,"south",c.roomSize/4));break;case"down":r.rotation(180),r.position(m(e.x,e.y,"north",c.roomSize/4));break;case"in":const n=r.clone();n.rotation(-90),n.position(m(e.x,e.y,"east",c.roomSize/4)),s.add(n),r.rotation(90),r.position(m(e.x,e.y,"west",c.roomSize/4));break;case"out":const i=r.clone();i.rotation(90),i.position(m(e.x,e.y,"east",c.roomSize/4)),s.add(i),r.rotation(-90),r.position(m(e.x,e.y,"west",c.roomSize/4));break}return s}}).filter(t=>t!==void 0)}renderDoor(e,t,s){const r={x:e[0]+(e[2]-e[0])/2,y:e[1]+(e[3]-e[1])/2};return new p.Rect({x:r.x-c.roomSize/4,y:r.y-c.roomSize/4,width:c.roomSize/2,height:c.roomSize/2,stroke:s??q(t),strokeWidth:.025})}}class Y{constructor(e,t){this.rooms=[],this.labels=[],this.rooms=e,this.bounds=this.createBounds(),this.labels=t}getRooms(){return this.rooms}getLabels(){return this.labels}getBounds(){return this.bounds}createBounds(){return this.rooms.reduce((e,t)=>({minX:Math.min(e.minX,t.x),maxX:Math.max(e.maxX,t.x),minY:Math.min(e.minY,t.y),maxY:Math.max(e.maxY,t.y)}),{minX:Number.POSITIVE_INFINITY,maxX:Number.NEGATIVE_INFINITY,minY:Number.POSITIVE_INFINITY,maxY:Number.NEGATIVE_INFINITY})}}class L{constructor(e){this.planes={},this.exits=new Map,this.version=0,this.area=e,this.planes=this.createPlanes(),this.createExits()}getAreaName(){return this.area.areaName}getAreaId(){return parseInt(this.area.areaId)}getVersion(){return this.version}markDirty(){this.version++}getPlane(e){return this.planes[e]}getPlanes(){return Object.values(this.planes)}getRooms(){return this.area.rooms}getLinkExits(e){return Array.from(this.exits.values()).filter(t=>t.zIndex.includes(e))}createPlanes(){const e=this.area.rooms.reduce((t,s)=>(t[s.z]||(t[s.z]=[]),t[s.z].push(s),t),{});return Object.entries(e).reduce((t,[s,r])=>(t[+s]=new Y(r,this.area.labels.filter(o=>o.Z===+s)),t),{})}createExits(){this.area.rooms.forEach(e=>{Object.entries(e.exits).filter(([t,s])=>_.indexOf(t)>-1&&!e.customLines.hasOwnProperty(D[t])).forEach(([t,s])=>this.createHalfExit(e.id,s,e.z,t))})}createHalfExit(e,t,s,r){if(e===t)return;const o=Math.min(e,t),n=Math.max(e,t),i=`${o}-${n}`;let h=this.exits.get(i);h||(h={a:o,b:n,zIndex:[s]}),o==e?h.aDir=r:h.bDir=r,h.zIndex.push(s),this.exits.set(i,h)}}class z extends Y{constructor(e,t){super(e.getRooms(),e.getLabels()),this.basePlane=e,this.visitedRooms=t}getRooms(){return this.basePlane.getRooms().filter(e=>this.visitedRooms.has(e.id))}getLabels(){return this.basePlane.getLabels()}getBounds(){const e=this.getRooms();return e.length?e.reduce((t,s)=>({minX:Math.min(t.minX,s.x),maxX:Math.max(t.maxX,s.x),minY:Math.min(t.minY,s.y),maxY:Math.max(t.maxY,s.y)}),{minX:Number.POSITIVE_INFINITY,maxX:Number.NEGATIVE_INFINITY,minY:Number.POSITIVE_INFINITY,maxY:Number.NEGATIVE_INFINITY}):this.basePlane.getBounds()}}class C extends L{constructor(e,t){super(e),this.planeCache=new WeakMap,this.visitedRooms=t instanceof Set?t:new Set(t??[]),this.areaRoomIds=new Set(e.rooms.map(s=>s.id))}getPlane(e){const t=super.getPlane(e);if(!t)return t;let s=this.planeCache.get(t);return s||(s=new z(t,this.visitedRooms),this.planeCache.set(t,s)),s}getPlanes(){return super.getPlanes().map(e=>{let t=this.planeCache.get(e);return t||(t=new z(e,this.visitedRooms),this.planeCache.set(e,t)),t})}getLinkExits(e){return super.getLinkExits(e).filter(t=>this.visitedRooms.has(t.a)||this.visitedRooms.has(t.b))}getVisitedRoomCount(){return super.getRooms().reduce((e,t)=>e+(this.visitedRooms.has(t.id)?1:0),0)}getTotalRoomCount(){return this.areaRoomIds.size}hasVisitedRoom(e){return this.areaRoomIds.has(e)&&this.visitedRooms.has(e)}getVisitedRoomIds(){return super.getRooms().filter(e=>this.visitedRooms.has(e.id)).map(e=>e.id)}addVisitedRoom(e){const t=this.visitedRooms.has(e);this.visitedRooms.add(e);const s=!t&&this.areaRoomIds.has(e);return s&&this.markDirty(),s}addVisitedRooms(e){let t=0;for(const s of e){const r=this.visitedRooms.has(s);this.visitedRooms.add(s),!r&&this.areaRoomIds.has(s)&&t++}return t>0&&this.markDirty(),t}}class Q{constructor(e,t){this.paths=[],this.mapReader=e,this.overlayLayer=t}renderPath(e,t,s,r="#66E64D"){if(t===void 0||s===void 0)return;const o=e.map(a=>this.mapReader.getRoom(a)).filter(a=>a!==void 0),n=[];let i=null;const h=()=>{i&&(i.length<4&&n.pop(),i=null)},u=()=>(i||(i=[],n.push(i)),i);o.forEach((a,g)=>{if(!this.isRoomVisible(a,t,s))return;const f=g>0?o[g-1]:void 0,y=g<o.length-1?o[g+1]:void 0;if(this.isRoomVisible(f,t,s))u();else{h();const R=u();if(f){const x=this.getDirectionTowards(a,f);if(x){const O=m(a.x,a.y,x,c.roomSize);R.push(O.x,O.y)}}}if(i==null||i.push(a.x,a.y),!this.isRoomVisible(y,t,s)&&y){const R=this.getDirectionTowards(a,y);if(R){const x=m(a.x,a.y,R,c.roomSize);i==null||i.push(x.x,x.y)}h()}}),h();const d=n.filter(a=>a.length>=4).map(a=>new p.Line({points:a,stroke:r,strokeWidth:.1}));return d.forEach(a=>{this.overlayLayer.add(a),this.paths.push(a)}),d[0]}clearPaths(){this.paths.forEach(e=>{e.destroy()}),this.paths=[]}isRoomVisible(e,t,s){return e?e.area===t&&e.z===s:!1}getDirectionTowards(e,t){for(const s of P)if(e.exits[s]===t.id)return s;for(const s of P)if(t.exits[s]===e.id)return G[s]}}const W=.6,S=75,J="rgb(225, 255, 225)",k="rgb(120, 72, 0)",v=class v{};v.roomSize=W,v.lineColor=J,v.instantMapMove=!1,v.highlightCurrentRoom=!0;let c=v;class U{constructor(e,t){this.highlights=new Map,this.currentZoom=1,this.currentRoomOverlay=[],this.stage=new p.Stage({container:e,width:e.clientWidth,height:e.clientHeight,draggable:!0}),window.addEventListener("resize",()=>{this.onResize(e)}),e.addEventListener("resize",()=>{this.onResize(e)}),this.linkLayer=new p.Layer({listening:!1}),this.stage.add(this.linkLayer),this.roomLayer=new p.Layer,this.stage.add(this.roomLayer),this.overlayLayer=new p.Layer({listening:!1}),this.stage.add(this.overlayLayer),this.positionLayer=new p.Layer({listening:!1}),this.stage.add(this.positionLayer),this.mapReader=t,this.exitRenderer=new K(t),this.pathRenderer=new Q(t,this.overlayLayer),this.initScaling(1.1)}onResize(e){this.stage.width(e.clientWidth),this.stage.height(e.clientHeight),this.currentRoomId&&this.centerOnRoom(this.mapReader.getRoom(this.currentRoomId),!1),this.stage.batchDraw()}initScaling(e){p.hitOnDragEnabled=!0;let t,s=!1,r=!1;this.stage.on("touchstart",o=>{const n=o.evt.touches;n&&n.length>1?(r=!0,this.stage.isDragging()&&(this.stage.stopDrag(),s=!0),this.stage.draggable(!1)):(r=!1,this.stage.draggable(!0))}),this.stage.on("touchend touchcancel",o=>{t=void 0;const n=o.evt.touches;(!n||n.length<=1)&&(r=!1,this.stage.draggable(!0))}),this.stage.on("wheel",o=>{o.evt.preventDefault();const n=this.stage.scaleX(),i=this.stage.getPointerPosition();if(!i)return;const h={x:(i.x-this.stage.x())/n,y:(i.y-this.stage.y())/n};let u=o.evt.deltaY>0?-1:1;o.evt.ctrlKey&&(u=-u);const d=u>0?this.currentZoom*e:this.currentZoom/e,a=d*S,g=this.setZoom(d),f={x:i.x-h.x*a,y:i.y-h.y*a};this.stage.position(f),g&&this.emitZoomChangeEvent()}),this.stage.on("touchmove",o=>{const n=o.evt.touches,i=n==null?void 0:n[0],h=n==null?void 0:n[1];if(h||r&&(r=!1,this.stage.draggable(!0)),i&&!h&&s&&!this.stage.isDragging()&&(this.stage.startDrag(),s=!1),!i||!h){t=void 0;return}o.evt.preventDefault(),this.stage.isDragging()&&(this.stage.stopDrag(),s=!0),r||(r=!0,this.stage.draggable(!1));const u=this.stage.container().getBoundingClientRect(),d={x:i.clientX-u.left,y:i.clientY-u.top},a={x:h.clientX-u.left,y:h.clientY-u.top},g=Math.hypot(d.x-a.x,d.y-a.y);if(t===void 0){t=g;return}if(t===0)return;const f=this.stage.scaleX(),y=this.stage.x(),w=this.stage.y(),b={x:this.stage.width()/2,y:this.stage.height()/2},R={x:(b.x-y)/f,y:(b.y-w)/f},x=this.currentZoom*(g/t),O=this.setZoom(x),I=this.stage.scaleX(),$={x:b.x-R.x*I,y:b.y-R.y*I};this.stage.position($),this.stage.batchDraw(),t=g,O&&this.emitZoomChangeEvent()})}drawArea(e,t){const s=this.mapReader.getArea(e);if(!s)return;const r=s.getPlane(t);r&&(this.currentArea=e,this.currentAreaInstance=s,this.currentZIndex=t,this.currentAreaVersion=s.getVersion(),this.clearCurrentRoomOverlay(),this.roomLayer.destroyChildren(),this.linkLayer.destroyChildren(),this.stage.scale({x:S*this.currentZoom,y:S*this.currentZoom}),this.renderLabels(r.getLabels()),this.renderExits(s.getLinkExits(t)),this.renderRooms(r.getRooms()??[]),this.refreshHighlights(),this.stage.batchDraw())}emitRoomContextEvent(e,t,s){const r=this.stage.container(),o=r.getBoundingClientRect(),n={roomId:e,position:{x:t-o.left,y:s-o.top}},i=new CustomEvent("roomcontextmenu",{detail:n});r.dispatchEvent(i)}emitZoomChangeEvent(){const e=new CustomEvent("zoom",{detail:{zoom:this.currentZoom}});this.stage.container().dispatchEvent(e)}setZoom(e){return this.currentZoom===e?!1:(this.currentZoom=e,this.stage.scale({x:S*e,y:S*e}),!0)}getZoom(){return this.currentZoom}getCurrentArea(){return this.currentArea?this.mapReader.getArea(this.currentArea):void 0}setPosition(e){const t=this.mapReader.getRoom(e);if(!t)return;const s=this.mapReader.getArea(t.area),r=s==null?void 0:s.getVersion();let o=this.currentArea!==t.area||this.currentZIndex!==t.z;(this.currentArea!==t.area||this.currentZIndex!==t.z||r!==void 0&&this.currentAreaVersion!==r||s!==void 0&&this.currentAreaInstance!==s)&&this.drawArea(t.area,t.z),this.positionRender||(this.positionRender=new p.Circle({x:t.x,y:t.y,radius:W*.85,stroke:"rgb(0, 229, 178)",strokeWidth:.1,dash:[.05,.05],dashEnabled:!0}),this.positionLayer.add(this.positionRender)),this.centerOnRoom(t,o),this.updateCurrentRoomOverlay(t)}renderPath(e,t){return this.pathRenderer.renderPath(e,this.currentArea,this.currentZIndex,t)}clearPaths(){this.pathRenderer.clearPaths()}renderHighlight(e,t){const s=this.mapReader.getRoom(e);if(!s)return;const r=this.highlights.get(e);r!=null&&r.shape&&(r.shape.destroy(),delete r.shape);const o={color:t,area:s.area,z:s.z};if(this.highlights.set(e,o),s.area===this.currentArea&&s.z===this.currentZIndex){const n=this.createHighlightShape(s,t);return this.overlayLayer.add(n),o.shape=n,this.overlayLayer.batchDraw(),n}return o.shape}clearHighlights(){this.highlights.forEach(({shape:e})=>e==null?void 0:e.destroy()),this.highlights.clear(),this.overlayLayer.batchDraw()}refreshHighlights(){this.highlights.forEach((e,t)=>{var o;if((o=e.shape)==null||o.destroy(),delete e.shape,e.area!==this.currentArea||e.z!==this.currentZIndex)return;const s=this.mapReader.getRoom(t);if(!s)return;const r=this.createHighlightShape(s,e.color);this.overlayLayer.add(r),e.shape=r}),this.overlayLayer.batchDraw()}createHighlightShape(e,t){return new p.Circle({x:e.x,y:e.y,radius:c.roomSize*.9,stroke:t,strokeWidth:.15,dash:[.1,.05],dashEnabled:!0,listening:!1})}centerOnRoom(e,t=!1){var u;this.currentRoomId=e.id;const s={x:e.x,y:e.y};(u=this.positionRender)==null||u.position(e);const o=this.stage.getAbsoluteTransform().point(s),n={x:this.stage.width()/2,y:this.stage.height()/2},i=n.x-o.x,h=n.y-o.y;this.currentTransition&&(this.currentTransition.pause(),this.currentTransition.destroy(),delete this.currentTransition),t||c.instantMapMove?this.stage.position({x:this.stage.x()+i,y:this.stage.y()+h}):(this.currentTransition=new p.Tween({node:this.stage,x:this.stage.x()+i,y:this.stage.y()+h,duration:.2,easing:p.Easings.EaseInOut}),this.currentTransition.play())}renderRooms(e){e.forEach(t=>{const s=new p.Group({x:t.x-c.roomSize/2,y:t.y-c.roomSize/2}),r=new p.Rect({x:0,y:0,width:c.roomSize,height:c.roomSize,fill:this.mapReader.getColorValue(t.env),strokeWidth:.025,stroke:c.lineColor}),o=(a,g)=>this.emitRoomContextEvent(t.id,a,g);s.on("mouseenter",()=>{this.stage.container().style.cursor="pointer"}),s.on("mouseleave",()=>{this.stage.container().style.cursor="auto"}),s.on("contextmenu",a=>{a.evt.preventDefault();const g=a.evt;o(g.clientX,g.clientY)});let n,i,h;const u=()=>{h!==void 0&&(this.stage.draggable(h),h=void 0)},d=()=>{n!==void 0&&(window.clearTimeout(n),n=void 0),i=void 0,u()};s.on("touchstart",a=>{var f;if(d(),a.evt.touches&&a.evt.touches.length>1)return;const g=(f=a.evt.touches)==null?void 0:f[0];g&&(i={clientX:g.clientX,clientY:g.clientY},h=this.stage.draggable(),this.stage.draggable(!1),n=window.setTimeout(()=>{i&&o(i.clientX,i.clientY),d()},500))}),s.on("touchend",d),s.on("touchmove",a=>{var R;if(!i)return;const g=(R=a.evt.touches)==null?void 0:R[0];if(!g){d();return}const f=g.clientX-i.clientX,y=g.clientY-i.clientY,w=f*f+y*y,b=10;if(w>b*b){const x=h;d(),x&&this.stage.startDrag()}}),s.on("touchcancel",d),s.add(r),this.renderSymbol(t,s),this.roomLayer.add(s),this.exitRenderer.renderSpecialExits(t).forEach(a=>{this.linkLayer.add(a)}),this.exitRenderer.renderStubs(t).forEach(a=>{this.linkLayer.add(a)}),this.exitRenderer.renderInnerExits(t).forEach(a=>{this.roomLayer.add(a)})})}clearCurrentRoomOverlay(){this.currentRoomOverlay.forEach(e=>e.destroy()),this.currentRoomOverlay=[],this.overlayLayer.batchDraw()}updateCurrentRoomOverlay(e){if(this.clearCurrentRoomOverlay(),e.area!==this.currentArea||e.z!==this.currentZIndex){this.overlayLayer.batchDraw();return}const t=new Map;t.set(e.id,e);const s=[],r=this.currentAreaInstance instanceof C?this.currentAreaInstance:void 0;this.currentAreaInstance&&this.currentZIndex!==void 0&&this.currentAreaInstance.getLinkExits(this.currentZIndex).filter(h=>h.a===e.id||h.b===e.id).forEach(h=>{const u=c.highlightCurrentRoom?this.exitRenderer.renderWithColor(h,k):this.exitRenderer.render(h);u&&s.push(u)});const o=c.highlightCurrentRoom?k:void 0;this.exitRenderer.renderSpecialExits(e,o).forEach(i=>{s.push(i)}),(c.highlightCurrentRoom?this.exitRenderer.renderStubs(e,k):this.exitRenderer.renderStubs(e)).forEach(i=>{s.push(i)}),[...Object.values(e.exits),...Object.values(e.specialExits)].forEach(i=>{const h=this.mapReader.getRoom(i),u=!r||r.hasVisitedRoom(i);h&&h.area===this.currentArea&&h.z===this.currentZIndex&&u&&t.set(i,h)}),s.forEach(i=>{this.overlayLayer.add(i),this.currentRoomOverlay.push(i)}),t.forEach((i,h)=>{const u=h===e.id,d=this.createOverlayRoomGroup(i,{stroke:u&&c.highlightCurrentRoom?k:c.lineColor,fillEnabled:!0,strokeEnabled:u?c.highlightCurrentRoom:!1});this.overlayLayer.add(d),this.currentRoomOverlay.push(d),this.exitRenderer.renderInnerExits(i).forEach(a=>{this.overlayLayer.add(a),this.currentRoomOverlay.push(a)})}),this.overlayLayer.batchDraw()}createOverlayRoomGroup(e,t){const s=new p.Group({x:e.x-c.roomSize/2,y:e.y-c.roomSize/2,listening:!1}),r=new p.Rect({x:0,y:0,width:c.roomSize,height:c.roomSize,fill:this.mapReader.getColorValue(e.env),stroke:t.stroke,strokeWidth:.025,strokeEnabled:t.strokeEnabled});return t.fillEnabled||r.fillEnabled(!1),s.add(r),this.renderSymbol(e,s),s}renderSymbol(e,t){if(e.roomChar!==void 0){const s=new p.Text({x:0,y:0,text:e.roomChar,fontSize:.45,fontStyle:"bold",fill:this.mapReader.getSymbolColor(e.env),align:"center",verticalAlign:"middle",width:c.roomSize,height:c.roomSize});t.add(s)}}renderExits(e){e.forEach(t=>{const s=this.exitRenderer.render(t);s&&this.linkLayer.add(s)})}renderLabels(e){e.forEach(t=>{if(!t.pixMap)return;const s=new Image;s.src=`data:image/png;base64,${t.pixMap}`;const r=new p.Image({x:t.X,y:-t.Y,width:t.Width,height:t.Height,image:s});this.linkLayer.add(r)})}}const V={rgbValue:"rgb(114, 1, 0)",symbolColor:[225,225,225]};function A(l){const e=l[0]/255,t=l[1]/255,s=l[2]/255,r=Math.max(e,t,s),o=Math.min(e,t,s);return(r+o)/2}class ee{constructor(e,t){this.rooms={},this.areas={},this.areaSources={},this.explorationEnabled=!1,this.colors={},e.forEach(s=>{s.rooms.forEach(o=>{o.y=-o.y,this.rooms[o.id]=o});const r=parseInt(s.areaId);this.areas[r]=new L(s),this.areaSources[r]=s}),this.colors=t.reduce((s,r)=>({...s,[r.envId]:{rgb:r.colors,rgbValue:`rgb(${r.colors.join(",")}`,symbolColor:A(r.colors)>.41?[25,25,25]:[225,255,255],symbolColorValue:A(r.colors)>.41?"rgb(25,25,25)":"rgb(225,255,255)"}}),{})}getArea(e){return this.areas[e]}getExplorationArea(e){const t=this.areas[e];if(t instanceof C)return t}getAreas(){return Object.values(this.areas)}getRooms(){return Object.values(this.rooms)}getRoom(e){return this.rooms[e]}ensureVisitedRooms(){return this.visitedRooms||(this.visitedRooms=new Set),this.visitedRooms}applyExplorationDecoration(){this.visitedRooms&&Object.entries(this.areaSources).forEach(([e,t])=>{const s=parseInt(e,10);this.areas[s]=new C(t,this.visitedRooms)})}decorateWithExploration(e){return e!==void 0?this.setVisitedRooms(e):this.ensureVisitedRooms(),this.applyExplorationDecoration(),this.explorationEnabled=!0,this.visitedRooms}getVisitedRooms(){return this.visitedRooms}clearExplorationDecoration(){Object.entries(this.areaSources).forEach(([e,t])=>{const s=parseInt(e,10);this.areas[s]=new L(t)}),this.explorationEnabled=!1}isExplorationEnabled(){return this.explorationEnabled}setVisitedRooms(e){return this.visitedRooms=e instanceof Set?e:new Set(e),this.explorationEnabled&&this.applyExplorationDecoration(),this.visitedRooms}addVisitedRoom(e){if(this.explorationEnabled){const r=this.getRoom(e);if(r){const o=this.getExplorationArea(r.area);if(o)return o.addVisitedRoom(e)}}const t=this.ensureVisitedRooms(),s=t.has(e);return t.add(e),!s}addVisitedRooms(e){const t=this.ensureVisitedRooms();let s=0;for(const r of e){if(this.explorationEnabled){const n=this.getRoom(r);if(n){const i=this.getExplorationArea(n.area);if(i){i.addVisitedRoom(r)&&s++;continue}}}const o=t.has(r);t.add(r),o||s++}return s}hasVisitedRoom(e){var t;return((t=this.visitedRooms)==null?void 0:t.has(e))??!1}getColorValue(e){var t;return((t=this.colors[e])==null?void 0:t.rgbValue)??V.rgbValue}getSymbolColor(e,t){var n;const s=((n=this.colors[e])==null?void 0:n.symbolColor)??V.symbolColor,r=Math.min(Math.max(t??1,0),1),o=s.join(",");return r!=1?`rgba(${o}, ${r})`:`rgba(${o})`}}function te(l){return l&&l.__esModule&&Object.prototype.hasOwnProperty.call(l,"default")?l.default:l}class se{constructor(){this.keys=new Set,this.queue=[]}sort(){this.queue.sort((e,t)=>e.priority-t.priority)}set(e,t){const s=Number(t);if(isNaN(s))throw new TypeError('"priority" must be a number');return this.keys.has(e)?this.queue.map(r=>(r.key===e&&Object.assign(r,{priority:s}),r)):(this.keys.add(e),this.queue.push({key:e,priority:s})),this.sort(),this.queue.length}next(){const e=this.queue.shift();return this.keys.delete(e.key),e}isEmpty(){return this.queue.length===0}has(e){return this.keys.has(e)}get(e){return this.queue.find(t=>t.key===e)}}var re=se;function Z(l,e){const t=new Map;for(const[s,r]of l)s!==e&&r instanceof Map?t.set(s,Z(r,e)):s!==e&&t.set(s,r);return t}var ie=Z;function oe(l){const e=Number(l);return!(isNaN(e)||e<=0)}function X(l){const e=new Map;return Object.keys(l).forEach(s=>{const r=l[s];if(r!==null&&typeof r=="object"&&!Array.isArray(r))return e.set(s,X(r));if(!oe(r))throw new Error(`Could not add node at key "${s}", make sure it's a valid node`,r);return e.set(s,Number(r))}),e}var ne=X;function j(l){if(!(l instanceof Map))throw new Error(`Invalid graph: Expected Map instead found ${typeof l}`);l.forEach((e,t)=>{if(typeof e=="object"&&e instanceof Map){j(e);return}if(typeof e!="number"||e<=0)throw new Error(`Values must be numbers greater than 0. Found value ${e} at ${t}`)})}var ae=j;const he=re,ce=ie,M=ne,T=ae;class le{constructor(e){e instanceof Map?(T(e),this.graph=e):e?this.graph=M(e):this.graph=new Map}addNode(e,t){let s;return t instanceof Map?(T(t),s=t):s=M(t),this.graph.set(e,s),this}addVertex(e,t){return this.addNode(e,t)}removeNode(e){return this.graph=ce(this.graph,e),this}path(e,t,s={}){if(!this.graph.size)return s.cost?{path:null,cost:0}:null;const r=new Set,o=new he,n=new Map;let i=[],h=0,u=[];if(s.avoid&&(u=[].concat(s.avoid)),u.includes(e))throw new Error(`Starting node (${e}) cannot be avoided`);if(u.includes(t))throw new Error(`Ending node (${t}) cannot be avoided`);for(o.set(e,0);!o.isEmpty();){const d=o.next();if(d.key===t){h=d.priority;let g=d.key;for(;n.has(g);)i.push(g),g=n.get(g);break}r.add(d.key),(this.graph.get(d.key)||new Map).forEach((g,f)=>{if(r.has(f)||u.includes(f))return null;if(!o.has(f))return n.set(f,d.key),o.set(f,d.priority+g);const y=o.get(f).priority,w=d.priority+g;return w<y?(n.set(f,d.key),o.set(f,w)):null})}return i.length?(s.trim?i.shift():i=i.concat([e]),s.reverse||(i=i.reverse()),s.cost?{path:i,cost:h}:i):s.cost?{path:null,cost:0}:null}shortestPath(...e){return this.path(...e)}}var ue=le;const de=te(ue),ge={1:"north",2:"northeast",3:"northwest",4:"east",5:"west",6:"south",7:"southeast",8:"southwest",9:"up",10:"down",11:"in",12:"out"};class pe{constructor(e){this.cache=new Map,this.mapReader=e,this.graph=this.buildGraph()}buildGraph(){const e={};return this.mapReader.getRooms().forEach(t=>{const s={},r=new Set((t.exitLocks??[]).map(n=>ge[n]).filter(n=>!!n)),o=new Set(t.mSpecialExitLocks??[]);Object.entries(t.exits??{}).forEach(([n,i])=>{r.has(n)||this.mapReader.getRoom(i)&&(s[i.toString()]=1)}),Object.values(t.specialExits??{}).forEach(n=>{o.has(n)||this.mapReader.getRoom(n)&&(s[n.toString()]=1)}),e[t.id.toString()]=s}),new de(e)}findPath(e,t){const s=`${e}->${t}`;if(this.cache.has(s))return this.cache.get(s);if(e===t){const i=this.mapReader.getRoom(e)?[e]:null;return this.cache.set(s,i),i}if(!this.mapReader.getRoom(e)||!this.mapReader.getRoom(t))return this.cache.set(s,null),null;const r=this.graph.path(e.toString(),t.toString()),o=Array.isArray(r)?r:r==null?void 0:r.path,n=o?o.map(i=>Number(i)):null;return this.cache.set(s,n),n}}exports.ExplorationArea=C;exports.MapReader=ee;exports.PathFinder=pe;exports.Renderer=U;exports.Settings=c;
|
|
2
2
|
//# sourceMappingURL=index.cjs.map
|