Skip to content

Commit

Permalink
Added removing of paths and buildings
Browse files Browse the repository at this point in the history
  • Loading branch information
neki-dev committed Sep 13, 2023
1 parent dbccf7d commit 1ac96ed
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 9 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ const length = path.getLength(): number
path.each(callback: (position: Position) => void)
```
#### Remove path from nodes
```ts
path.remove()
```
#### Get path buildings
```ts
const buildings = path.getBuildings(): Building[]
Expand Down Expand Up @@ -186,6 +191,11 @@ const height: number = building.height
building.each(callback: (position: Position) => void)
```
#### Remove building from path
```ts
building.remove()
```
.
## Example
Expand Down
5 changes: 4 additions & 1 deletion dist/building.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { Path } from './path';
import { Position } from './types';
export declare class Building {
readonly vertices: Position[];
readonly position: Position;
readonly width: number;
readonly height: number;
constructor(vertices: Position[]);
readonly path: Path;
constructor(path: Path, vertices: Position[]);
remove(): void;
each(callback: (position: Position) => void): void;
}
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/path.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ export declare class Path {
setNodeEnd(node: Node): void;
addBuilding(vertices: Position[]): Building;
getLength(): number;
remove(): void;
each(callback: (position: Position) => void): void;
}
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.1.1",
"version": "1.2.0",
"keywords": [
"map",
"generation",
Expand Down
15 changes: 14 additions & 1 deletion src/building.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable import/no-cycle */
import { Path } from './path';
import { Position } from './types';

export class Building {
Expand All @@ -10,11 +11,14 @@ export class Building {

readonly height: number;

constructor(vertices: Position[]) {
readonly path: Path;

constructor(path: Path, vertices: Position[]) {
if (vertices.length !== 4) {
throw Error('Invalid building vertices');
}

this.path = path;
this.vertices = vertices;

const xs = this.vertices.map((position) => position.x);
Expand All @@ -29,6 +33,15 @@ export class Building {
this.height = Math.max(...ys) - this.position.y + 1;
}

public remove() {
const pathBuildings = this.path.getBuildings();
const index = pathBuildings.findIndex((building) => building === this);

if (index !== -1) {
pathBuildings.splice(index, 1);
}
}

public each(callback: (position: Position) => void) {
for (let x = 0; x < this.width; x++) {
for (let y = 0; y < this.height; y++) {
Expand Down
26 changes: 23 additions & 3 deletions src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,40 @@ export class Path {
}

public addBuilding(vertices: Position[]) {
const building = new Building(vertices);
const building = new Building(this, vertices);

this.buildings.push(building);

return building;
}

public getLength() {
const positions = this.getPositions();

return Math.hypot(
this.nodeBeg.position.x - this.cursor.x,
this.nodeBeg.position.y - this.cursor.y,
positions.beg.x - positions.end.x,
positions.beg.y - positions.end.y,
);
}

public remove() {
const outputPaths = this.nodeBeg.getOutputPaths();
const outputIndex = outputPaths.findIndex((path) => path === this);

if (outputIndex !== -1) {
outputPaths.splice(outputIndex, 1);
}

if (this.nodeEnd) {
const inputPaths = this.nodeEnd.getInputPaths();
const inputIndex = inputPaths.findIndex((path) => path === this);

if (inputIndex !== -1) {
inputPaths.splice(inputIndex, 1);
}
}
}

public each(callback: (position: Position) => void) {
const shift = getShift(this.direction);
const length = this.getLength();
Expand Down

0 comments on commit 1ac96ed

Please sign in to comment.