mudlet-map-renderer 0.6.6-konva → 0.7.0-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/Renderer.d.ts +12 -0
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +403 -366
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/Renderer.d.ts
CHANGED
|
@@ -15,6 +15,13 @@ export declare class Settings {
|
|
|
15
15
|
static lineColor: string;
|
|
16
16
|
static instantMapMove: boolean;
|
|
17
17
|
static highlightCurrentRoom: boolean;
|
|
18
|
+
static cullingEnabled: boolean;
|
|
19
|
+
static cullingBounds: {
|
|
20
|
+
x: number;
|
|
21
|
+
y: number;
|
|
22
|
+
width: number;
|
|
23
|
+
height: number;
|
|
24
|
+
} | null;
|
|
18
25
|
}
|
|
19
26
|
export declare class Renderer {
|
|
20
27
|
private readonly stage;
|
|
@@ -35,6 +42,9 @@ export declare class Renderer {
|
|
|
35
42
|
private currentTransition?;
|
|
36
43
|
private currentZoom;
|
|
37
44
|
private currentRoomOverlay;
|
|
45
|
+
private roomNodes;
|
|
46
|
+
private standaloneExitNodes;
|
|
47
|
+
private cullingScheduled;
|
|
38
48
|
constructor(container: HTMLDivElement, mapReader: MapReader);
|
|
39
49
|
private onResize;
|
|
40
50
|
private initScaling;
|
|
@@ -57,6 +67,8 @@ export declare class Renderer {
|
|
|
57
67
|
private createHighlightShape;
|
|
58
68
|
private centerOnRoom;
|
|
59
69
|
private renderRooms;
|
|
70
|
+
private scheduleRoomCulling;
|
|
71
|
+
private updateRoomCulling;
|
|
60
72
|
private clearCurrentRoomOverlay;
|
|
61
73
|
private updateCurrentRoomOverlay;
|
|
62
74
|
private createOverlayRoomGroup;
|
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"],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;
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const f=require("konva"),K=["north","south","east","west","northeast","northwest","southeast","southwest"],O={north:"n",south:"s",east:"e",west:"w",northeast:"ne",northwest:"nw",southeast:"se",southwest:"sw",up:"u",down:"d",in:"i",out:"o"},_={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}},Y=["north","south","east","west","northeast","northwest","southeast","southwest"],Q={north:"south",south:"north",east:"west",west:"east",northeast:"southwest",northwest:"southeast",southeast:"northwest",southwest:"northeast"};function U(u){return u?Object.prototype.hasOwnProperty.call(_,u):!1}function x(u,t,e,s=1){if(!U(e))return{x:u,y:t};const i=_[e];return{x:u+i.x*s,y:t+i.y*s}}const C={OPEN_DOOR:"rgb(10, 155, 10)",CLOSED_DOOR:"rgb(226, 205, 59)",LOCKED_DOOR:"rgb(155, 10, 10)"},J={1:"north",2:"northeast",3:"northwest",4:"east",5:"west",6:"south",7:"southeast",8:"southwest",9:"up",10:"down",11:"in",12:"out"},tt=["up","down","in","out"];function et(u){switch(u){case 1:return C.OPEN_DOOR;case 2:return C.CLOSED_DOOR;default:return C.LOCKED_DOOR}}class st{constructor(t){this.mapReader=t}render(t){return this.renderWithColor(t,h.lineColor)}renderWithColor(t,e){return t.aDir&&t.bDir?this.renderTwoWayExit(t,e):this.renderOneWayExit(t,e)}renderTwoWayExit(t,e){const s=this.mapReader.getRoom(t.a),i=this.mapReader.getRoom(t.b);if(!s||!i||!t.aDir||!t.bDir)return;const o=new f.Group,n=[];if(n.push(...Object.values(x(s.x,s.y,t.aDir,h.roomSize/2))),n.push(...Object.values(x(i.x,i.y,t.bDir,h.roomSize/2))),s.doors[O[t.aDir]]||i.doors[O[t.bDir]]){const a=this.renderDoor(n,s.doors[O[t.aDir]]??i.doors[O[t.bDir]],e);o.add(a)}const r=new f.Line({points:n,stroke:e,strokeWidth:.025});return o.add(r),o}renderOneWayExit(t,e){const s=t.aDir?this.mapReader.getRoom(t.a):this.mapReader.getRoom(t.b),i=t.aDir?this.mapReader.getRoom(t.b):this.mapReader.getRoom(t.a),o=t.aDir?t.aDir:t.bDir;if(!s||!i)return;if(s.area!=i.area&&o)return this.renderAreaExit(s,o,e);let n={x:i.x,y:i.y};(i.area!==s.area||i.z!==s.z)&&(n=x(s.x,s.y,o,h.roomSize/2));const r=x(s.x,s.y,o,.3),a=r.x-(r.x-n.x)/2,d=r.y-(r.y-n.y)/2,g=new f.Group,c=[];c.push(...Object.values(x(s.x,s.y,o,h.roomSize/2))),c.push(n.x,n.y);const l=new f.Line({points:c,stroke:e,strokeWidth:.025,dashEnabled:!0,dash:[.1,.05]});g.add(l);const m=new f.Arrow({points:[c[0],c[1],a,d],pointerLength:.5,pointerWidth:.35,strokeWidth:.035,stroke:e,fill:e,dashEnabled:!0,dash:[.1,.05]});return g.add(m),g}renderAreaExit(t,e,s){const i=x(t.x,t.y,e,h.roomSize/2),o=x(t.x,t.y,e,h.roomSize*1.5),n=s??this.mapReader.getColorValue(t.env);return new f.Arrow({points:[i.x,i.y,o.x,o.y],pointerLength:.3,pointerWidth:.3,strokeWidth:.035,stroke:n,fill:n})}renderSpecialExits(t,e){return Object.entries(t.customLines).map(([s,i])=>{const o=[t.x,t.y];i.points.reduce((g,c)=>(g.push(c.x,-c.y),g),o);const n=i.attributes.arrow?f.Arrow:f.Line,r=e??`rgb(${i.attributes.color.r}, ${i.attributes.color.g}, ${i.attributes.color.b})`,a=new n({points:o,strokeWidth:.025,stroke:r,fill:e??`rgb(${i.attributes.color.r}, ${i.attributes.color.g} , ${i.attributes.color.b})`,pointerLength:.3,pointerWidth:.2});let d=i.attributes.style;return d==="dot line"?(a.dash([.05,.05]),a.dashOffset(.1)):d==="dash line"?a.dash([.4,.2]):d==="solid line"||d!==void 0&&console.log("Brak opisu stylu: "+d),a})}renderStubs(t,e=h.lineColor){return t.stubs.map(s=>{const i=J[s],o=x(t.x,t.y,i,h.roomSize/2),n=x(t.x,t.y,i,h.roomSize/2+.5),r=[o.x,o.y,n.x,n.y];return new f.Line({points:r,stroke:e,strokeWidth:.025})})}renderInnerExits(t){return tt.map(e=>{if(t.exits[e]){const s=new f.Group,i=new f.RegularPolygon({x:t.x,y:t.y,sides:3,fill:this.mapReader.getSymbolColor(t.env,.6),stroke:this.mapReader.getSymbolColor(t.env),strokeWidth:.025,radius:h.roomSize/5,scaleX:1.4,scaleY:.8});s.add(i);let o=t.doors[e];if(o!==void 0)switch(o){case 1:i.stroke(C.OPEN_DOOR);break;case 2:i.stroke(C.CLOSED_DOOR);break;default:i.stroke(C.LOCKED_DOOR)}switch(e){case"up":i.position(x(t.x,t.y,"south",h.roomSize/4));break;case"down":i.rotation(180),i.position(x(t.x,t.y,"north",h.roomSize/4));break;case"in":const n=i.clone();n.rotation(-90),n.position(x(t.x,t.y,"east",h.roomSize/4)),s.add(n),i.rotation(90),i.position(x(t.x,t.y,"west",h.roomSize/4));break;case"out":const r=i.clone();r.rotation(90),r.position(x(t.x,t.y,"east",h.roomSize/4)),s.add(r),i.rotation(-90),i.position(x(t.x,t.y,"west",h.roomSize/4));break}return s}}).filter(e=>e!==void 0)}renderDoor(t,e,s){const i={x:t[0]+(t[2]-t[0])/2,y:t[1]+(t[3]-t[1])/2};return new f.Rect({x:i.x-h.roomSize/4,y:i.y-h.roomSize/4,width:h.roomSize/2,height:h.roomSize/2,stroke:s??et(e),strokeWidth:.025})}}class F{constructor(t,e){this.rooms=[],this.labels=[],this.rooms=t,this.bounds=this.createBounds(),this.labels=e}getRooms(){return this.rooms}getLabels(){return this.labels}getBounds(){return this.bounds}createBounds(){return this.rooms.reduce((t,e)=>({minX:Math.min(t.minX,e.x),maxX:Math.max(t.maxX,e.x),minY:Math.min(t.minY,e.y),maxY:Math.max(t.maxY,e.y)}),{minX:Number.POSITIVE_INFINITY,maxX:Number.NEGATIVE_INFINITY,minY:Number.POSITIVE_INFINITY,maxY:Number.NEGATIVE_INFINITY})}}class A{constructor(t){this.planes={},this.exits=new Map,this.version=0,this.area=t,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(t){return this.planes[t]}getPlanes(){return Object.values(this.planes)}getRooms(){return this.area.rooms}getLinkExits(t){return Array.from(this.exits.values()).filter(e=>e.zIndex.includes(t))}createPlanes(){const t=this.area.rooms.reduce((e,s)=>(e[s.z]||(e[s.z]=[]),e[s.z].push(s),e),{});return Object.entries(t).reduce((e,[s,i])=>(e[+s]=new F(i,this.area.labels.filter(o=>o.Z===+s)),e),{})}createExits(){this.area.rooms.forEach(t=>{Object.entries(t.exits).filter(([e,s])=>K.indexOf(e)>-1&&!t.customLines.hasOwnProperty(O[e])).forEach(([e,s])=>this.createHalfExit(t.id,s,t.z,e))})}createHalfExit(t,e,s,i){if(t===e)return;const o=Math.min(t,e),n=Math.max(t,e),r=`${o}-${n}`;let a=this.exits.get(r);a||(a={a:o,b:n,zIndex:[s]}),o==t?a.aDir=i:a.bDir=i,a.zIndex.push(s),this.exits.set(r,a)}}class X extends F{constructor(t,e){super(t.getRooms(),t.getLabels()),this.basePlane=t,this.visitedRooms=e}getRooms(){return this.basePlane.getRooms().filter(t=>this.visitedRooms.has(t.id))}getLabels(){return this.basePlane.getLabels()}getBounds(){const t=this.getRooms();return t.length?t.reduce((e,s)=>({minX:Math.min(e.minX,s.x),maxX:Math.max(e.maxX,s.x),minY:Math.min(e.minY,s.y),maxY:Math.max(e.maxY,s.y)}),{minX:Number.POSITIVE_INFINITY,maxX:Number.NEGATIVE_INFINITY,minY:Number.POSITIVE_INFINITY,maxY:Number.NEGATIVE_INFINITY}):this.basePlane.getBounds()}}class P extends A{constructor(t,e){super(t),this.planeCache=new WeakMap,this.visitedRooms=e instanceof Set?e:new Set(e??[]),this.areaRoomIds=new Set(t.rooms.map(s=>s.id))}getPlane(t){const e=super.getPlane(t);if(!e)return e;let s=this.planeCache.get(e);return s||(s=new X(e,this.visitedRooms),this.planeCache.set(e,s)),s}getPlanes(){return super.getPlanes().map(t=>{let e=this.planeCache.get(t);return e||(e=new X(t,this.visitedRooms),this.planeCache.set(t,e)),e})}getLinkExits(t){return super.getLinkExits(t).filter(e=>this.visitedRooms.has(e.a)||this.visitedRooms.has(e.b))}getVisitedRoomCount(){return super.getRooms().reduce((t,e)=>t+(this.visitedRooms.has(e.id)?1:0),0)}getTotalRoomCount(){return this.areaRoomIds.size}hasVisitedRoom(t){return this.areaRoomIds.has(t)&&this.visitedRooms.has(t)}getVisitedRoomIds(){return super.getRooms().filter(t=>this.visitedRooms.has(t.id)).map(t=>t.id)}addVisitedRoom(t){const e=this.visitedRooms.has(t);this.visitedRooms.add(t);const s=!e&&this.areaRoomIds.has(t);return s&&this.markDirty(),s}addVisitedRooms(t){let e=0;for(const s of t){const i=this.visitedRooms.has(s);this.visitedRooms.add(s),!i&&this.areaRoomIds.has(s)&&e++}return e>0&&this.markDirty(),e}}class it{constructor(t,e){this.paths=[],this.mapReader=t,this.overlayLayer=e}renderPath(t,e,s,i="#66E64D"){if(e===void 0||s===void 0)return;const o=t.map(c=>this.mapReader.getRoom(c)).filter(c=>c!==void 0),n=[];let r=null;const a=()=>{r&&(r.length<4&&n.pop(),r=null)},d=()=>(r||(r=[],n.push(r)),r);o.forEach((c,l)=>{if(!this.isRoomVisible(c,e,s))return;const m=l>0?o[l-1]:void 0,R=l<o.length-1?o[l+1]:void 0;if(this.isRoomVisible(m,e,s))d();else{a();const b=d();if(m){const y=this.getDirectionTowards(c,m);if(y){const p=x(c.x,c.y,y,h.roomSize);b.push(p.x,p.y)}}}if(r==null||r.push(c.x,c.y),!this.isRoomVisible(R,e,s)&&R){const b=this.getDirectionTowards(c,R);if(b){const y=x(c.x,c.y,b,h.roomSize);r==null||r.push(y.x,y.y)}a()}}),a();const g=n.filter(c=>c.length>=4).map(c=>new f.Line({points:c,stroke:i,strokeWidth:.1}));return g.forEach(c=>{this.overlayLayer.add(c),this.paths.push(c)}),g[0]}clearPaths(){this.paths.forEach(t=>{t.destroy()}),this.paths=[]}isRoomVisible(t,e,s){return t?t.area===e&&t.z===s:!1}getDirectionTowards(t,e){for(const s of Y)if(t.exits[s]===e.id)return s;for(const s of Y)if(e.exits[s]===t.id)return Q[s]}}const G=.6,k=75,rt="rgb(225, 255, 225)",M="rgb(120, 72, 0)",D=class D{};D.roomSize=G,D.lineColor=rt,D.instantMapMove=!1,D.highlightCurrentRoom=!0,D.cullingEnabled=!0,D.cullingBounds=null;let h=D;class ot{constructor(t,e){this.highlights=new Map,this.currentZoom=1,this.currentRoomOverlay=[],this.roomNodes=new Map,this.standaloneExitNodes=[],this.cullingScheduled=!1,this.stage=new f.Stage({container:t,width:t.clientWidth,height:t.clientHeight,draggable:!0}),window.addEventListener("resize",()=>{this.onResize(t)}),t.addEventListener("resize",()=>{this.onResize(t)}),this.linkLayer=new f.Layer({listening:!1}),this.stage.add(this.linkLayer),this.roomLayer=new f.Layer,this.stage.add(this.roomLayer),this.overlayLayer=new f.Layer({listening:!1}),this.stage.add(this.overlayLayer),this.positionLayer=new f.Layer({listening:!1}),this.stage.add(this.positionLayer),this.mapReader=e,this.exitRenderer=new st(e),this.pathRenderer=new it(e,this.overlayLayer),this.initScaling(1.1),this.stage.on("dragmove",()=>this.scheduleRoomCulling()),this.stage.on("dragend",()=>this.scheduleRoomCulling())}onResize(t){this.stage.width(t.clientWidth),this.stage.height(t.clientHeight),this.currentRoomId&&this.centerOnRoom(this.mapReader.getRoom(this.currentRoomId),!1),this.stage.batchDraw(),this.scheduleRoomCulling()}initScaling(t){f.hitOnDragEnabled=!0;let e,s=!1,i=!1;this.stage.on("touchstart",o=>{const n=o.evt.touches;n&&n.length>1?(i=!0,this.stage.isDragging()&&(this.stage.stopDrag(),s=!0),this.stage.draggable(!1)):(i=!1,this.stage.draggable(!0))}),this.stage.on("touchend touchcancel",o=>{e=void 0;const n=o.evt.touches;(!n||n.length<=1)&&(i=!1,this.stage.draggable(!0))}),this.stage.on("wheel",o=>{o.evt.preventDefault();const n=this.stage.scaleX(),r=this.stage.getPointerPosition();if(!r)return;const a={x:(r.x-this.stage.x())/n,y:(r.y-this.stage.y())/n};let d=o.evt.deltaY>0?-1:1;o.evt.ctrlKey&&(d=-d);const g=d>0?this.currentZoom*t:this.currentZoom/t,c=g*k,l=this.setZoom(g),m={x:r.x-a.x*c,y:r.y-a.y*c};this.stage.position(m),this.scheduleRoomCulling(),l&&this.emitZoomChangeEvent()}),this.stage.on("touchmove",o=>{const n=o.evt.touches,r=n==null?void 0:n[0],a=n==null?void 0:n[1];if(a||i&&(i=!1,this.stage.draggable(!0)),r&&!a&&s&&!this.stage.isDragging()&&(this.stage.startDrag(),s=!1),!r||!a){e=void 0;return}o.evt.preventDefault(),this.stage.isDragging()&&(this.stage.stopDrag(),s=!0),i||(i=!0,this.stage.draggable(!1));const d=this.stage.container().getBoundingClientRect(),g={x:r.clientX-d.left,y:r.clientY-d.top},c={x:a.clientX-d.left,y:a.clientY-d.top},l=Math.hypot(g.x-c.x,g.y-c.y);if(e===void 0){e=l;return}if(e===0)return;const m=this.stage.scaleX(),R=this.stage.x(),v=this.stage.y(),E={x:this.stage.width()/2,y:this.stage.height()/2},b={x:(E.x-R)/m,y:(E.y-v)/m},y=this.currentZoom*(l/e),p=this.setZoom(y),w=this.stage.scaleX(),S={x:E.x-b.x*w,y:E.y-b.y*w};this.stage.position(S),this.stage.batchDraw(),this.scheduleRoomCulling(),e=l,p&&this.emitZoomChangeEvent()})}drawArea(t,e){const s=this.mapReader.getArea(t);if(!s)return;const i=s.getPlane(e);i&&(this.currentArea=t,this.currentAreaInstance=s,this.currentZIndex=e,this.currentAreaVersion=s.getVersion(),this.clearCurrentRoomOverlay(),this.roomLayer.destroyChildren(),this.linkLayer.destroyChildren(),this.roomNodes.clear(),this.standaloneExitNodes=[],this.stage.scale({x:k*this.currentZoom,y:k*this.currentZoom}),this.renderLabels(i.getLabels()),this.renderExits(s.getLinkExits(e)),this.renderRooms(i.getRooms()??[]),this.refreshHighlights(),this.stage.batchDraw(),this.scheduleRoomCulling())}emitRoomContextEvent(t,e,s){const i=this.stage.container(),o=i.getBoundingClientRect(),n={roomId:t,position:{x:e-o.left,y:s-o.top}},r=new CustomEvent("roomcontextmenu",{detail:n});i.dispatchEvent(r)}emitZoomChangeEvent(){const t=new CustomEvent("zoom",{detail:{zoom:this.currentZoom}});this.stage.container().dispatchEvent(t)}setZoom(t){return this.currentZoom===t?!1:(this.currentZoom=t,this.stage.scale({x:k*t,y:k*t}),this.scheduleRoomCulling(),!0)}getZoom(){return this.currentZoom}getCurrentArea(){return this.currentArea?this.mapReader.getArea(this.currentArea):void 0}setPosition(t){const e=this.mapReader.getRoom(t);if(!e)return;const s=this.mapReader.getArea(e.area),i=s==null?void 0:s.getVersion();let o=this.currentArea!==e.area||this.currentZIndex!==e.z;(this.currentArea!==e.area||this.currentZIndex!==e.z||i!==void 0&&this.currentAreaVersion!==i||s!==void 0&&this.currentAreaInstance!==s)&&this.drawArea(e.area,e.z),this.positionRender||(this.positionRender=new f.Circle({x:e.x,y:e.y,radius:G*.85,stroke:"rgb(0, 229, 178)",strokeWidth:.1,dash:[.05,.05],dashEnabled:!0}),this.positionLayer.add(this.positionRender)),this.centerOnRoom(e,o),this.updateCurrentRoomOverlay(e)}renderPath(t,e){return this.pathRenderer.renderPath(t,this.currentArea,this.currentZIndex,e)}clearPaths(){this.pathRenderer.clearPaths()}renderHighlight(t,e){const s=this.mapReader.getRoom(t);if(!s)return;const i=this.highlights.get(t);i!=null&&i.shape&&(i.shape.destroy(),delete i.shape);const o={color:e,area:s.area,z:s.z};if(this.highlights.set(t,o),s.area===this.currentArea&&s.z===this.currentZIndex){const n=this.createHighlightShape(s,e);return this.overlayLayer.add(n),o.shape=n,this.overlayLayer.batchDraw(),n}return o.shape}clearHighlights(){this.highlights.forEach(({shape:t})=>t==null?void 0:t.destroy()),this.highlights.clear(),this.overlayLayer.batchDraw()}refreshHighlights(){this.highlights.forEach((t,e)=>{var o;if((o=t.shape)==null||o.destroy(),delete t.shape,t.area!==this.currentArea||t.z!==this.currentZIndex)return;const s=this.mapReader.getRoom(e);if(!s)return;const i=this.createHighlightShape(s,t.color);this.overlayLayer.add(i),t.shape=i}),this.overlayLayer.batchDraw()}createHighlightShape(t,e){return new f.Circle({x:t.x,y:t.y,radius:h.roomSize*.9,stroke:e,strokeWidth:.15,dash:[.1,.05],dashEnabled:!0,listening:!1})}centerOnRoom(t,e=!1){var d;this.currentRoomId=t.id;const s={x:t.x,y:t.y};(d=this.positionRender)==null||d.position(t);const o=this.stage.getAbsoluteTransform().point(s),n={x:this.stage.width()/2,y:this.stage.height()/2},r=n.x-o.x,a=n.y-o.y;this.currentTransition&&(this.currentTransition.pause(),this.currentTransition.destroy(),delete this.currentTransition),e||h.instantMapMove?(this.stage.position({x:this.stage.x()+r,y:this.stage.y()+a}),this.scheduleRoomCulling()):(this.currentTransition=new f.Tween({node:this.stage,x:this.stage.x()+r,y:this.stage.y()+a,duration:.2,easing:f.Easings.EaseInOut,onUpdate:()=>this.scheduleRoomCulling(),onFinish:()=>this.scheduleRoomCulling()}),this.currentTransition.play())}renderRooms(t){t.forEach(e=>{const s=new f.Group({x:e.x-h.roomSize/2,y:e.y-h.roomSize/2}),i=new f.Rect({x:0,y:0,width:h.roomSize,height:h.roomSize,fill:this.mapReader.getColorValue(e.env),strokeWidth:.025,stroke:h.lineColor}),o=(l,m)=>this.emitRoomContextEvent(e.id,l,m);s.on("mouseenter",()=>{this.stage.container().style.cursor="pointer"}),s.on("mouseleave",()=>{this.stage.container().style.cursor="auto"}),s.on("contextmenu",l=>{l.evt.preventDefault();const m=l.evt;o(m.clientX,m.clientY)});let n,r,a;const d=()=>{a!==void 0&&(this.stage.draggable(a),a=void 0)},g=()=>{n!==void 0&&(window.clearTimeout(n),n=void 0),r=void 0,d()};s.on("touchstart",l=>{var R;if(g(),l.evt.touches&&l.evt.touches.length>1)return;const m=(R=l.evt.touches)==null?void 0:R[0];m&&(r={clientX:m.clientX,clientY:m.clientY},a=this.stage.draggable(),this.stage.draggable(!1),n=window.setTimeout(()=>{r&&o(r.clientX,r.clientY),g()},500))}),s.on("touchend",g),s.on("touchmove",l=>{var y;if(!r)return;const m=(y=l.evt.touches)==null?void 0:y[0];if(!m){g();return}const R=m.clientX-r.clientX,v=m.clientY-r.clientY,E=R*R+v*v,b=10;if(E>b*b){const p=a;g(),p&&this.stage.startDrag()}}),s.on("touchcancel",g),s.add(i),this.renderSymbol(e,s),this.exitRenderer.renderInnerExits(e).forEach(l=>{s.add(l),l.moveToTop()}),this.roomLayer.add(s);const c=[];this.exitRenderer.renderSpecialExits(e).forEach(l=>{this.linkLayer.add(l),c.push(l)}),this.exitRenderer.renderStubs(e).forEach(l=>{this.linkLayer.add(l),c.push(l)}),this.roomNodes.set(e.id,{room:e,group:s,linkNodes:c})})}scheduleRoomCulling(){this.cullingScheduled||(this.cullingScheduled=!0,window.requestAnimationFrame(()=>{this.cullingScheduled=!1,this.updateRoomCulling()}))}updateRoomCulling(){if(this.roomNodes.size===0&&this.standaloneExitNodes.length===0)return;const t=this.stage.scaleX();if(!t)return;const e=this.stage.position(),s=h.roomSize/2,i=h.cullingBounds,o=i?i.x:0,n=i?i.x+i.width:this.stage.width(),r=i?i.y:0,a=i?i.y+i.height:this.stage.height(),d=Math.min(o,n),g=Math.max(o,n),c=Math.min(r,a),l=Math.max(r,a),m=(d-e.x)/t,R=(g-e.x)/t,v=(c-e.y)/t,E=(l-e.y)/t;let b=!1,y=!1;if(!h.cullingEnabled){this.roomNodes.forEach(({group:p,linkNodes:w})=>{p.visible()||(p.visible(!0),b=!0),w.forEach(S=>{S.visible()||(S.visible(!0),y=!0)})}),this.standaloneExitNodes.forEach(p=>{p.visible()||(p.visible(!0),y=!0)}),b&&this.roomLayer.batchDraw(),y&&this.linkLayer.batchDraw();return}this.roomNodes.forEach(({room:p,group:w,linkNodes:S})=>{const V=p.x-s,z=p.x+s,N=p.y-s,L=p.y+s,I=z>=m&&V<=R&&L>=v&&N<=E;w.visible()!==I&&(w.visible(I),b=!0),S.forEach(T=>{T.visible()!==I&&(T.visible(I),y=!0)})}),this.standaloneExitNodes.forEach(p=>{const w=p.getClientRect({relativeTo:this.linkLayer}),S=w.x,V=w.x+w.width,z=w.y,N=w.y+w.height,L=V>=m&&S<=R&&N>=v&&z<=E;p.visible()!==L&&(p.visible(L),y=!0)}),b&&this.roomLayer.batchDraw(),y&&this.linkLayer.batchDraw()}clearCurrentRoomOverlay(){this.currentRoomOverlay.forEach(t=>t.destroy()),this.currentRoomOverlay=[],this.overlayLayer.batchDraw()}updateCurrentRoomOverlay(t){if(this.clearCurrentRoomOverlay(),t.area!==this.currentArea||t.z!==this.currentZIndex){this.overlayLayer.batchDraw();return}const e=new Map;e.set(t.id,t);const s=[],i=this.currentAreaInstance instanceof P?this.currentAreaInstance:void 0;this.currentAreaInstance&&this.currentZIndex!==void 0&&this.currentAreaInstance.getLinkExits(this.currentZIndex).filter(a=>a.a===t.id||a.b===t.id).forEach(a=>{const d=h.highlightCurrentRoom?this.exitRenderer.renderWithColor(a,M):this.exitRenderer.render(a);d&&s.push(d)});const o=h.highlightCurrentRoom?M:void 0;this.exitRenderer.renderSpecialExits(t,o).forEach(r=>{s.push(r)}),(h.highlightCurrentRoom?this.exitRenderer.renderStubs(t,M):this.exitRenderer.renderStubs(t)).forEach(r=>{s.push(r)}),[...Object.values(t.exits),...Object.values(t.specialExits)].forEach(r=>{const a=this.mapReader.getRoom(r),d=!i||i.hasVisitedRoom(r);a&&a.area===this.currentArea&&a.z===this.currentZIndex&&d&&e.set(r,a)}),s.forEach(r=>{this.overlayLayer.add(r),this.currentRoomOverlay.push(r)}),e.forEach((r,a)=>{const d=a===t.id,g=this.createOverlayRoomGroup(r,{stroke:d&&h.highlightCurrentRoom?M:h.lineColor,fillEnabled:!0,strokeEnabled:d?h.highlightCurrentRoom:!1});this.overlayLayer.add(g),this.currentRoomOverlay.push(g),this.exitRenderer.renderInnerExits(r).forEach(c=>{this.overlayLayer.add(c),this.currentRoomOverlay.push(c)})}),this.overlayLayer.batchDraw()}createOverlayRoomGroup(t,e){const s=new f.Group({x:t.x-h.roomSize/2,y:t.y-h.roomSize/2,listening:!1}),i=new f.Rect({x:0,y:0,width:h.roomSize,height:h.roomSize,fill:this.mapReader.getColorValue(t.env),stroke:e.stroke,strokeWidth:.025,strokeEnabled:e.strokeEnabled});return e.fillEnabled||i.fillEnabled(!1),s.add(i),this.renderSymbol(t,s),s}renderSymbol(t,e){if(t.roomChar!==void 0){const s=new f.Text({x:0,y:0,text:t.roomChar,fontSize:.45,fontStyle:"bold",fill:this.mapReader.getSymbolColor(t.env),align:"center",verticalAlign:"middle",width:h.roomSize,height:h.roomSize});e.add(s)}}renderExits(t){t.forEach(e=>{const s=this.exitRenderer.render(e);s&&(this.linkLayer.add(s),this.standaloneExitNodes.push(s))})}renderLabels(t){t.forEach(e=>{if(!e.pixMap)return;const s=new Image;s.src=`data:image/png;base64,${e.pixMap}`;const i=new f.Image({x:e.X,y:-e.Y,width:e.Width,height:e.Height,image:s});this.linkLayer.add(i)})}}const W={rgbValue:"rgb(114, 1, 0)",symbolColor:[225,225,225]};function Z(u){const t=u[0]/255,e=u[1]/255,s=u[2]/255,i=Math.max(t,e,s),o=Math.min(t,e,s);return(i+o)/2}class nt{constructor(t,e){this.rooms={},this.areas={},this.areaSources={},this.explorationEnabled=!1,this.colors={},t.forEach(s=>{s.rooms.forEach(o=>{o.y=-o.y,this.rooms[o.id]=o});const i=parseInt(s.areaId);this.areas[i]=new A(s),this.areaSources[i]=s}),this.colors=e.reduce((s,i)=>({...s,[i.envId]:{rgb:i.colors,rgbValue:`rgb(${i.colors.join(",")}`,symbolColor:Z(i.colors)>.41?[25,25,25]:[225,255,255],symbolColorValue:Z(i.colors)>.41?"rgb(25,25,25)":"rgb(225,255,255)"}}),{})}getArea(t){return this.areas[t]}getExplorationArea(t){const e=this.areas[t];if(e instanceof P)return e}getAreas(){return Object.values(this.areas)}getRooms(){return Object.values(this.rooms)}getRoom(t){return this.rooms[t]}ensureVisitedRooms(){return this.visitedRooms||(this.visitedRooms=new Set),this.visitedRooms}applyExplorationDecoration(){this.visitedRooms&&Object.entries(this.areaSources).forEach(([t,e])=>{const s=parseInt(t,10);this.areas[s]=new P(e,this.visitedRooms)})}decorateWithExploration(t){return t!==void 0?this.setVisitedRooms(t):this.ensureVisitedRooms(),this.applyExplorationDecoration(),this.explorationEnabled=!0,this.visitedRooms}getVisitedRooms(){return this.visitedRooms}clearExplorationDecoration(){Object.entries(this.areaSources).forEach(([t,e])=>{const s=parseInt(t,10);this.areas[s]=new A(e)}),this.explorationEnabled=!1}isExplorationEnabled(){return this.explorationEnabled}setVisitedRooms(t){return this.visitedRooms=t instanceof Set?t:new Set(t),this.explorationEnabled&&this.applyExplorationDecoration(),this.visitedRooms}addVisitedRoom(t){if(this.explorationEnabled){const i=this.getRoom(t);if(i){const o=this.getExplorationArea(i.area);if(o)return o.addVisitedRoom(t)}}const e=this.ensureVisitedRooms(),s=e.has(t);return e.add(t),!s}addVisitedRooms(t){const e=this.ensureVisitedRooms();let s=0;for(const i of t){if(this.explorationEnabled){const n=this.getRoom(i);if(n){const r=this.getExplorationArea(n.area);if(r){r.addVisitedRoom(i)&&s++;continue}}}const o=e.has(i);e.add(i),o||s++}return s}hasVisitedRoom(t){var e;return((e=this.visitedRooms)==null?void 0:e.has(t))??!1}getColorValue(t){var e;return((e=this.colors[t])==null?void 0:e.rgbValue)??W.rgbValue}getSymbolColor(t,e){var n;const s=((n=this.colors[t])==null?void 0:n.symbolColor)??W.symbolColor,i=Math.min(Math.max(e??1,0),1),o=s.join(",");return i!=1?`rgba(${o}, ${i})`:`rgba(${o})`}}function at(u){return u&&u.__esModule&&Object.prototype.hasOwnProperty.call(u,"default")?u.default:u}class ht{constructor(){this.keys=new Set,this.queue=[]}sort(){this.queue.sort((t,e)=>t.priority-e.priority)}set(t,e){const s=Number(e);if(isNaN(s))throw new TypeError('"priority" must be a number');return this.keys.has(t)?this.queue.map(i=>(i.key===t&&Object.assign(i,{priority:s}),i)):(this.keys.add(t),this.queue.push({key:t,priority:s})),this.sort(),this.queue.length}next(){const t=this.queue.shift();return this.keys.delete(t.key),t}isEmpty(){return this.queue.length===0}has(t){return this.keys.has(t)}get(t){return this.queue.find(e=>e.key===t)}}var ct=ht;function B(u,t){const e=new Map;for(const[s,i]of u)s!==t&&i instanceof Map?e.set(s,B(i,t)):s!==t&&e.set(s,i);return e}var lt=B;function ut(u){const t=Number(u);return!(isNaN(t)||t<=0)}function H(u){const t=new Map;return Object.keys(u).forEach(s=>{const i=u[s];if(i!==null&&typeof i=="object"&&!Array.isArray(i))return t.set(s,H(i));if(!ut(i))throw new Error(`Could not add node at key "${s}", make sure it's a valid node`,i);return t.set(s,Number(i))}),t}var dt=H;function q(u){if(!(u instanceof Map))throw new Error(`Invalid graph: Expected Map instead found ${typeof u}`);u.forEach((t,e)=>{if(typeof t=="object"&&t instanceof Map){q(t);return}if(typeof t!="number"||t<=0)throw new Error(`Values must be numbers greater than 0. Found value ${t} at ${e}`)})}var gt=q;const mt=ct,ft=lt,j=dt,$=gt;class pt{constructor(t){t instanceof Map?($(t),this.graph=t):t?this.graph=j(t):this.graph=new Map}addNode(t,e){let s;return e instanceof Map?($(e),s=e):s=j(e),this.graph.set(t,s),this}addVertex(t,e){return this.addNode(t,e)}removeNode(t){return this.graph=ft(this.graph,t),this}path(t,e,s={}){if(!this.graph.size)return s.cost?{path:null,cost:0}:null;const i=new Set,o=new mt,n=new Map;let r=[],a=0,d=[];if(s.avoid&&(d=[].concat(s.avoid)),d.includes(t))throw new Error(`Starting node (${t}) cannot be avoided`);if(d.includes(e))throw new Error(`Ending node (${e}) cannot be avoided`);for(o.set(t,0);!o.isEmpty();){const g=o.next();if(g.key===e){a=g.priority;let l=g.key;for(;n.has(l);)r.push(l),l=n.get(l);break}i.add(g.key),(this.graph.get(g.key)||new Map).forEach((l,m)=>{if(i.has(m)||d.includes(m))return null;if(!o.has(m))return n.set(m,g.key),o.set(m,g.priority+l);const R=o.get(m).priority,v=g.priority+l;return v<R?(n.set(m,g.key),o.set(m,v)):null})}return r.length?(s.trim?r.shift():r=r.concat([t]),s.reverse||(r=r.reverse()),s.cost?{path:r,cost:a}:r):s.cost?{path:null,cost:0}:null}shortestPath(...t){return this.path(...t)}}var yt=pt;const xt=at(yt),Rt={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 bt{constructor(t){this.cache=new Map,this.mapReader=t,this.graph=this.buildGraph()}buildGraph(){const t={};return this.mapReader.getRooms().forEach(e=>{const s={},i=new Set((e.exitLocks??[]).map(n=>Rt[n]).filter(n=>!!n)),o=new Set(e.mSpecialExitLocks??[]);Object.entries(e.exits??{}).forEach(([n,r])=>{i.has(n)||this.mapReader.getRoom(r)&&(s[r.toString()]=1)}),Object.values(e.specialExits??{}).forEach(n=>{o.has(n)||this.mapReader.getRoom(n)&&(s[n.toString()]=1)}),t[e.id.toString()]=s}),new xt(t)}findPath(t,e){const s=`${t}->${e}`;if(this.cache.has(s))return this.cache.get(s);if(t===e){const r=this.mapReader.getRoom(t)?[t]:null;return this.cache.set(s,r),r}if(!this.mapReader.getRoom(t)||!this.mapReader.getRoom(e))return this.cache.set(s,null),null;const i=this.graph.path(t.toString(),e.toString()),o=Array.isArray(i)?i:i==null?void 0:i.path,n=o?o.map(r=>Number(r)):null;return this.cache.set(s,n),n}}exports.ExplorationArea=P;exports.MapReader=nt;exports.PathFinder=bt;exports.Renderer=ot;exports.Settings=h;
|
|
2
2
|
//# sourceMappingURL=index.cjs.map
|