Skip to content

Commit

Permalink
Added buildingOffset parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
neki-dev committed Sep 14, 2023
1 parent 5c210ed commit 83effc5
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ await city.generate(params?)
| `buildingMaxSize` | Maximum size of bulding size | 6 |
| `buildingMinSpace` | Minimum distance between buildings | 1 |
| `buildingMaxSpace` | Maximum distance between buildings | 3 |
| `buildingOffset` | Distance between building and path | 0 |
.
Expand Down
4 changes: 4 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
<input type="input" name="buildingMaxSpace" value="3" />
</div>
</div>
<div class="item">
<label>Building offset</label>
<input type="input" name="buildingOffset" value="0" />
</div>
<div class="item">
<label>Probability intersection</label>
<input type="input" name="probabilityIntersection" value="0.1" />
Expand Down
1 change: 1 addition & 0 deletions demo/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ function generateAndRenderCity() {
buildingMaxSize: Number(ui.inputs.buildingMaxSize?.value),
buildingMinSpace: Number(ui.inputs.buildingMinSpace?.value),
buildingMaxSpace: Number(ui.inputs.buildingMaxSpace?.value),
buildingOffset: Number(ui.inputs.buildingOffset?.value),
probabilityIntersection: Number(ui.inputs.probabilityIntersection?.value),
probabilityTurn: Number(ui.inputs.probabilityTurn?.value),
probabilityStreetEnd: Number(ui.inputs.probabilityStreetEnd?.value),
Expand Down
1 change: 1 addition & 0 deletions demo/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const ui = {
buildingMaxSize: document.querySelector<HTMLInputElement>('[name=buildingMaxSize]'),
buildingMinSpace: document.querySelector<HTMLInputElement>('[name=buildingMinSpace]'),
buildingMaxSpace: document.querySelector<HTMLInputElement>('[name=buildingMaxSpace]'),
buildingOffset: document.querySelector<HTMLInputElement>('[name=buildingOffset]'),
probabilityIntersection: document.querySelector<HTMLInputElement>('[name=probabilityIntersection]'),
probabilityTurn: document.querySelector<HTMLInputElement>('[name=probabilityTurn]'),
probabilityStreetEnd: document.querySelector<HTMLInputElement>('[name=probabilityStreetEnd]'),
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions dist/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ export type CityGenerationParameters = {
* Default: 3
*/
buildingMaxSpace: number;
/**
* Distance between building and path.
* Default: 0
*/
buildingOffset: number;
};
export type CityGenerationParametersCustom = {
[key in keyof CityGenerationParameters]?: CityGenerationParameters[key];
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.3.2",
"version": "1.4.0",
"keywords": [
"map",
"generation",
Expand Down
7 changes: 4 additions & 3 deletions src/city.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export class City {
buildingMaxSize: 6,
buildingMinSpace: 1,
buildingMaxSpace: 3,
buildingOffset: 0,
probabilityIntersection: 0.1,
probabilityTurn: 0.05,
probabilityStreetEnd: 0.001,
Expand Down Expand Up @@ -184,11 +185,11 @@ export class City {
};

turnDirection(path.direction).forEach((direction) => {
let stepOffset = 0;
let stepOffset = this.params.buildingOffset;

while (path.getLength() > stepOffset) {
const stepShift = getShift(path.direction, stepOffset);
const shiftFromPath = getShift(direction);
const shiftFromPath = getShift(direction, this.params.buildingOffset + 1);
const startPosition = {
x: position.x + stepShift.x + shiftFromPath.x,
y: position.y + stepShift.y + shiftFromPath.y,
Expand All @@ -198,7 +199,7 @@ export class City {
randomRange(this.params.buildingMinSize, this.params.buildingMaxSize),
];

if (stepOffset + size[0] > path.getLength()) {
if (stepOffset + size[0] + this.params.buildingOffset > path.getLength()) {
break;
}

Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ export type CityGenerationParameters = {
* Default: 3
*/
buildingMaxSpace: number

/**
* Distance between building and path.
* Default: 0
*/
buildingOffset: number
};

export type CityGenerationParametersCustom = {
Expand Down

0 comments on commit 83effc5

Please sign in to comment.