UNPKG

@woosh/meep-engine

Version:

Pure JavaScript game engine. Fully featured and production ready.

50 lines (40 loc) 1.08 kB
export class RemainingTimeEstimator { /** * * @param {number} sampleCount * @constructor */ constructor(sampleCount) { this.sampleCount = sampleCount; this.rates = new Float32Array(sampleCount); this.cursor = 0; } /** * * @param {number} timeElapsed * @param {number} progress */ update(timeElapsed, progress) { const index = this.cursor; this.cursor = (this.cursor + 1) % this.sampleCount; const rate = progress / timeElapsed; this.rates[index] = rate; } /** * * @param {number} currentProgress * @returns {number} */ estimate(currentProgress) { let rateSum = 0; for (let i = 0; i < this.sampleCount; i++) { rateSum += this.rates[i]; } const rate = rateSum / this.sampleCount; if (rate === 0) { return Infinity; } const result = (1 - currentProgress) / rate; return result; } }