UNPKG

@dominicstop/utils

Version:

Yet another event emitter written in typescript.

55 lines (54 loc) 1.61 kB
export class OfflineSimulation { constructor(engine, fixedDeltaTime = 1 / 60) { this.isRunning = false; this._totalSimulatedTime = 0; this._iterationCount = 0; this.maxIterations = 200; this.onPreStep = null; this.onPostStep = null; this.onUpdate = null; this.engine = engine; this.fixedDeltaTimeSeconds = fixedDeltaTime; } get totalSimulatedTime() { return this._totalSimulatedTime; } get iterationCount() { return this._iterationCount; } run() { this.isRunning = true; this._iterationCount = 0; while (this.isRunning && this._iterationCount < this.maxIterations) { this.step(); this._iterationCount++; this.onUpdate?.(); if (this.isStable()) { this.isRunning = false; } } } ; step() { this.onPreStep?.(this.fixedDeltaTimeSeconds); this.engine.update(this.fixedDeltaTimeSeconds); this.onPostStep?.(this.fixedDeltaTimeSeconds); this._totalSimulatedTime += this.fixedDeltaTimeSeconds; } reset() { this.isRunning = false; this.engine.clear(); this._totalSimulatedTime = 0; this._iterationCount = 0; } addParticle(particle) { this.engine.addParticle(particle); } addForce(force) { this.engine.addForce(force); } isStable() { // Placeholder: Replace with actual stability logic return this.engine.particles.every(p => p.checkIsAtRest()); } }