Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save random generator in constructor #107

Merged
merged 1 commit into from
Jun 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/armature/Generator.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { RandomGenerator } from '../utils/random';
import { Node, Point } from './Node';

import { range } from 'lodash';
Expand All @@ -20,6 +21,7 @@ export class Generator {
private rules: { [name: string]: { totalWeight: number; definitions: Definition[] } } = {};
private spawnPoints: SpawnPoint[] = [];
private nextSpawnPoints: SpawnPoint[] = [];
private random: RandomGenerator = Math.random;

/**
* Define a component to procedurally generate a component of a struture.
Expand Down Expand Up @@ -107,7 +109,7 @@ export class Generator {
throw new Error(`Cannot find definition for component "${component}"`);
}

const value = Math.random() * this.rules[component].totalWeight;
const value = this.random() * this.rules[component].totalWeight;
let weight = 0;
for (const definition of this.rules[component].definitions) {
weight += definition.weight;
Expand Down
4 changes: 4 additions & 0 deletions src/utils/random.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Generates a random number between 0 and 1.
*/
export type RandomGenerator = () => number;