tarinai/js/performance_manager.js
2026-06-21 22:29:00 +09:00

63 lines
2.1 KiB
JavaScript

"use strict";
class TarinaiPerformanceManager {
constructor() {
this.fps = 60;
this.level = 0;
this.lastLevel = 0;
this.samples = [];
this.sampleLimit = 20;
}
updateFps(fps, worldRef = null) {
const n = Number(fps);
if (!Number.isFinite(n) || n <= 0) return this.level;
this.samples.push(n);
if (this.samples.length > this.sampleLimit) this.samples.shift();
const avg = this.samples.reduce((a, b) => a + b, 0) / this.samples.length;
this.fps = Math.round(avg);
const next = this.computeLevel(avg);
if (next !== this.level) {
this.lastLevel = this.level;
this.level = next;
window.TarinaiEvents?.emit("performance:level", { level: this.level, previous: this.lastLevel, fps: this.fps, world: worldRef || null });
}
return this.level;
}
computeLevel(fps = this.fps) {
if (!fps || !Number.isFinite(fps)) return 0;
if (fps < 22) return 3;
if (fps < 34) return 2;
if (fps < 48) return 1;
return 0;
}
quality(worldRef = null) {
const level = this.level;
let antCount = 0;
for (const ant of worldRef?.ants || []) if (ant && !ant.dead) antCount += 1;
const itemCount = (worldRef?.items || []).length || 0;
const effectCount = (worldRef?.effects || []).length || 0;
const antStride = antCount >= 95 || level >= 3 ? 3 : (antCount >= 35 || level >= 2 ? 2 : 1);
return {
fps: this.fps,
level,
antStride,
itemCount,
antCount,
effectCount,
effectLimit: level >= 3 ? 34 : level >= 2 ? 52 : level >= 1 ? 68 : 88,
statsInterval: level >= 3 ? 1.20 : level === 2 ? 0.92 : level === 1 ? 0.68 : 0.48,
terrainRedrawInterval: level >= 3 ? 1.30 : level === 2 ? 1.05 : level === 1 ? 0.82 : 0.62,
backgroundLightStep: level >= 3 ? 64 : level === 2 ? 96 : 180,
uiUpdateInterval: level >= 3 ? 1.2 : level === 2 ? 0.9 : level === 1 ? 0.65 : 0.45,
};
}
debugSnapshot(worldRef = null) {
return { version: window.TARINAI_VERSION || "", fps: this.fps, level: this.level, quality: this.quality(worldRef) };
}
}
window.TarinaiPerformance = window.TarinaiPerformance || new TarinaiPerformanceManager();