From 509539760d5744c23899c4127c6c49247c7df2c6 Mon Sep 17 00:00:00 2001 From: panw Date: Fri, 28 Nov 2025 10:29:54 +0800 Subject: [PATCH] =?UTF-8?q?refactor(hero):=20=E4=BC=98=E5=8C=96=E8=B0=83?= =?UTF-8?q?=E8=AF=95=E6=97=A5=E5=BF=97=E8=BE=93=E5=87=BA=E5=B9=B6=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E8=B0=83=E8=AF=95=E5=B7=A5=E5=85=B7=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加条件日志方法 debugLog 和 debugWarn 来统一管理调试输出 将 HeroAtkSystem 中的 console.log 调用改为条件输出 启用 HeroViewComp 的调试模式以便开发时查看日志 --- assets/script/game/hero/HeroAtkSystem.ts | 6 ++--- assets/script/game/hero/HeroViewComp.ts | 28 +++++++++++++++++++----- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/assets/script/game/hero/HeroAtkSystem.ts b/assets/script/game/hero/HeroAtkSystem.ts index 96c0616e..e85024be 100644 --- a/assets/script/game/hero/HeroAtkSystem.ts +++ b/assets/script/game/hero/HeroAtkSystem.ts @@ -151,7 +151,7 @@ export class HeroAtkSystem extends ecs.ComblockSystem implements ecs.ISystemUpd const isCrit = this.checkChance(damageEvent.Attrs[Attrs.CRITICAL]-TAttrsComp.Attrs[Attrs.CRITICAL_RESIST]); // 计算基础伤害 let damage = this.dmgCount(damageEvent,TAttrsComp); - console.log("[HeroAtkSystem] dmgCount",damage) + if (this.debugMode) console.log("[HeroAtkSystem] dmgCount",damage) if (isCrit) { // 暴击伤害计算 // 使用施法者的暴击伤害加成属性(damageEvent.Attrs 快照) @@ -162,10 +162,10 @@ export class HeroAtkSystem extends ecs.ComblockSystem implements ecs.ISystemUpd CAttrsComp?.useValueTalByAttr(Attrs.CRITICAL); // 清除施法者的暴击buff } - console.log("[HeroAtkSystem] after crit",damage) + if (this.debugMode) console.log("[HeroAtkSystem] after crit",damage) // 护盾吸收 damage =Math.floor(this.absorbShield(TAttrsComp, damage)) - console.log("[HeroAtkSystem] after shield",damage) + if (this.debugMode) console.log("[HeroAtkSystem] after shield",damage) if (damage <= 0) return reDate; // 应用伤害到数据层 TAttrsComp.hp -= damage; diff --git a/assets/script/game/hero/HeroViewComp.ts b/assets/script/game/hero/HeroViewComp.ts index 07e3dffb..f3d8b018 100644 --- a/assets/script/game/hero/HeroViewComp.ts +++ b/assets/script/game/hero/HeroViewComp.ts @@ -26,7 +26,20 @@ export interface BuffInfo { @ccclass('HeroViewComp') // 定义Cocos Creator 组件 @ecs.register('HeroView', false) // 定义ECS 组件 export class HeroViewComp extends CCComp { - private debugMode: boolean = false; // 是否启用调试模式 + private debugMode: boolean = true; // 是否启用调试模式 + + // 添加条件日志方法 + private debugLog(...args: any[]): void { + if (this.debugMode) { + console.log(...args); + } + } + + private debugWarn(...args: any[]): void { + if (this.debugMode) { + console.warn(...args); + } + } // ==================== View 层属性(表现相关)==================== as: HeroSpine = null! status:String = "idle" @@ -44,7 +57,7 @@ export class HeroViewComp extends CCComp { get model() { // 🔥 修复:添加安全检查,防止ent为null时的访问异常 if (!this.ent) { - console.warn("[HeroViewComp] ent is null, returning null for model"); + this.debugWarn("[HeroViewComp] ent is null, returning null for model"); return null; } return this.ent.get(HeroAttrsComp); @@ -161,7 +174,7 @@ export class HeroViewComp extends CCComp { // 不再基于血量是否满来决定显示状态,只更新进度条 let hp=this.model.hp; let hp_max=this.model.Attrs[Attrs.HP_MAX]; - console.log("hp_show",hp,hp_max) + this.debugLog("hp_show",hp,hp_max) this.top_node.getChildByName("hp").getComponent(ProgressBar).progress = hp / hp_max;; this.scheduleOnce(() => { this.top_node.getChildByName("hp").getChildByName("hpb").getComponent(ProgressBar).progress = hp / hp_max;; @@ -173,7 +186,7 @@ export class HeroViewComp extends CCComp { if(!this.top_node.active) return let mp=this.model.mp; let mp_max=this.model.Attrs[Attrs.MP_MAX]; - console.log("mp_show",mp,mp_max) + this.debugLog("mp_show",mp,mp_max) this.top_node.getChildByName("mp").getComponent(ProgressBar).progress = mp / mp_max; this.scheduleOnce(() => { this.top_node.getChildByName("mp").getChildByName("mpb").getComponent(ProgressBar).progress = mp / mp_max; @@ -331,7 +344,7 @@ export class HeroViewComp extends CCComp { realDead(){ // 🔥 修复:添加model安全检查,防止实体销毁过程中的空指针异常 if (!this.model) { - console.warn("[HeroViewComp] realDead called but model is null, skipping"); + this.debugWarn("[HeroViewComp] realDead called but model is null, skipping"); return; } if(this.model.fac === FacSet.HERO){ @@ -472,3 +485,8 @@ export class HeroViewComp extends CCComp { } } + + + + +