Skip to content

Commit

Permalink
Removed matrix export
Browse files Browse the repository at this point in the history
  • Loading branch information
neki-dev committed Sep 13, 2023
1 parent 1ac96ed commit 9104079
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 87 deletions.
26 changes: 0 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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[]
Expand All @@ -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(): {
Expand Down Expand Up @@ -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
Expand Down
15 changes: 5 additions & 10 deletions dist/city.d.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<void>;
private reset;
private generatePaths;
private processingPath;
private generateBuildings;
private processingBuilding;
private getAt;
private markAt;
private isEmptyAt;
private addNode;
private closePath;
private forkPath;
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/node.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "gen-city",
"description": "Procedural generation city",
"version": "1.2.0",
"version": "1.3.0",
"keywords": [
"map",
"generation",
Expand Down
47 changes: 12 additions & 35 deletions src/city.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
import {
randomChance, generateSeed, getShift, forkDirection, turnDirection, randomRange,
} from './utils';
import { Building } from './building';

export class City {
readonly width: number;
Expand All @@ -34,10 +33,6 @@ export class City {
this.height = height;
}

public getMatrix() {
return this.matrix;
}

public getSeed() {
return this.seed;
}
Expand All @@ -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();

Expand Down Expand Up @@ -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);

Expand Down
8 changes: 8 additions & 0 deletions src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
14 changes: 2 additions & 12 deletions src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 9104079

Please sign in to comment.