perf: 优化战斗系统内存与性能,增加对象池限制与内存监控面板

- 为Skill和Monster对象池添加最大容量限制(64/24),防止内存泄漏
- 实现DamageQueueComp的环形队列优化,减少数组操作开销
- 在MissionComp中添加内存监控面板,实时显示堆内存、实体数量、对象池状态
- 优化MoveSystem的渲染排序性能,缓存查询结果减少GC压力
- 调整角色控制器UI位置与样式,关闭调试日志减少性能开销
- 战斗结束时自动清理对象池,确保内存可回收
This commit is contained in:
panw
2026-03-16 18:49:43 +08:00
parent fb7b10b7e1
commit 5d24dbff29
8 changed files with 285 additions and 63 deletions

View File

@@ -38,6 +38,7 @@ export interface DamageEvent {
export class DamageQueueComp extends ecs.Comp {
/** 伤害事件队列 */
damageEvents: DamageEvent[] = [];
private readIndex: number = 0;
/** 队列创建时间 */
createTime: number = 0;
@@ -49,7 +50,8 @@ export class DamageQueueComp extends ecs.Comp {
processedCount: number = 0;
reset() {
this.damageEvents = [];
this.damageEvents.length = 0;
this.readIndex = 0;
this.createTime = 0;
this.isProcessing = false;
this.processedCount = 0;
@@ -60,14 +62,13 @@ export class DamageQueueComp extends ecs.Comp {
*/
addDamageEvent(attrs: any, casterEid: number, s_uuid: number,ext_dmg:number=0,dmg_ratio:number=1): void {
const timestamp = Date.now();
const eventId = `${casterEid}_${s_uuid}_${timestamp}_${Math.random()}`;
const damageEvent: DamageEvent = {
Attrs: attrs ? { ...attrs } : null, // 深拷贝属性数据
Attrs: attrs || null,
casterEid: casterEid,
s_uuid: s_uuid,
timestamp: timestamp,
eventId: eventId,
eventId: "",
ext_dmg:ext_dmg,
dmg_ratio:dmg_ratio,
};
@@ -75,7 +76,7 @@ export class DamageQueueComp extends ecs.Comp {
this.damageEvents.push(damageEvent);
// 如果是第一个伤害事件,记录创建时间
if (this.damageEvents.length === 1) {
if (this.getRemainingCount() === 1) {
this.createTime = timestamp;
}
}
@@ -84,29 +85,36 @@ export class DamageQueueComp extends ecs.Comp {
* 获取下一个待处理的伤害事件
*/
getNextDamageEvent(): DamageEvent | null {
if (this.damageEvents.length === 0) return null;
return this.damageEvents.shift() || null;
if (this.readIndex >= this.damageEvents.length) return null;
const event = this.damageEvents[this.readIndex];
this.readIndex += 1;
if (this.readIndex >= 32 && this.readIndex * 2 >= this.damageEvents.length) {
this.damageEvents = this.damageEvents.slice(this.readIndex);
this.readIndex = 0;
}
return event || null;
}
/**
* 获取队列中剩余的伤害事件数量
*/
getRemainingCount(): number {
return this.damageEvents.length;
return this.damageEvents.length - this.readIndex;
}
/**
* 检查队列是否为空
*/
isEmpty(): boolean {
return this.damageEvents.length === 0;
return this.readIndex >= this.damageEvents.length;
}
/**
* 清空队列
*/
clear(): void {
this.damageEvents = [];
this.damageEvents.length = 0;
this.readIndex = 0;
this.processedCount = 0;
this.isProcessing = false;
}
@@ -115,6 +123,10 @@ export class DamageQueueComp extends ecs.Comp {
* 按时间戳排序伤害事件(可选功能)
*/
sortByTimestamp(): void {
if (this.readIndex > 0) {
this.damageEvents = this.damageEvents.slice(this.readIndex);
this.readIndex = 0;
}
this.damageEvents.sort((a, b) => a.timestamp - b.timestamp);
}
@@ -123,6 +135,10 @@ export class DamageQueueComp extends ecs.Comp {
*/
removeDuplicates(): void {
const seen = new Set<string>();
if (this.readIndex > 0) {
this.damageEvents = this.damageEvents.slice(this.readIndex);
this.readIndex = 0;
}
this.damageEvents = this.damageEvents.filter(event => {
if (seen.has(event.eventId)) {
return false;
@@ -145,7 +161,7 @@ export class DamageQueueComp extends ecs.Comp {
return {
totalEvents: this.processedCount + this.damageEvents.length,
processedEvents: this.processedCount,
remainingEvents: this.damageEvents.length,
remainingEvents: this.getRemainingCount(),
isProcessing: this.isProcessing,
createTime: this.createTime
};
@@ -187,4 +203,4 @@ export class DamageQueueHelper {
const damageQueue = entity.get(DamageQueueComp);
return damageQueue ? damageQueue.getQueueStats() : null;
}
}
}