@woosh/meep-engine
Version:
Pure JavaScript game engine. Fully featured and production ready.
62 lines (49 loc) • 1.65 kB
JavaScript
import Task from "../Task.js";
import { TaskSignal } from "../TaskSignal.js";
/**
*
* @param {number|function(*):number} initial
* @param {number|function(*):number} limit limit is excluded from the count, same as `for(;i < limit;)`
* @param {function(index:int)} callback
* @returns {Task}
*/
export function countTask(initial, limit, callback) {
let initialValue = 0;
let limitValue = 0;
let i = initialValue;
function cycle() {
if (i >= limitValue) {
return TaskSignal.EndSuccess;
}
callback(i);
i++;
return TaskSignal.Continue;
}
const initialType = typeof initial;
const limitType = typeof limit;
const name = "count (from " + ((initialType === 'number') ? initial : 'variable') + " to " + ((limitType === 'number') ? limit : 'variable') + ")";
return new Task({
name: name,
initializer() {
if (initialType === "number") {
initialValue = initial;
} else if (initialType === "function") {
initialValue = initial();
}
if (limitType === "number") {
limitValue = limit;
} else if (limitType === "function") {
limitValue = limit();
}
i = initialValue;
},
cycleFunction: cycle,
computeProgress: function () {
const span = limitValue - initialValue;
if (span === 0) {
return 0;
}
return (i - initialValue) / span;
}
});
}