UNPKG

@woosh/meep-engine

Version:

Pure JavaScript game engine. Fully featured and production ready.

65 lines (47 loc) 1.45 kB
import { array_shuffle } from "../../../collection/array/array_shuffle.js"; import { seededRandom } from "../../../math/random/seededRandom.js"; import Task from "../Task.js"; import { TaskSignal } from "../TaskSignal.js"; /** * * @param {number} seed RNG seed * @param {number|function(*):number} initial * @param {number|function(*):number} limit * @param {function(index:int)} callback * @returns {Task} */ export function randomCountTask(seed, initial, limit, callback) { const random = seededRandom(seed); const span = limit - initial; const sequence = new Uint16Array(span); let i = 0; function cycle() { if (i >= span) { return TaskSignal.EndSuccess; } const order = sequence[i]; const index = order + initial; callback(index); i++; return TaskSignal.Continue; } return new Task({ name: `Count ${initial} -> ${limit}`, initializer() { i = 0; //generate sequence for (let i = 0; i < span; i++) { sequence[i] = i; } //shuffle array_shuffle(random, sequence); }, cycleFunction: cycle, computeProgress: function () { if (span === 0) { return 0; } return i / span; } }); }