We will write several utility functions in lib/game/utils.ts
. With flatten
, we will transform an array of arrays into one array.
export function flatten<U>(source: U[][]): U[] { return (<U[]>[]).concat(...source); }
With update, we can modify some properties of an object. This is the same function as in previous chapters.
export function update<U extends V, V>(old: U, changes: V): U { const result = Object.create(Object.getPrototypeOf(old)); for (const key of Object.keys(old)) { result[key] = (<any> old)[key]; } for (const key of Object.keys(changes)) { result[key] = (<any> changes)[key]; } return result; }
Next, we will create a function for working with Math.random
. randomInt
will return a random integer in a certain range and chance
has a chance to return true
:
export function randomInt(min: number, max: number) { return min +...