Out of control trains!

This commit is contained in:
2018-04-06 01:07:16 -04:00
parent edae3f76f4
commit 4b7bf5353c
4 changed files with 96 additions and 28 deletions

View File

@@ -5,10 +5,26 @@ export const randomInt = (min: number, max: number): number => (
Math.floor(Math.random() * (max - (min + 1))) + min
);
export const weightedRandom = (choices: any[], weights: number[]): any => {
const totalWeight = weights.reduce((a, b) => a + b, 0);
const rand = randomInt(0, totalWeight);
let cumulWeight = 0;
for (let i = 0; i < weights.length; i += 1) {
cumulWeight += weights[i];
if (rand < cumulWeight) {
return choices[i];
}
}
};
export const randomPoint = () => (
new PIXI.Point(randomInt(0, window.innerWidth), randomInt(0, window.innerHeight))
);
export const pointsEqual = (pointA: PIXI.Point, pointB: PIXI.Point): boolean => (
(pointA.x === pointB.x && pointA.y === pointB.y)
);
export const distance = (pointA: PIXI.Point, pointB: PIXI.Point): number => {
const distX = pointA.x - pointB.x;
const distY = pointA.y - pointB.y;