perf: 优化战斗系统内存与性能,增加对象池限制与内存监控面板
- 为Skill和Monster对象池添加最大容量限制(64/24),防止内存泄漏 - 实现DamageQueueComp的环形队列优化,减少数组操作开销 - 在MissionComp中添加内存监控面板,实时显示堆内存、实体数量、对象池状态 - 优化MoveSystem的渲染排序性能,缓存查询结果减少GC压力 - 调整角色控制器UI位置与样式,关闭调试日志减少性能开销 - 战斗结束时自动清理对象池,确保内存可回收
This commit is contained in:
@@ -19,6 +19,7 @@ export class Skill extends ecs.Entity {
|
||||
private debugMode: boolean = false;
|
||||
/** 多键对象池:Map<prefabPath, NodePool> */
|
||||
static pools: Map<string, NodePool> = new Map();
|
||||
static readonly MAX_POOL_SIZE: number = 64;
|
||||
|
||||
static getFromPool(path: string): Node | null {
|
||||
if (this.pools.has(path)) {
|
||||
@@ -38,7 +39,31 @@ export class Skill extends ecs.Entity {
|
||||
if (!this.pools.has(path)) {
|
||||
this.pools.set(path, new NodePool());
|
||||
}
|
||||
this.pools.get(path)!.put(node);
|
||||
const pool = this.pools.get(path)!;
|
||||
if (pool.size() >= this.MAX_POOL_SIZE) {
|
||||
node.destroy();
|
||||
return;
|
||||
}
|
||||
pool.put(node);
|
||||
}
|
||||
|
||||
static clearPools() {
|
||||
this.pools.forEach((pool) => {
|
||||
pool.clear();
|
||||
});
|
||||
this.pools.clear();
|
||||
}
|
||||
|
||||
static getPoolStats() {
|
||||
let total = 0;
|
||||
this.pools.forEach((pool) => {
|
||||
total += pool.size();
|
||||
});
|
||||
return {
|
||||
paths: this.pools.size,
|
||||
total,
|
||||
maxPerPath: this.MAX_POOL_SIZE
|
||||
};
|
||||
}
|
||||
|
||||
/** ---------- 数据层 ---------- */
|
||||
|
||||
@@ -21,7 +21,7 @@ export class SkillView extends CCComp {
|
||||
atk_y: number = 0
|
||||
|
||||
@property({ tooltip: "是否启用调试日志" })
|
||||
private debugMode: boolean = true;
|
||||
private debugMode: boolean = false;
|
||||
|
||||
anim:Animation=null;
|
||||
group:number=0;
|
||||
@@ -65,13 +65,14 @@ export class SkillView extends CCComp {
|
||||
}
|
||||
if (this.isDisposing) return;
|
||||
if (!this.node || !this.node.activeInHierarchy) return;
|
||||
// 安全获取双方信息用于日志
|
||||
const casterName = this.sData.caster?.ent?.get(HeroAttrsComp)?.hero_name ?? '未知施法者';
|
||||
const casterEid = this.sData.casterEid;
|
||||
const targetView = oCol.getComponent(HeroViewComp);
|
||||
const targetName = targetView?.ent?.get(HeroAttrsComp)?.hero_name ?? '非英雄对象';
|
||||
const targetEid = targetView?.ent?.eid ?? '未知EID';
|
||||
mLogger.log(this.debugMode, 'SkillView', `[skillView] 碰撞1 [${this.sData.caster.box_group}][${casterName}][${casterEid}]的[${seCol.group}]:[${this.SConf.name}][${this.ent.eid}]碰撞了 [${oCol.group}]:[ ${targetName}][${targetEid}]`);
|
||||
if (this.debugMode) {
|
||||
const casterName = this.sData.caster?.ent?.get(HeroAttrsComp)?.hero_name ?? '未知施法者';
|
||||
const casterEid = this.sData.casterEid;
|
||||
const targetName = targetView?.ent?.get(HeroAttrsComp)?.hero_name ?? '非英雄对象';
|
||||
const targetEid = targetView?.ent?.eid ?? '未知EID';
|
||||
mLogger.log(this.debugMode, 'SkillView', `[skillView] 碰撞1 [${this.sData.caster.box_group}][${casterName}][${casterEid}]的[${seCol.group}]:[${this.SConf.name}][${this.ent.eid}]碰撞了 [${oCol.group}]:[ ${targetName}][${targetEid}]`);
|
||||
}
|
||||
if (oCol.group === seCol.group) return;
|
||||
if (this.pendingDisableCollider) return;
|
||||
if (this.sData.hit_count >= this.sData.max_hit_count) {
|
||||
@@ -91,7 +92,9 @@ export class SkillView extends CCComp {
|
||||
return;
|
||||
}
|
||||
let model = targetView.ent.get(HeroAttrsComp);
|
||||
mLogger.log(this.debugMode, 'SkillView', `[skillView] 碰撞3`, oCol.group, seCol.group, model);
|
||||
if (this.debugMode) {
|
||||
mLogger.log(this.debugMode, 'SkillView', `[skillView] 碰撞3`, oCol.group, seCol.group, model);
|
||||
}
|
||||
if (!model) return;
|
||||
if (model.is_dead) return;
|
||||
if (this.sData.fac == model.fac) return;
|
||||
@@ -126,11 +129,11 @@ export class SkillView extends CCComp {
|
||||
if (!target.ent) return;
|
||||
if (!this.sData) return;
|
||||
if (!this.SConf) return;
|
||||
// 安全获取名称,防止实体销毁导致的空指针异常
|
||||
const casterName = this.sData.caster?.ent?.get(HeroAttrsComp)?.hero_name ?? "未知施法者";
|
||||
const targetName = target.ent.get(HeroAttrsComp)?.hero_name ?? "未知目标";
|
||||
|
||||
mLogger.log(this.debugMode, 'SkillView', `[skillView] 伤害 [${this.group}][${casterName}][${this.sData.casterEid}]的 [${this.SConf.name}]对 [${target.box_group}][ ${targetName}][${target.ent.eid}]`);
|
||||
if (this.debugMode) {
|
||||
const casterName = this.sData.caster?.ent?.get(HeroAttrsComp)?.hero_name ?? "未知施法者";
|
||||
const targetName = target.ent.get(HeroAttrsComp)?.hero_name ?? "未知目标";
|
||||
mLogger.log(this.debugMode, 'SkillView', `[skillView] 伤害 [${this.group}][${casterName}][${this.sData.casterEid}]的 [${this.SConf.name}]对 [${target.box_group}][ ${targetName}][${target.ent.eid}]`);
|
||||
}
|
||||
// 使用伤害队列系统处理伤害
|
||||
DamageQueueHelper.addDamageToEntity(
|
||||
target.ent,
|
||||
|
||||
Reference in New Issue
Block a user