Skip to content

Commit

Permalink
Improve mutate function
Browse files Browse the repository at this point in the history
  • Loading branch information
zvonimirfras committed Feb 25, 2021
1 parent ce89a47 commit 8e4682f
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion frontend/src/world/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Vector3 } from "@babylonjs/core";

export class Utils {
static mutate(initialValue: number | Vector3, randomization: number) : any {
static mutate(initialValue: number | Vector3 | any, randomization: number) : any {
if (initialValue instanceof Vector3) {
return new Vector3(
this.mutate(initialValue.x, randomization),
Expand All @@ -10,6 +10,19 @@ export class Utils {
);
}

if (typeof initialValue === 'object') {
const returnValue: any = {};

Object.keys(initialValue).forEach(element => {
let randomizationRate = randomization;
if (typeof initialValue[element] === 'number') {
randomizationRate = initialValue[element] * randomization;
}
returnValue[element] = Utils.mutate(initialValue[element], randomizationRate);
});
return returnValue;
}

const sign = Math.random() > 0.5 ? -1 : 1;
const num = Math.random() * randomization;
return initialValue + (sign * num);
Expand Down

0 comments on commit 8e4682f

Please sign in to comment.