@dominicstop/utils
Version:
Yet another event emitter written in typescript.
59 lines (58 loc) • 1.76 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OfflineSimulation = void 0;
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());
}
}
exports.OfflineSimulation = OfflineSimulation;