diff --git a/README.md b/README.md index 4e99cbd..2dd7f72 100644 --- a/README.md +++ b/README.md @@ -62,17 +62,6 @@ Return seed if city was generated with runtime mode const seed = city.getSeed(): number[] | null ``` -#### Get matrix -Get all tiles (Node, Path, Building) as matrix -```ts -const matrix = city.getMatrix(): MatrixTile[][] -``` - -#### Get tile at matrix position -```ts -const tile = city.getAt(position: Position): MatrixTile | null -``` - . ## Nodes @@ -82,11 +71,6 @@ const tile = city.getAt(position: Position): MatrixTile | null const nodes = city.getAllNodes(): Node[] ``` -#### Get node at matrix position -```ts -const node = city.getNodeAt(position: Position): Node | null -``` - #### Get node paths ```ts const inputPaths = node.getInputPaths(): Path[] @@ -109,11 +93,6 @@ const type = node.getType(): NodeType const paths = city.getAllPaths(): Path[] ``` -#### Get path at matrix position -```ts -const path = city.getPathAt(position: Position): Path | null -``` - #### Get path positions ```ts const positions = path.getPositions(): { @@ -163,11 +142,6 @@ const direction: number = path.direction const buildings = city.getAllBuildings(): Building[] ``` -#### Get building at matrix position -```ts -const building = city.getBuildingAt(position: Position): Building | null -``` - #### Get building vertices Array of rectangle corners positions ```ts diff --git a/dist/city.d.ts b/dist/city.d.ts index 1f9b094..7f49472 100644 --- a/dist/city.d.ts +++ b/dist/city.d.ts @@ -1,7 +1,6 @@ import { Path } from './path'; import { Node } from './node'; -import { CityData, Position, MatrixTile, CityGenerationParametersCustom } from './types'; -import { Building } from './building'; +import { CityData, CityGenerationParametersCustom } from './types'; export declare class City { readonly width: number; readonly height: number; @@ -11,23 +10,19 @@ export declare class City { private gauge; private params; constructor({ width, height }: CityData); - getMatrix(): MatrixTile[][]; getSeed(): number[] | null; - getAllBuildings(): Building[]; - getBuildingAt(position: Position): Building | null; + getAllBuildings(): import("./building").Building[]; getAllNodes(): Node[]; - getNodeAt(position: Position): Node | null; getAllPaths(): Path[]; - getPathAt(position: Position): Path | null; - getAt(position: Position): MatrixTile; - private markAt; - private isEmptyAt; generate(params?: CityGenerationParametersCustom): Promise; private reset; private generatePaths; private processingPath; private generateBuildings; private processingBuilding; + private getAt; + private markAt; + private isEmptyAt; private addNode; private closePath; private forkPath; diff --git a/dist/index.js b/dist/index.js index 9212a79..640dc4d 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1 +1 @@ -(()=>{"use strict";var t={895:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.Building=void 0,e.Building=class{constructor(t,e){if(4!==e.length)throw Error("Invalid building vertices");this.path=t,this.vertices=e;const i=this.vertices.map((t=>t.x)),n=this.vertices.map((t=>t.y));this.position={x:Math.min(...i),y:Math.min(...n)},this.width=Math.max(...i)-this.position.x+1,this.height=Math.max(...n)-this.position.y+1}remove(){const t=this.path.getBuildings(),e=t.findIndex((t=>t===this));-1!==e&&t.splice(e,1)}each(t){for(let e=0;et.getBuildings())).flat()}getBuildingAt(t){const e=this.getAt(t);return e instanceof a.Building?e:null}getAllNodes(){return this.nodes}getNodeAt(t){const e=this.getAt(t);return e instanceof r.Node?e:null}getAllPaths(){return this.nodes.map((t=>t.getOutputPaths())).flat()}getPathAt(t){const e=this.getAt(t);return e instanceof s.Path?e:null}getAt(t){var e,i;return null===(i=null===(e=this.matrix)||void 0===e?void 0:e[t.y])||void 0===i?void 0:i[t.x]}markAt(t,e){this.matrix[t.y][t.x]=e}isEmptyAt(t){return null===this.getAt(t)}generate(t={}){return n(this,void 0,void 0,(function*(){this.reset(),this.params=Object.assign({mode:o.CityGenerationMode.RUNTIME,seed:[],startPosition:{x:Math.round(this.width/2),y:Math.round(this.height/2)},startDirections:[0,90,180,270],streetMinLength:10,buildingMinSize:3,buildingMaxSize:6,buildingMinSpace:1,buildingMaxSpace:3,probabilityIntersection:.1,probabilityTurn:.05,probabilityStreetEnd:.001},t),this.params.mode===o.CityGenerationMode.SEED&&(this.seed=0===this.params.seed.length?(0,h.generateSeed)():this.params.seed);const e=this.addNode(this.params.startPosition);this.markAt(this.params.startPosition,e),this.params.startDirections.forEach((t=>{e.addOutputPath(t)})),this.generatePaths(),this.generateBuildings()}))}reset(){for(let t=0;t{const e=this.getAllPaths();for(let t=0,i=e.length;tt.isCompleted()))||t()};t()}processingPath(t){if(t.isCompleted())return;const e=t.getNextCursor();if(void 0===this.getAt(e)||this.variability(this.params.probabilityStreetEnd))return void this.closePath(t);const i=this.getCross(t);if(null==i?void 0:i.tile){let e=null;return i.tile instanceof r.Node?e=i.tile:i.tile instanceof s.Path&&(e=this.splitPath(i.tile,i.position)),void(e&&t.setNodeEnd(e))}t.setCursor(e),this.markAt(t.getCursor(),t);const n=this.params.streetMinLength;t.getLength()>n&&!this.getCross(t,n)&&(this.variability(this.params.probabilityIntersection)?this.forkPath(t):this.variability(this.params.probabilityTurn)&&this.turnPath(t))}generateBuildings(){this.getAllPaths().forEach((t=>{const e=(0,h.getShift)(t.direction),i=t.getNodeBeg().position.x+e.x,n=t.getNodeBeg().position.y+e.y;(0,h.turnDirection)(t.direction).forEach((e=>{let s=0;for(;t.getLength()>s;){const r=(0,h.getShift)(t.direction,s),o=(0,h.getShift)(e),a={x:i+r.x+o.x,y:n+r.y+o.y},d=[(0,h.randomRange)(this.params.buildingMinSize,this.params.buildingMaxSize),(0,h.randomRange)(this.params.buildingMinSize,this.params.buildingMaxSize)];if(s+d[0]>t.getLength())break;this.processingBuilding(t,a,d,[t.direction,e]);const u=(0,h.randomRange)(this.params.buildingMinSpace,this.params.buildingMaxSpace);s+=d[0]+u}}))}))}processingBuilding(t,e,i,n){const s=[],r=[];for(let t=0;t{this.markAt(t,o)}))}addNode(t){const e=new r.Node(this.nodes.length,t);return this.nodes.push(e),e}closePath(t){const e=t.getCursor(),i=this.addNode(e);return t.setNodeEnd(i),this.markAt(e,i),i}forkPath(t){let e=(0,h.forkDirection)(t.direction).sort((()=>this.variability(.5)?1:-1));if(e=this.filterDirections(t,e),0===e.length||1===e.length&&e[0]===t.direction)return;const i=this.closePath(t);for(let t=0;tthis.variability(.5)?1:-1));if(e=this.filterDirections(t,e),0===e.length)return;const i=this.closePath(t);i.addOutputPath(e[0]),this.markAt(i.position,i)}splitPath(t,e){const i=this.addNode(e),n=i.addOutputPath(t.direction);this.markAt(e,i);const r=t.getNodeEnd();return r?n.setNodeEnd(r):n.setCursor(t.getCursor()),n.each((t=>{this.getAt(t)instanceof s.Path&&this.markAt(t,n)})),t.setNodeEnd(i),i}getCross(t,e=1){const i=t.getCursor();for(let n=1;n<=e;n++){const e=(0,h.getShift)(t.direction,n),s={x:i.x+e.x,y:i.y+e.y};if(!this.isEmptyAt(s))return{tile:this.getAt(s),position:s}}return null}filterDirections(t,e){return e.filter((e=>{const i=(0,h.getShift)(e),n=t.getCursor(),s={x:n.x+i.x,y:n.y+i.y};return this.isEmptyAt(s)}))}variability(t){if(!this.seed)return(0,h.randomChance)(t);const e=this.seed[this.gauge%this.seed.length];return this.gauge++,e/1e3>=1-t}}},465:function(t,e,i){var n=this&&this.__createBinding||(Object.create?function(t,e,i,n){void 0===n&&(n=i);var s=Object.getOwnPropertyDescriptor(e,i);s&&!("get"in s?!e.__esModule:s.writable||s.configurable)||(s={enumerable:!0,get:function(){return e[i]}}),Object.defineProperty(t,n,s)}:function(t,e,i,n){void 0===n&&(n=i),t[n]=e[i]}),s=this&&this.__exportStar||function(t,e){for(var i in t)"default"===i||Object.prototype.hasOwnProperty.call(e,i)||n(e,t,i)};Object.defineProperty(e,"__esModule",{value:!0}),s(i(863),e),s(i(895),e),s(i(792),e),s(i(82),e),s(i(309),e)},792:(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.Node=void 0;const n=i(82),s=i(309);e.Node=class{constructor(t,e){this.outputPaths=[],this.inputPaths=[],this.id=t,this.position=e}addOutputPath(t){const e=new n.Path(this,t);return this.outputPaths.push(e),e}getOutputPaths(){return this.outputPaths}addInputPath(t){const e=t.getNodeEnd();e&&e.removeInputPath(t),this.inputPaths.push(t)}removeInputPath(t){const e=this.inputPaths.indexOf(t);-1!==e&&this.inputPaths.splice(e,1)}getInputPaths(){return this.inputPaths}getAllPaths(){return this.outputPaths.concat(this.inputPaths)}getType(){const t=this.outputPaths.length+this.inputPaths.length;return 2===t?s.NodeType.TURN:1===t?s.NodeType.END:s.NodeType.CROSS}}},82:(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.Path=void 0;const n=i(679),s=i(895);e.Path=class{constructor(t,e){this.buildings=[],this.nodeEnd=null,this.direction=e,this.nodeBeg=t,this.cursor=t.position}getBuildings(){return this.buildings}getPositions(){var t,e;return{beg:this.nodeBeg.position,end:null!==(e=null===(t=this.nodeEnd)||void 0===t?void 0:t.position)&&void 0!==e?e:this.cursor}}isCompleted(){return Boolean(this.nodeEnd)}getNextCursor(){const t=(0,n.getShift)(this.direction);return{x:this.cursor.x+t.x,y:this.cursor.y+t.y}}getCursor(){return this.cursor}setCursor(t){this.cursor=t}getNodeBeg(){return this.nodeBeg}getNodeEnd(){return this.nodeEnd}setNodeEnd(t){t.addInputPath(this),this.nodeEnd=t,this.cursor=t.position}addBuilding(t){const e=new s.Building(this,t);return this.buildings.push(e),e}getLength(){const t=this.getPositions();return Math.hypot(t.beg.x-t.end.x,t.beg.y-t.end.y)}remove(){const t=this.nodeBeg.getOutputPaths(),e=t.findIndex((t=>t===this));if(-1!==e&&t.splice(e,1),this.nodeEnd){const t=this.nodeEnd.getInputPaths(),e=t.findIndex((t=>t===this));-1!==e&&t.splice(e,1)}}each(t){const e=(0,n.getShift)(this.direction),i=this.getLength(),s=Object.assign({},this.nodeBeg.position);for(let n=0;n{var i,n;Object.defineProperty(e,"__esModule",{value:!0}),e.NodeType=e.CityGenerationMode=void 0,function(t){t.RUNTIME="RUNTIME",t.SEED="SEED"}(i||(e.CityGenerationMode=i={})),function(t){t.TURN="TURN",t.CROSS="CROSS",t.END="END"}(n||(e.NodeType=n={}))},679:(t,e)=>{function i(t){return t*(Math.PI/180)}function n(t){return[(t+90)%360,(t-90)%360]}Object.defineProperty(e,"__esModule",{value:!0}),e.between=e.getShift=e.forkDirection=e.turnDirection=e.degToRad=e.generateSeed=e.randomRange=e.randomItem=e.randomChance=void 0,e.randomChance=function(t){return Math.random()>1-t},e.randomItem=function(t){return t[Math.floor(Math.random()*t.length)]},e.randomRange=function(t,e){return Math.floor(t+Math.random()*(e-t+1))},e.generateSeed=function(t=32){const e=[];for(let i=0;i=e[0]&&t<=e[1]}}},e={},i=function i(n){var s=e[n];if(void 0!==s)return s.exports;var r=e[n]={exports:{}};return t[n].call(r.exports,r,r.exports,i),r.exports}(465);module.exports=i})(); \ No newline at end of file +(()=>{"use strict";var t={895:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.Building=void 0,e.Building=class{constructor(t,e){if(4!==e.length)throw Error("Invalid building vertices");this.path=t,this.vertices=e;const i=this.vertices.map((t=>t.x)),s=this.vertices.map((t=>t.y));this.position={x:Math.min(...i),y:Math.min(...s)},this.width=Math.max(...i)-this.position.x+1,this.height=Math.max(...s)-this.position.y+1}remove(){const t=this.path.getBuildings(),e=t.findIndex((t=>t===this));-1!==e&&t.splice(e,1)}each(t){for(let e=0;et.getBuildings())).flat()}getAllNodes(){return this.nodes}getAllPaths(){return this.nodes.map((t=>t.getOutputPaths())).flat()}generate(t={}){return s(this,void 0,void 0,(function*(){this.reset(),this.params=Object.assign({mode:o.CityGenerationMode.RUNTIME,seed:[],startPosition:{x:Math.round(this.width/2),y:Math.round(this.height/2)},startDirections:[0,90,180,270],streetMinLength:10,buildingMinSize:3,buildingMaxSize:6,buildingMinSpace:1,buildingMaxSpace:3,probabilityIntersection:.1,probabilityTurn:.05,probabilityStreetEnd:.001},t),this.params.mode===o.CityGenerationMode.SEED&&(this.seed=0===this.params.seed.length?(0,h.generateSeed)():this.params.seed);const e=this.addNode(this.params.startPosition);this.markAt(this.params.startPosition,e),this.params.startDirections.forEach((t=>{e.addOutputPath(t)})),this.generatePaths(),this.generateBuildings()}))}reset(){for(let t=0;t{const e=this.getAllPaths();for(let t=0,i=e.length;tt.isCompleted()))||t()};t()}processingPath(t){if(t.isCompleted())return;const e=t.getNextCursor();if(void 0===this.getAt(e)||this.variability(this.params.probabilityStreetEnd))return void this.closePath(t);const i=this.getCross(t);if(null==i?void 0:i.tile){let e=null;return i.tile instanceof r.Node?e=i.tile:i.tile instanceof n.Path&&(e=this.splitPath(i.tile,i.position)),void(e&&t.setNodeEnd(e))}t.setCursor(e),this.markAt(t.getCursor(),t);const s=this.params.streetMinLength;t.getLength()>s&&!this.getCross(t,s)&&(this.variability(this.params.probabilityIntersection)?this.forkPath(t):this.variability(this.params.probabilityTurn)&&this.turnPath(t))}generateBuildings(){this.getAllPaths().forEach((t=>{const e=(0,h.getShift)(t.direction),i=t.getNodeBeg().position.x+e.x,s=t.getNodeBeg().position.y+e.y;(0,h.turnDirection)(t.direction).forEach((e=>{let n=0;for(;t.getLength()>n;){const r=(0,h.getShift)(t.direction,n),o=(0,h.getShift)(e),a={x:i+r.x+o.x,y:s+r.y+o.y},d=[(0,h.randomRange)(this.params.buildingMinSize,this.params.buildingMaxSize),(0,h.randomRange)(this.params.buildingMinSize,this.params.buildingMaxSize)];if(n+d[0]>t.getLength())break;this.processingBuilding(t,a,d,[t.direction,e]);const u=(0,h.randomRange)(this.params.buildingMinSpace,this.params.buildingMaxSpace);n+=d[0]+u}}))}))}processingBuilding(t,e,i,s){const n=[],r=[];for(let t=0;t{this.markAt(t,o)}))}getAt(t){var e,i;return null===(i=null===(e=this.matrix)||void 0===e?void 0:e[t.y])||void 0===i?void 0:i[t.x]}markAt(t,e){this.matrix[t.y][t.x]=e}isEmptyAt(t){return null===this.getAt(t)}addNode(t){const e=new r.Node(this.nodes.length,t);return this.nodes.push(e),e}closePath(t){const e=t.getCursor(),i=this.addNode(e);return t.setNodeEnd(i),this.markAt(e,i),i}forkPath(t){let e=(0,h.forkDirection)(t.direction).sort((()=>this.variability(.5)?1:-1));if(e=this.filterDirections(t,e),0===e.length||1===e.length&&e[0]===t.direction)return;const i=this.closePath(t);for(let t=0;tthis.variability(.5)?1:-1));if(e=this.filterDirections(t,e),0===e.length)return;const i=this.closePath(t);i.addOutputPath(e[0]),this.markAt(i.position,i)}splitPath(t,e){const i=this.addNode(e),s=i.addOutputPath(t.direction);this.markAt(e,i);const r=t.getNodeEnd();return r?s.setNodeEnd(r):s.setCursor(t.getCursor()),s.each((t=>{this.getAt(t)instanceof n.Path&&this.markAt(t,s)})),t.setNodeEnd(i),i}getCross(t,e=1){const i=t.getCursor();for(let s=1;s<=e;s++){const e=(0,h.getShift)(t.direction,s),n={x:i.x+e.x,y:i.y+e.y};if(!this.isEmptyAt(n))return{tile:this.getAt(n),position:n}}return null}filterDirections(t,e){return e.filter((e=>{const i=(0,h.getShift)(e),s=t.getCursor(),n={x:s.x+i.x,y:s.y+i.y};return this.isEmptyAt(n)}))}variability(t){if(!this.seed)return(0,h.randomChance)(t);const e=this.seed[this.gauge%this.seed.length];return this.gauge++,e/1e3>=1-t}}},465:function(t,e,i){var s=this&&this.__createBinding||(Object.create?function(t,e,i,s){void 0===s&&(s=i);var n=Object.getOwnPropertyDescriptor(e,i);n&&!("get"in n?!e.__esModule:n.writable||n.configurable)||(n={enumerable:!0,get:function(){return e[i]}}),Object.defineProperty(t,s,n)}:function(t,e,i,s){void 0===s&&(s=i),t[s]=e[i]}),n=this&&this.__exportStar||function(t,e){for(var i in t)"default"===i||Object.prototype.hasOwnProperty.call(e,i)||s(e,t,i)};Object.defineProperty(e,"__esModule",{value:!0}),n(i(863),e),n(i(895),e),n(i(792),e),n(i(82),e),n(i(309),e)},792:(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.Node=void 0;const s=i(82),n=i(309);e.Node=class{constructor(t,e){this.outputPaths=[],this.inputPaths=[],this.id=t,this.position=e}addOutputPath(t){const e=new s.Path(this,t);return this.outputPaths.push(e),e}removeOutputPath(t){const e=this.outputPaths.indexOf(t);-1!==e&&this.outputPaths.splice(e,1)}getOutputPaths(){return this.outputPaths}addInputPath(t){const e=t.getNodeEnd();e&&e.removeInputPath(t),this.inputPaths.push(t)}removeInputPath(t){const e=this.inputPaths.indexOf(t);-1!==e&&this.inputPaths.splice(e,1)}getInputPaths(){return this.inputPaths}getAllPaths(){return this.outputPaths.concat(this.inputPaths)}getType(){const t=this.outputPaths.length+this.inputPaths.length;return 2===t?n.NodeType.TURN:1===t?n.NodeType.END:n.NodeType.CROSS}}},82:(t,e,i)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.Path=void 0;const s=i(679),n=i(895);e.Path=class{constructor(t,e){this.buildings=[],this.nodeEnd=null,this.direction=e,this.nodeBeg=t,this.cursor=t.position}getBuildings(){return this.buildings}getPositions(){var t,e;return{beg:this.nodeBeg.position,end:null!==(e=null===(t=this.nodeEnd)||void 0===t?void 0:t.position)&&void 0!==e?e:this.cursor}}isCompleted(){return Boolean(this.nodeEnd)}getNextCursor(){const t=(0,s.getShift)(this.direction);return{x:this.cursor.x+t.x,y:this.cursor.y+t.y}}getCursor(){return this.cursor}setCursor(t){this.cursor=t}getNodeBeg(){return this.nodeBeg}getNodeEnd(){return this.nodeEnd}setNodeEnd(t){t.addInputPath(this),this.nodeEnd=t,this.cursor=t.position}addBuilding(t){const e=new n.Building(this,t);return this.buildings.push(e),e}getLength(){const t=this.getPositions();return Math.hypot(t.beg.x-t.end.x,t.beg.y-t.end.y)}remove(){this.nodeBeg.removeOutputPath(this),this.nodeEnd&&this.nodeEnd.removeInputPath(this)}each(t){const e=(0,s.getShift)(this.direction),i=this.getLength(),n=Object.assign({},this.nodeBeg.position);for(let s=0;s{var i,s;Object.defineProperty(e,"__esModule",{value:!0}),e.NodeType=e.CityGenerationMode=void 0,function(t){t.RUNTIME="RUNTIME",t.SEED="SEED"}(i||(e.CityGenerationMode=i={})),function(t){t.TURN="TURN",t.CROSS="CROSS",t.END="END"}(s||(e.NodeType=s={}))},679:(t,e)=>{function i(t){return t*(Math.PI/180)}function s(t){return[(t+90)%360,(t-90)%360]}Object.defineProperty(e,"__esModule",{value:!0}),e.between=e.getShift=e.forkDirection=e.turnDirection=e.degToRad=e.generateSeed=e.randomRange=e.randomItem=e.randomChance=void 0,e.randomChance=function(t){return Math.random()>1-t},e.randomItem=function(t){return t[Math.floor(Math.random()*t.length)]},e.randomRange=function(t,e){return Math.floor(t+Math.random()*(e-t+1))},e.generateSeed=function(t=32){const e=[];for(let i=0;i=e[0]&&t<=e[1]}}},e={},i=function i(s){var n=e[s];if(void 0!==n)return n.exports;var r=e[s]={exports:{}};return t[s].call(r.exports,r,r.exports,i),r.exports}(465);module.exports=i})(); \ No newline at end of file diff --git a/dist/node.d.ts b/dist/node.d.ts index fc9216b..1677b4d 100644 --- a/dist/node.d.ts +++ b/dist/node.d.ts @@ -7,6 +7,7 @@ export declare class Node { private inputPaths; constructor(id: number, position: Position); addOutputPath(direction: number): Path; + removeOutputPath(path: Path): void; getOutputPaths(): Path[]; addInputPath(path: Path): void; removeInputPath(path: Path): void; diff --git a/package-lock.json b/package-lock.json index 01f942d..68cc1d1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "gen-city", - "version": "1.2.0", + "version": "1.3.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "gen-city", - "version": "1.2.0", + "version": "1.3.0", "license": "MIT", "devDependencies": { "eslint": "8.36.0", diff --git a/package.json b/package.json index ace3b8c..395713b 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "gen-city", "description": "Procedural generation city", - "version": "1.2.0", + "version": "1.3.0", "keywords": [ "map", "generation", diff --git a/src/city.ts b/src/city.ts index fd2b902..6c81b5b 100644 --- a/src/city.ts +++ b/src/city.ts @@ -11,7 +11,6 @@ import { import { randomChance, generateSeed, getShift, forkDirection, turnDirection, randomRange, } from './utils'; -import { Building } from './building'; export class City { readonly width: number; @@ -34,10 +33,6 @@ export class City { this.height = height; } - public getMatrix() { - return this.matrix; - } - public getSeed() { return this.seed; } @@ -46,44 +41,14 @@ export class City { return this.getAllPaths().map((path) => path.getBuildings()).flat(); } - public getBuildingAt(position: Position) { - const tile = this.getAt(position); - - return tile instanceof Building ? tile : null; - } - public getAllNodes() { return this.nodes; } - public getNodeAt(position: Position) { - const tile = this.getAt(position); - - return tile instanceof Node ? tile : null; - } - public getAllPaths() { return this.nodes.map((node) => node.getOutputPaths()).flat(); } - public getPathAt(position: Position) { - const tile = this.getAt(position); - - return tile instanceof Path ? tile : null; - } - - public getAt(position: Position) { - return this.matrix?.[position.y]?.[position.x]; - } - - private markAt(position: Position, tile: MatrixTile) { - this.matrix[position.y][position.x] = tile; - } - - private isEmptyAt(position: Position) { - return this.getAt(position) === null; - } - public async generate(params: CityGenerationParametersCustom = {}) { this.reset(); @@ -294,6 +259,18 @@ export class City { }); } + private getAt(position: Position) { + return this.matrix?.[position.y]?.[position.x]; + } + + private markAt(position: Position, tile: MatrixTile) { + this.matrix[position.y][position.x] = tile; + } + + private isEmptyAt(position: Position) { + return this.getAt(position) === null; + } + private addNode(position: Position) { const node = new Node(this.nodes.length, position); diff --git a/src/node.ts b/src/node.ts index 925d62a..b847f88 100644 --- a/src/node.ts +++ b/src/node.ts @@ -24,6 +24,14 @@ export class Node { return path; } + public removeOutputPath(path: Path) { + const index = this.outputPaths.indexOf(path); + + if (index !== -1) { + this.outputPaths.splice(index, 1); + } + } + public getOutputPaths() { return this.outputPaths; } diff --git a/src/path.ts b/src/path.ts index 0611fd2..661f73c 100644 --- a/src/path.ts +++ b/src/path.ts @@ -86,20 +86,10 @@ export class Path { } public remove() { - const outputPaths = this.nodeBeg.getOutputPaths(); - const outputIndex = outputPaths.findIndex((path) => path === this); - - if (outputIndex !== -1) { - outputPaths.splice(outputIndex, 1); - } + this.nodeBeg.removeOutputPath(this); if (this.nodeEnd) { - const inputPaths = this.nodeEnd.getInputPaths(); - const inputIndex = inputPaths.findIndex((path) => path === this); - - if (inputIndex !== -1) { - inputPaths.splice(inputIndex, 1); - } + this.nodeEnd.removeInputPath(this); } }