重构项目结构:整理gitignore,移动source目录到根目录,统一依赖管理

This commit is contained in:
YHH
2025-06-09 14:51:26 +08:00
parent ec5f70ecfc
commit f2d3880a06
89 changed files with 3912 additions and 6820 deletions

81
src/Utils/Emitter.ts Normal file
View File

@@ -0,0 +1,81 @@
/**
* 用于包装事件的一个小类
*/
export class FuncPack {
/** 函数 */
public func: Function;
/** 上下文 */
public context: any;
constructor(func: Function, context: any) {
this.func = func;
this.context = context;
}
}
/**
* 用于事件管理
*/
export class Emitter<T> {
private _messageTable: Map<T, FuncPack[]>;
constructor() {
this._messageTable = new Map<T, FuncPack[]>();
}
/**
* 开始监听项
* @param eventType 监听类型
* @param handler 监听函数
* @param context 监听上下文
*/
public addObserver(eventType: T, handler: Function, context: any) {
let list = this._messageTable.get(eventType);
if (!list) {
list = [];
this._messageTable.set(eventType, list);
}
if (!this.hasObserver(eventType, handler)) {
list.push(new FuncPack(handler, context));
}
}
/**
* 移除监听项
* @param eventType 事件类型
* @param handler 事件函数
*/
public removeObserver(eventType: T, handler: Function) {
let messageData = this._messageTable.get(eventType);
if (messageData) {
let index = messageData.findIndex(data => data.func == handler);
if (index != -1)
messageData.splice(index, 1);
}
}
/**
* 触发该事件
* @param eventType 事件类型
* @param data 事件数据
*/
public emit(eventType: T, ...data: any[]) {
let list = this._messageTable.get(eventType);
if (list) {
for (let observer of list) {
observer.func.call(observer.context, ...data);
}
}
}
/**
* 判断是否存在该类型的观察者
* @param eventType 事件类型
* @param handler 事件函数
*/
public hasObserver(eventType: T, handler: Function): boolean {
let list = this._messageTable.get(eventType);
return list ? list.some(observer => observer.func === handler) : false;
}
}

View File

@@ -0,0 +1,15 @@
/**
* 数字扩展工具类
* 提供数字转换的实用方法
*/
export class NumberExtension {
/**
* 将值转换为数字
* @param value 要转换的值
* @returns 转换后的数字如果值为undefined则返回0
*/
public static toNumber(value: any): number {
if (value == undefined) return 0;
return Number(value);
}
}

View File

@@ -0,0 +1,14 @@
/**
* 类型工具类
* 提供类型相关的实用方法
*/
export class TypeUtils {
/**
* 获取对象的类型
* @param obj 对象
* @returns 对象的构造函数
*/
public static getType(obj: any) {
return obj.constructor;
}
}

View File

@@ -0,0 +1,3 @@
// 扩展工具类导出
export { TypeUtils } from './TypeUtils';
export { NumberExtension } from './NumberExtension';

View File

@@ -0,0 +1,55 @@
/**
* 全局管理器的基类。所有全局管理器都应该从此类继承。
*/
export class GlobalManager {
/**
* 表示管理器是否启用
*/
public _enabled: boolean = false;
/**
* 获取或设置管理器是否启用
*/
public get enabled() {
return this._enabled;
}
public set enabled(value: boolean) {
this.setEnabled(value);
}
/**
* 设置管理器是否启用
* @param isEnabled 如果为true则启用管理器否则禁用管理器
*/
public setEnabled(isEnabled: boolean) {
if (this._enabled != isEnabled) {
this._enabled = isEnabled;
if (this._enabled) {
// 如果启用了管理器则调用onEnabled方法
this.onEnabled();
} else {
// 如果禁用了管理器则调用onDisabled方法
this.onDisabled();
}
}
}
/**
* 在启用管理器时调用的回调方法
*/
protected onEnabled() {
}
/**
* 在禁用管理器时调用的回调方法
*/
protected onDisabled() {
}
/**
* 更新管理器状态的方法
*/
public update() {
}
}

View File

@@ -0,0 +1,403 @@
/**
* 性能监控数据
*/
export interface PerformanceData {
/** 系统名称 */
name: string;
/** 执行时间(毫秒) */
executionTime: number;
/** 处理的实体数量 */
entityCount: number;
/** 平均每个实体的处理时间 */
averageTimePerEntity: number;
/** 最后更新时间戳 */
lastUpdateTime: number;
/** 内存使用量(字节) */
memoryUsage?: number;
/** CPU使用率百分比 */
cpuUsage?: number;
}
/**
* 性能统计信息
*/
export interface PerformanceStats {
/** 总执行时间 */
totalTime: number;
/** 平均执行时间 */
averageTime: number;
/** 最小执行时间 */
minTime: number;
/** 最大执行时间 */
maxTime: number;
/** 执行次数 */
executionCount: number;
/** 最近的执行时间列表 */
recentTimes: number[];
/** 标准差 */
standardDeviation: number;
/** 95百分位数 */
percentile95: number;
/** 99百分位数 */
percentile99: number;
}
/**
* 性能警告类型
*/
export enum PerformanceWarningType {
HIGH_EXECUTION_TIME = 'high_execution_time',
HIGH_MEMORY_USAGE = 'high_memory_usage',
HIGH_CPU_USAGE = 'high_cpu_usage',
FREQUENT_GC = 'frequent_gc',
LOW_FPS = 'low_fps',
HIGH_ENTITY_COUNT = 'high_entity_count'
}
/**
* 性能警告
*/
export interface PerformanceWarning {
type: PerformanceWarningType;
systemName: string;
message: string;
severity: 'low' | 'medium' | 'high' | 'critical';
timestamp: number;
value: number;
threshold: number;
suggestion?: string;
}
/**
* 性能阈值配置
*/
export interface PerformanceThresholds {
/** 执行时间阈值(毫秒) */
executionTime: {
warning: number;
critical: number;
};
/** 内存使用阈值MB */
memoryUsage: {
warning: number;
critical: number;
};
/** CPU使用率阈值百分比 */
cpuUsage: {
warning: number;
critical: number;
};
/** FPS阈值 */
fps: {
warning: number;
critical: number;
};
/** 实体数量阈值 */
entityCount: {
warning: number;
critical: number;
};
}
/**
* 高性能监控器
* 用于监控ECS系统的性能表现提供详细的分析和优化建议
*/
export class PerformanceMonitor {
private static _instance: PerformanceMonitor;
private _systemData = new Map<string, PerformanceData>();
private _systemStats = new Map<string, PerformanceStats>();
private _warnings: PerformanceWarning[] = [];
private _isEnabled = false;
private _maxRecentSamples = 60; // 保留最近60帧的数据
private _maxWarnings = 100; // 最大警告数量
// 性能阈值配置
private _thresholds: PerformanceThresholds = {
executionTime: { warning: 16.67, critical: 33.33 }, // 60fps和30fps对应的帧时间
memoryUsage: { warning: 100, critical: 200 }, // MB
cpuUsage: { warning: 70, critical: 90 }, // 百分比
fps: { warning: 45, critical: 30 },
entityCount: { warning: 1000, critical: 5000 }
};
// FPS监控
private _fpsHistory: number[] = [];
private _lastFrameTime = 0;
private _frameCount = 0;
private _fpsUpdateInterval = 1000; // 1秒更新一次FPS
private _lastFpsUpdate = 0;
private _currentFps = 60;
// 内存监控
private _memoryCheckInterval = 5000; // 5秒检查一次内存
private _lastMemoryCheck = 0;
private _memoryHistory: number[] = [];
// GC监控
private _gcCount = 0;
private _lastGcCheck = 0;
private _gcCheckInterval = 1000;
/**
* 获取单例实例
*/
public static get instance(): PerformanceMonitor {
if (!PerformanceMonitor._instance) {
PerformanceMonitor._instance = new PerformanceMonitor();
}
return PerformanceMonitor._instance;
}
private constructor() {}
/**
* 启用性能监控
*/
public enable(): void {
this._isEnabled = true;
}
/**
* 禁用性能监控
*/
public disable(): void {
this._isEnabled = false;
}
/**
* 检查是否启用了性能监控
*/
public get isEnabled(): boolean {
return this._isEnabled;
}
/**
* 开始监控系统性能
* @param systemName 系统名称
* @returns 开始时间戳
*/
public startMonitoring(systemName: string): number {
if (!this._isEnabled) {
return 0;
}
return performance.now();
}
/**
* 结束监控并记录性能数据
* @param systemName 系统名称
* @param startTime 开始时间戳
* @param entityCount 处理的实体数量
*/
public endMonitoring(systemName: string, startTime: number, entityCount: number = 0): void {
if (!this._isEnabled || startTime === 0) {
return;
}
const endTime = performance.now();
const executionTime = endTime - startTime;
const averageTimePerEntity = entityCount > 0 ? executionTime / entityCount : 0;
// 更新当前性能数据
const data: PerformanceData = {
name: systemName,
executionTime,
entityCount,
averageTimePerEntity,
lastUpdateTime: endTime
};
this._systemData.set(systemName, data);
// 更新统计信息
this.updateStats(systemName, executionTime);
}
/**
* 更新系统统计信息
* @param systemName 系统名称
* @param executionTime 执行时间
*/
private updateStats(systemName: string, executionTime: number): void {
let stats = this._systemStats.get(systemName);
if (!stats) {
stats = {
totalTime: 0,
averageTime: 0,
minTime: Number.MAX_VALUE,
maxTime: 0,
executionCount: 0,
recentTimes: [],
standardDeviation: 0,
percentile95: 0,
percentile99: 0
};
this._systemStats.set(systemName, stats);
}
// 更新基本统计
stats.totalTime += executionTime;
stats.executionCount++;
stats.averageTime = stats.totalTime / stats.executionCount;
stats.minTime = Math.min(stats.minTime, executionTime);
stats.maxTime = Math.max(stats.maxTime, executionTime);
// 更新最近时间列表
stats.recentTimes.push(executionTime);
if (stats.recentTimes.length > this._maxRecentSamples) {
stats.recentTimes.shift();
}
// 计算高级统计信息
this.calculateAdvancedStats(stats);
}
/**
* 计算高级统计信息
* @param stats 统计信息对象
*/
private calculateAdvancedStats(stats: PerformanceStats): void {
if (stats.recentTimes.length === 0) return;
// 计算标准差
const mean = stats.recentTimes.reduce((a, b) => a + b, 0) / stats.recentTimes.length;
const variance = stats.recentTimes.reduce((acc, time) => acc + Math.pow(time - mean, 2), 0) / stats.recentTimes.length;
stats.standardDeviation = Math.sqrt(variance);
// 计算百分位数
const sortedTimes = [...stats.recentTimes].sort((a, b) => a - b);
const len = sortedTimes.length;
stats.percentile95 = sortedTimes[Math.floor(len * 0.95)] || 0;
stats.percentile99 = sortedTimes[Math.floor(len * 0.99)] || 0;
}
/**
* 获取系统的当前性能数据
* @param systemName 系统名称
* @returns 性能数据或undefined
*/
public getSystemData(systemName: string): PerformanceData | undefined {
return this._systemData.get(systemName);
}
/**
* 获取系统的统计信息
* @param systemName 系统名称
* @returns 统计信息或undefined
*/
public getSystemStats(systemName: string): PerformanceStats | undefined {
return this._systemStats.get(systemName);
}
/**
* 获取所有系统的性能数据
* @returns 所有系统的性能数据
*/
public getAllSystemData(): Map<string, PerformanceData> {
return new Map(this._systemData);
}
/**
* 获取所有系统的统计信息
* @returns 所有系统的统计信息
*/
public getAllSystemStats(): Map<string, PerformanceStats> {
return new Map(this._systemStats);
}
/**
* 获取性能报告
* @returns 格式化的性能报告字符串
*/
public getPerformanceReport(): string {
if (!this._isEnabled) {
return "Performance monitoring is disabled.";
}
const lines: string[] = [];
lines.push("=== ECS Performance Report ===");
lines.push("");
// 按平均执行时间排序
const sortedSystems = Array.from(this._systemStats.entries())
.sort((a, b) => b[1].averageTime - a[1].averageTime);
for (const [systemName, stats] of sortedSystems) {
const data = this._systemData.get(systemName);
lines.push(`System: ${systemName}`);
lines.push(` Current: ${data?.executionTime.toFixed(2)}ms (${data?.entityCount} entities)`);
lines.push(` Average: ${stats.averageTime.toFixed(2)}ms`);
lines.push(` Min/Max: ${stats.minTime.toFixed(2)}ms / ${stats.maxTime.toFixed(2)}ms`);
lines.push(` Total: ${stats.totalTime.toFixed(2)}ms (${stats.executionCount} calls)`);
if (data?.averageTimePerEntity && data.averageTimePerEntity > 0) {
lines.push(` Per Entity: ${data.averageTimePerEntity.toFixed(4)}ms`);
}
lines.push("");
}
// 总体统计
const totalCurrentTime = Array.from(this._systemData.values())
.reduce((sum, data) => sum + data.executionTime, 0);
lines.push(`Total Frame Time: ${totalCurrentTime.toFixed(2)}ms`);
lines.push(`Systems Count: ${this._systemData.size}`);
return lines.join('\n');
}
/**
* 重置所有性能数据
*/
public reset(): void {
this._systemData.clear();
this._systemStats.clear();
}
/**
* 重置指定系统的性能数据
* @param systemName 系统名称
*/
public resetSystem(systemName: string): void {
this._systemData.delete(systemName);
this._systemStats.delete(systemName);
}
/**
* 获取性能警告
* @param thresholdMs 警告阈值(毫秒)
* @returns 超过阈值的系统列表
*/
public getPerformanceWarnings(thresholdMs: number = 16.67): string[] {
const warnings: string[] = [];
for (const [systemName, data] of this._systemData.entries()) {
if (data.executionTime > thresholdMs) {
warnings.push(`${systemName}: ${data.executionTime.toFixed(2)}ms (>${thresholdMs}ms)`);
}
}
return warnings;
}
/**
* 设置最大保留样本数
* @param maxSamples 最大样本数
*/
public setMaxRecentSamples(maxSamples: number): void {
this._maxRecentSamples = maxSamples;
// 裁剪现有数据
for (const stats of this._systemStats.values()) {
while (stats.recentTimes.length > maxSamples) {
stats.recentTimes.shift();
}
}
}
}

565
src/Utils/Pool.ts Normal file
View File

@@ -0,0 +1,565 @@
/**
* 可池化对象接口
*/
export interface IPoolable {
/**
* 重置对象状态,准备重用
*/
reset(): void;
}
/**
* 对象池统计信息
*/
export interface PoolStats {
/** 池中对象数量 */
size: number;
/** 池的最大大小 */
maxSize: number;
/** 总共创建的对象数量 */
totalCreated: number;
/** 总共获取的次数 */
totalObtained: number;
/** 总共释放的次数 */
totalReleased: number;
/** 命中率(从池中获取的比例) */
hitRate: number;
/** 内存使用估算(字节) */
estimatedMemoryUsage: number;
}
/**
* 高性能通用对象池
* 支持任意类型的对象池化,包含详细的统计信息
*/
export class Pool<T extends IPoolable> {
private static _pools = new Map<Function, Pool<any>>();
private _objects: T[] = [];
private _createFn: () => T;
private _maxSize: number;
private _stats: PoolStats;
private _objectSize: number; // 估算的单个对象大小
/**
* 构造函数
* @param createFn 创建对象的函数
* @param maxSize 池的最大大小默认100
* @param estimatedObjectSize 估算的单个对象大小字节默认1024
*/
constructor(createFn: () => T, maxSize: number = 100, estimatedObjectSize: number = 1024) {
this._createFn = createFn;
this._maxSize = maxSize;
this._objectSize = estimatedObjectSize;
this._stats = {
size: 0,
maxSize,
totalCreated: 0,
totalObtained: 0,
totalReleased: 0,
hitRate: 0,
estimatedMemoryUsage: 0
};
}
/**
* 获取指定类型的对象池
* @param type 对象类型
* @param maxSize 池的最大大小
* @param estimatedObjectSize 估算的单个对象大小
* @returns 对象池实例
*/
public static getPool<T extends IPoolable>(
type: new (...args: any[]) => T,
maxSize: number = 100,
estimatedObjectSize: number = 1024
): Pool<T> {
let pool = this._pools.get(type);
if (!pool) {
pool = new Pool<T>(() => new type(), maxSize, estimatedObjectSize);
this._pools.set(type, pool);
}
return pool;
}
/**
* 从池中获取对象
* @returns 对象实例
*/
public obtain(): T {
this._stats.totalObtained++;
if (this._objects.length > 0) {
const obj = this._objects.pop()!;
this._stats.size--;
this._updateHitRate();
this._updateMemoryUsage();
return obj;
}
// 池中没有对象,创建新的
const obj = this._createFn();
this._stats.totalCreated++;
this._updateHitRate();
return obj;
}
/**
* 将对象归还到池中
* @param obj 要归还的对象
*/
public free(obj: T): void {
if (this._objects.length < this._maxSize) {
obj.reset();
this._objects.push(obj);
this._stats.size++;
this._stats.totalReleased++;
this._updateMemoryUsage();
}
// 如果池已满对象会被丢弃由GC回收
}
/**
* 预热池,创建指定数量的对象
* @param count 要创建的对象数量
*/
public warmUp(count: number): void {
const targetSize = Math.min(count, this._maxSize);
while (this._objects.length < targetSize) {
const obj = this._createFn();
this._stats.totalCreated++;
this._objects.push(obj);
this._stats.size++;
}
this._updateMemoryUsage();
}
/**
* 清空池
*/
public clear(): void {
this._objects.length = 0;
this._stats.size = 0;
this._updateMemoryUsage();
}
/**
* 获取池中对象数量
*/
public get size(): number {
return this._objects.length;
}
/**
* 获取池的最大大小
*/
public get maxSize(): number {
return this._maxSize;
}
/**
* 设置池的最大大小
*/
public set maxSize(value: number) {
this._maxSize = value;
this._stats.maxSize = value;
// 如果当前池大小超过新的最大值,则移除多余的对象
while (this._objects.length > this._maxSize) {
this._objects.pop();
this._stats.size--;
}
this._updateMemoryUsage();
}
/**
* 获取池的统计信息
*/
public getStats(): PoolStats {
return { ...this._stats };
}
/**
* 重置统计信息
*/
public resetStats(): void {
this._stats.totalCreated = 0;
this._stats.totalObtained = 0;
this._stats.totalReleased = 0;
this._stats.hitRate = 0;
}
/**
* 更新命中率
*/
private _updateHitRate(): void {
if (this._stats.totalObtained > 0) {
const hits = this._stats.totalObtained - this._stats.totalCreated;
this._stats.hitRate = hits / this._stats.totalObtained;
}
}
/**
* 更新内存使用估算
*/
private _updateMemoryUsage(): void {
this._stats.estimatedMemoryUsage = this._stats.size * this._objectSize;
}
/**
* 静态方法:从指定类型的池中获取对象
* @param type 对象类型
* @returns 对象实例
*/
public static obtain<T extends IPoolable>(type: new (...args: any[]) => T): T {
return this.getPool(type).obtain();
}
/**
* 静态方法:将对象归还到对应类型的池中
* @param type 对象类型
* @param obj 要归还的对象
*/
public static free<T extends IPoolable>(type: new (...args: any[]) => T, obj: T): void {
this.getPool(type).free(obj);
}
/**
* 静态方法:预热指定类型的池
* @param type 对象类型
* @param count 要创建的对象数量
*/
public static warmUp<T extends IPoolable>(type: new (...args: any[]) => T, count: number): void {
this.getPool(type).warmUp(count);
}
/**
* 静态方法:清空指定类型的池
* @param type 对象类型
*/
public static clearPool<T extends IPoolable>(type: new (...args: any[]) => T): void {
const pool = this._pools.get(type);
if (pool) {
pool.clear();
}
}
/**
* 静态方法:清空所有池
*/
public static clearAllPools(): void {
for (const pool of this._pools.values()) {
pool.clear();
}
this._pools.clear();
}
/**
* 静态方法:获取池的统计信息
* @returns 池的统计信息
*/
public static getStats(): { [typeName: string]: PoolStats } {
const stats: { [typeName: string]: PoolStats } = {};
for (const [type, pool] of this._pools.entries()) {
const typeName = (type as any).name || 'Unknown';
stats[typeName] = pool.getStats();
}
return stats;
}
/**
* 静态方法:获取所有池的总内存使用量
* @returns 总内存使用量(字节)
*/
public static getTotalMemoryUsage(): number {
let total = 0;
for (const pool of this._pools.values()) {
total += pool.getStats().estimatedMemoryUsage;
}
return total;
}
/**
* 静态方法:获取性能报告
* @returns 格式化的性能报告
*/
public static getPerformanceReport(): string {
const stats = this.getStats();
const lines: string[] = [];
lines.push('=== Object Pool Performance Report ===');
lines.push(`Total Memory Usage: ${(this.getTotalMemoryUsage() / 1024 / 1024).toFixed(2)} MB`);
lines.push('');
for (const [typeName, stat] of Object.entries(stats)) {
lines.push(`${typeName}:`);
lines.push(` Size: ${stat.size}/${stat.maxSize}`);
lines.push(` Hit Rate: ${(stat.hitRate * 100).toFixed(1)}%`);
lines.push(` Total Created: ${stat.totalCreated}`);
lines.push(` Total Obtained: ${stat.totalObtained}`);
lines.push(` Memory: ${(stat.estimatedMemoryUsage / 1024).toFixed(1)} KB`);
lines.push('');
}
return lines.join('\n');
}
}
/**
* 分层对象池
* 使用多个不同大小的池来优化内存使用
*/
export class TieredObjectPool<T extends IPoolable> {
private pools: Pool<T>[] = [];
private createFn: () => T;
private resetFn: (obj: T) => void;
private tierSizes: number[];
private totalObtained = 0;
private totalReleased = 0;
/**
* 构造函数
* @param createFn 创建对象的函数
* @param resetFn 重置对象的函数
* @param tierSizes 各层级的大小,默认[10, 50, 200]
* @param estimatedObjectSize 估算的单个对象大小
*/
constructor(
createFn: () => T,
resetFn: (obj: T) => void,
tierSizes: number[] = [10, 50, 200],
estimatedObjectSize: number = 1024
) {
this.createFn = createFn;
this.resetFn = resetFn;
this.tierSizes = tierSizes;
// 初始化不同层级的池
for (const size of tierSizes) {
this.pools.push(new Pool(createFn, size, estimatedObjectSize));
}
}
/**
* 获取对象
* @returns 对象实例
*/
public obtain(): T {
this.totalObtained++;
// 从最小的池开始尝试获取
for (const pool of this.pools) {
if (pool.size > 0) {
return pool.obtain();
}
}
// 所有池都空了,创建新对象
return this.createFn();
}
/**
* 释放对象
* @param obj 要释放的对象
*/
public release(obj: T): void {
this.totalReleased++;
this.resetFn(obj);
// 放入第一个有空间的池
for (const pool of this.pools) {
if (pool.size < pool.maxSize) {
pool.free(obj);
return;
}
}
// 所有池都满了,直接丢弃
}
/**
* 预热所有池
* @param totalCount 总预热数量
*/
public warmUp(totalCount: number): void {
let remaining = totalCount;
for (const pool of this.pools) {
const warmUpCount = Math.min(remaining, pool.maxSize);
pool.warmUp(warmUpCount);
remaining -= warmUpCount;
if (remaining <= 0) break;
}
}
/**
* 清空所有池
*/
public clear(): void {
for (const pool of this.pools) {
pool.clear();
}
}
/**
* 获取统计信息
*/
public getStats(): {
totalSize: number;
totalMaxSize: number;
totalMemoryUsage: number;
tierStats: PoolStats[];
hitRate: number;
} {
let totalSize = 0;
let totalMaxSize = 0;
let totalMemoryUsage = 0;
const tierStats: PoolStats[] = [];
for (const pool of this.pools) {
const stats = pool.getStats();
tierStats.push(stats);
totalSize += stats.size;
totalMaxSize += stats.maxSize;
totalMemoryUsage += stats.estimatedMemoryUsage;
}
const hitRate = this.totalObtained > 0 ?
(this.totalObtained - this.getTotalCreated()) / this.totalObtained : 0;
return {
totalSize,
totalMaxSize,
totalMemoryUsage,
tierStats,
hitRate
};
}
/**
* 获取总创建数量
*/
private getTotalCreated(): number {
return this.pools.reduce((total, pool) => total + pool.getStats().totalCreated, 0);
}
}
/**
* 池管理器
* 统一管理所有对象池
*/
export class PoolManager {
private static instance: PoolManager;
private pools = new Map<string, Pool<any> | TieredObjectPool<any>>();
private autoCompactInterval = 60000; // 60秒
private lastCompactTime = 0;
public static getInstance(): PoolManager {
if (!PoolManager.instance) {
PoolManager.instance = new PoolManager();
}
return PoolManager.instance;
}
/**
* 注册池
* @param name 池名称
* @param pool 池实例
*/
public registerPool<T extends IPoolable>(name: string, pool: Pool<T> | TieredObjectPool<T>): void {
this.pools.set(name, pool);
}
/**
* 获取池
* @param name 池名称
* @returns 池实例
*/
public getPool<T extends IPoolable>(name: string): Pool<T> | TieredObjectPool<T> | null {
return this.pools.get(name) || null;
}
/**
* 更新池管理器(应在游戏循环中调用)
*/
public update(): void {
const now = Date.now();
if (now - this.lastCompactTime > this.autoCompactInterval) {
this.compactAllPools();
this.lastCompactTime = now;
}
}
/**
* 压缩所有池(清理碎片)
*/
public compactAllPools(): void {
// 对于标准池,可以考虑清理一些长时间未使用的对象
// 这里简单实现为重置统计信息
for (const pool of this.pools.values()) {
if (pool instanceof Pool) {
pool.resetStats();
}
}
}
/**
* 获取所有池的统计信息
*/
public getAllStats(): Map<string, any> {
const stats = new Map<string, any>();
for (const [name, pool] of this.pools.entries()) {
if (pool instanceof Pool) {
stats.set(name, pool.getStats());
} else if (pool instanceof TieredObjectPool) {
stats.set(name, pool.getStats());
}
}
return stats;
}
/**
* 生成性能报告
*/
public generateReport(): string {
const lines: string[] = [];
lines.push('=== Pool Manager Report ===');
let totalMemory = 0;
for (const [name, pool] of this.pools.entries()) {
lines.push(`\n${name}:`);
if (pool instanceof Pool) {
const stats = pool.getStats();
lines.push(` Type: Standard Pool`);
lines.push(` Size: ${stats.size}/${stats.maxSize}`);
lines.push(` Hit Rate: ${(stats.hitRate * 100).toFixed(1)}%`);
lines.push(` Memory: ${(stats.estimatedMemoryUsage / 1024).toFixed(1)} KB`);
totalMemory += stats.estimatedMemoryUsage;
} else if (pool instanceof TieredObjectPool) {
const stats = pool.getStats();
lines.push(` Type: Tiered Pool`);
lines.push(` Total Size: ${stats.totalSize}/${stats.totalMaxSize}`);
lines.push(` Hit Rate: ${(stats.hitRate * 100).toFixed(1)}%`);
lines.push(` Memory: ${(stats.totalMemoryUsage / 1024).toFixed(1)} KB`);
totalMemory += stats.totalMemoryUsage;
}
}
lines.push(`\nTotal Memory Usage: ${(totalMemory / 1024 / 1024).toFixed(2)} MB`);
return lines.join('\n');
}
}

92
src/Utils/Time.ts Normal file
View File

@@ -0,0 +1,92 @@
/**
* 时间管理工具类
* 提供游戏时间相关的功能,包括帧时间、总时间、时间缩放等
*/
export class Time {
/**
* 上一帧到当前帧的时间间隔(秒)
*/
public static deltaTime: number = 0;
/**
* 未缩放的帧时间间隔(秒)
*/
public static unscaledDeltaTime: number = 0;
/**
* 游戏开始以来的总时间(秒)
*/
public static totalTime: number = 0;
/**
* 未缩放的总时间(秒)
*/
public static unscaledTotalTime: number = 0;
/**
* 时间缩放比例
*/
public static timeScale: number = 1;
/**
* 当前帧数
*/
public static frameCount: number = 0;
/**
* 上一帧的时间戳
*/
private static _lastTime: number = 0;
/**
* 是否为第一次更新
*/
private static _isFirstUpdate: boolean = true;
/**
* 更新时间信息
* @param currentTime 当前时间戳(毫秒)
*/
public static update(currentTime: number = -1): void {
if (currentTime === -1) {
currentTime = Date.now();
}
if (this._isFirstUpdate) {
this._lastTime = currentTime;
this._isFirstUpdate = false;
return;
}
// 计算帧时间间隔(转换为秒)
this.unscaledDeltaTime = (currentTime - this._lastTime) / 1000;
this.deltaTime = this.unscaledDeltaTime * this.timeScale;
// 更新总时间
this.unscaledTotalTime += this.unscaledDeltaTime;
this.totalTime += this.deltaTime;
// 更新帧数
this.frameCount++;
// 记录当前时间
this._lastTime = currentTime;
}
/**
* 场景改变时重置时间
*/
public static sceneChanged(): void {
this._isFirstUpdate = true;
}
/**
* 检查指定的时间间隔是否已经过去
* @param interval 时间间隔(秒)
* @param lastTime 上次检查的时间
* @returns 是否已经过去指定时间
*/
public static checkEvery(interval: number, lastTime: number): boolean {
return this.totalTime - lastTime >= interval;
}
}

View File

@@ -0,0 +1,18 @@
export interface ITimer {
context: any;
/**
* 调用stop以停止此计时器再次运行。这对非重复计时器没有影响。
*/
stop(): void;
/**
* 将计时器的运行时间重置为0
*/
reset(): void;
/**
* 返回投向T的上下文作为方便
*/
getContext<T>(): T;
}

56
src/Utils/Timers/Timer.ts Normal file
View File

@@ -0,0 +1,56 @@
import { ITimer } from './ITimer';
import { Time } from '../Time';
/**
* 私有类隐藏ITimer的实现
*/
export class Timer implements ITimer{
public context: any;
public _timeInSeconds: number = 0;
public _repeats: boolean = false;
public _onTime!: (timer: ITimer) => void;
public _isDone: boolean = false;
public _elapsedTime: number = 0;
public getContext<T>(): T {
return this.context as T;
}
public reset(): void {
this._elapsedTime = 0;
}
public stop(): void {
this._isDone = true;
}
public tick(){
// 如果stop在tick之前被调用那么isDone将为true我们不应该再做任何事情
if (!this._isDone && this._elapsedTime > this._timeInSeconds){
this._elapsedTime -= this._timeInSeconds;
this._onTime(this);
if (!this._isDone && !this._repeats)
this._isDone = true;
}
this._elapsedTime += Time.deltaTime;
return this._isDone;
}
public initialize(timeInsSeconds: number, repeats: boolean, context: any, onTime: (timer: ITimer)=>void){
this._timeInSeconds = timeInsSeconds;
this._repeats = repeats;
this.context = context;
this._onTime = onTime.bind(context);
}
/**
* 空出对象引用以便在js需要时GC可以清理它们的引用
*/
public unload(){
this.context = null;
this._onTime = null as any;
}
}

View File

@@ -0,0 +1,34 @@
import { GlobalManager } from '../GlobalManager';
import { Timer } from './Timer';
import { ITimer } from './ITimer';
/**
* 允许动作的延迟和重复执行
*/
export class TimerManager extends GlobalManager {
public _timers: Timer[] = [];
public override update() {
for (let i = this._timers.length - 1; i >= 0; i --){
if (this._timers[i].tick()){
this._timers[i].unload();
this._timers.splice(i, 1);
}
}
}
/**
* 调度一个一次性或重复的计时器,该计时器将调用已传递的动作
* @param timeInSeconds
* @param repeats
* @param context
* @param onTime
*/
public schedule(timeInSeconds: number, repeats: boolean, context: any, onTime: (timer: ITimer)=>void){
let timer = new Timer();
timer.initialize(timeInSeconds, repeats, context, onTime);
this._timers.push(timer);
return timer;
}
}

8
src/Utils/WasmCore.ts Normal file
View File

@@ -0,0 +1,8 @@
/**
* WASM ECS核心模块
*
* 提供高性能的ECS操作支持WASM和JavaScript双重实现
*/
export * from './Wasm';
export type Query = import('./Wasm').QueryResult;

17
src/Utils/index.ts Normal file
View File

@@ -0,0 +1,17 @@
export * from './Extensions';
export * from './Pool';
export * from './Emitter';
export * from './GlobalManager';
export * from './PerformanceMonitor';
export { Time } from './Time';
export {
WasmEcsCore,
ecsCore,
initializeEcs,
EntityId,
ComponentMask,
QueryResult,
PerformanceStats as WasmPerformanceStats,
WasmLoader,
JavaScriptFallback
} from './Wasm';