From 859ab3bc2a4fa8033e81fa8eef9c0d37012cb9aa Mon Sep 17 00:00:00 2001 From: panw Date: Tue, 3 Feb 2026 10:45:17 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=BC=95=E5=85=A5=E9=9B=86=E4=B8=AD?= =?UTF-8?q?=E5=BC=8F=E6=97=A5=E5=BF=97=E5=B7=A5=E5=85=B7=E7=B1=BB=E5=B9=B6?= =?UTF-8?q?=E9=87=8D=E6=9E=84=E8=B0=83=E8=AF=95=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 Logger 类提供统一的日志和警告输出,支持全局和模块级开关 - 重构 SkillView、HeroViewComp 和 HeroAtkSystem 中的调试日志方法,改用 Logger 类 - 在 HeroViewComp 中添加调试模式属性便于编辑器配置 - 统一日志格式为 [标签] + 内容,提高日志可读性和维护性 --- assets/script/game/common/Logger.ts | 28 ++++++++++++++ assets/script/game/common/Logger.ts.meta | 9 +++++ assets/script/game/hero/HeroAtkSystem.ts | 48 ++++++++++++++---------- assets/script/game/hero/HeroViewComp.ts | 12 +++--- assets/script/game/skill/SkillView.ts | 9 ++--- 5 files changed, 73 insertions(+), 33 deletions(-) create mode 100644 assets/script/game/common/Logger.ts create mode 100644 assets/script/game/common/Logger.ts.meta diff --git a/assets/script/game/common/Logger.ts b/assets/script/game/common/Logger.ts new file mode 100644 index 00000000..31479be0 --- /dev/null +++ b/assets/script/game/common/Logger.ts @@ -0,0 +1,28 @@ +export class Logger { + /** 总开关:控制所有日志输出 */ + public static GLOBAL_ENABLED: boolean = true; + + /** + * 统一日志输出 + * @param enable 单独开关(模块级开关) + * @param tag 标签(通常是类名或模块名) + * @param args 日志内容 + */ + public static log(enable: boolean, tag: string, ...args: any[]) { + if (this.GLOBAL_ENABLED && enable) { + console.log(`[${tag}]`, ...args); + } + } + + /** + * 统一警告输出 + * @param enable 单独开关(模块级开关) + * @param tag 标签(通常是类名或模块名) + * @param args 警告内容 + */ + public static warn(enable: boolean, tag: string, ...args: any[]) { + if (this.GLOBAL_ENABLED && enable) { + console.warn(`[${tag}]`, ...args); + } + } +} diff --git a/assets/script/game/common/Logger.ts.meta b/assets/script/game/common/Logger.ts.meta new file mode 100644 index 00000000..577435af --- /dev/null +++ b/assets/script/game/common/Logger.ts.meta @@ -0,0 +1,9 @@ +{ + "ver": "4.0.24", + "importer": "typescript", + "imported": true, + "uuid": "0d4013a0-024f-4b53-8c1c-d366a381b64d", + "files": [], + "subMetas": {}, + "userData": {} +} diff --git a/assets/script/game/hero/HeroAtkSystem.ts b/assets/script/game/hero/HeroAtkSystem.ts index e5ed81da..ad2652fd 100644 --- a/assets/script/game/hero/HeroAtkSystem.ts +++ b/assets/script/game/hero/HeroAtkSystem.ts @@ -12,6 +12,8 @@ import { oops } from "db://oops-framework/core/Oops"; import { GameEvent } from "../common/config/GameEvent"; +import { Logger } from "../common/Logger"; + /** 最终伤害数据接口 * 用于封装一次攻击计算的所有结果数据 * @property damage - 最终造成的伤害值(已考虑所有加成和减免) @@ -40,6 +42,14 @@ interface FinalData { export class HeroAtkSystem extends ecs.ComblockSystem implements ecs.ISystemUpdate { private debugMode: boolean = false; // 是否启用调试模式 + + private debugLog(...args: any[]): void { + Logger.log(this.debugMode, 'HeroAtkSystem', ...args); + } + + private debugWarn(...args: any[]): void { + Logger.warn(this.debugMode, 'HeroAtkSystem', ...args); + } /** * 过滤器:处理拥有伤害队列的实体 @@ -75,9 +85,7 @@ export class HeroAtkSystem extends ecs.ComblockSystem implements ecs.ISystemUpd damageQueue.processedCount++; // 如果目标已死亡,停止处理后续伤害 if (TAttrsComp.is_dead) { - if (this.debugMode) { - console.log(`[HeroAtkSystem] ${TAttrsComp.hero_name} 已死亡,停止处理剩余伤害`); - } + this.debugLog(`[HeroAtkSystem] ${TAttrsComp.hero_name} 已死亡,停止处理剩余伤害`); damageQueue.clear(); // 清空剩余伤害 break; } @@ -87,8 +95,8 @@ export class HeroAtkSystem extends ecs.ComblockSystem implements ecs.ISystemUpd if (damageQueue.isEmpty()) { e.remove(DamageQueueComp); - if (this.debugMode && processedCount > 0) { - console.log(`[HeroAtkSystem] ${TAttrsComp.hero_name} 伤害队列处理完成,共处理 ${processedCount} 个伤害事件`); + if (processedCount > 0) { + this.debugLog(`[HeroAtkSystem] ${TAttrsComp.hero_name} 伤害队列处理完成,共处理 ${processedCount} 个伤害事件`); } } } @@ -156,7 +164,7 @@ export class HeroAtkSystem extends ecs.ComblockSystem implements ecs.ISystemUpd const isCrit = this.checkChance(damageEvent.Attrs[Attrs.CRITICAL]-TAttrsComp.Attrs[Attrs.CRITICAL_RES]); // 计算基础伤害 let damage = this.dmgCount(damageEvent,TAttrsComp); - if (this.debugMode) console.log("[HeroAtkSystem] dmgCount",damage) + this.debugLog("[HeroAtkSystem] dmgCount",damage) if (isCrit) { // 暴击伤害计算 // 使用施法者的暴击伤害加成属性(damageEvent.Attrs 快照) @@ -167,11 +175,11 @@ export class HeroAtkSystem extends ecs.ComblockSystem implements ecs.ISystemUpd CAttrsComp?.useValueTalByAttr(Attrs.CRITICAL); // 清除施法者的暴击buff } - if (this.debugMode) console.log("[HeroAtkSystem] after crit",damage) + this.debugLog("[HeroAtkSystem] after crit",damage) // 护盾吸收 const shieldResult = this.absorbShield(TAttrsComp, damage); damage = shieldResult.remainingDamage; - if (this.debugMode) console.log("[HeroAtkSystem] after shield",damage) + this.debugLog("[HeroAtkSystem] after shield",damage) // 显示护盾吸收飘字 if (shieldResult.absorbedDamage > 0 && targetView) { targetView.shield_tip(shieldResult.absorbedDamage); @@ -187,10 +195,10 @@ export class HeroAtkSystem extends ecs.ComblockSystem implements ecs.ISystemUpd // } smc.updateHeroInfo(TAttrsComp); // 更新英雄数据到 VM - if (this.debugMode) { - const casterName = CAttrsComp?.hero_name || "未知"; - console.log(`[HeroAtkSystem] 英雄${TAttrsComp.hero_name} (uuid: ${TAttrsComp.hero_uuid}) 受到 ${casterName}(eid: ${casterEid})的 伤害 ${damage},${isCrit?"暴击":"普通"}攻击,技能ID ${damageEvent.s_uuid}`); - } + + const casterName = CAttrsComp?.hero_name || "未知"; + this.debugLog(`[HeroAtkSystem] 英雄${TAttrsComp.hero_name} (uuid: ${TAttrsComp.hero_uuid}) 受到 ${casterName}(eid: ${casterEid})的 伤害 ${damage},${isCrit?"暴击":"普通"}攻击,技能ID ${damageEvent.s_uuid}`); + //反伤判定 并应用到施法者 this.check_thorns(TAttrsComp, caster, damage); // 击退判定 @@ -221,7 +229,7 @@ export class HeroAtkSystem extends ecs.ComblockSystem implements ecs.ISystemUpd targetView.scheduleRevive(1.0); } - console.log(`[HeroAtkSystem] Hero waiting to revive! Lives left: ${TAttrsComp.Attrs[Attrs.REVIVE_COUNT]}`); + this.debugLog(`[HeroAtkSystem] Hero waiting to revive! Lives left: ${TAttrsComp.Attrs[Attrs.REVIVE_COUNT]}`); return reDate; } @@ -232,7 +240,7 @@ export class HeroAtkSystem extends ecs.ComblockSystem implements ecs.ISystemUpd if (TAttrsComp.is_master&&TAttrsComp.Attrs[Attrs.REVIVE_COUNT] <= 0) { smc.mission.stop_mon_action = true; oops.message.dispatchEvent(GameEvent.HeroDead, { hero_uuid: TAttrsComp.hero_uuid}); - console.log("[HeroAtkSystem] Hero died, stopping monster action (spawn/move)"+TAttrsComp.Attrs[Attrs.REVIVE_COUNT]); + this.debugLog("[HeroAtkSystem] Hero died, stopping monster action (spawn/move)"+TAttrsComp.Attrs[Attrs.REVIVE_COUNT]); } this.doDead(target); @@ -242,7 +250,7 @@ export class HeroAtkSystem extends ecs.ComblockSystem implements ecs.ISystemUpd } } - if (this.debugMode) console.log(`[HeroAtkSystem] ${TAttrsComp.hero_name} 受到 ${damage} 点伤害 (暴击: ${isCrit})`); + this.debugLog(`[HeroAtkSystem] ${TAttrsComp.hero_name} 受到 ${damage} 点伤害 (暴击: ${isCrit})`); reDate.damage=damage; @@ -322,11 +330,11 @@ export class HeroAtkSystem extends ecs.ComblockSystem implements ecs.ISystemUpd const TAttrs=TAttrsComp.Attrs; let sConf = SkillSet[damageEvent.s_uuid]; if (!sConf) return 0; - if (this.debugMode) console.log(`[HeroAtkSystem] 伤害处理对象`,CAttrs,TAttrs); + this.debugLog(`[HeroAtkSystem] 伤害处理对象`,CAttrs,TAttrs); // 2. 计算原始物理伤害和魔法伤害 // 物理伤害基础值 = 技能物理倍率 * (施法者物理攻击力 + 额外伤害) / 100 * 额外伤害比例 let apBase = (sConf.ap||0)*(CAttrs[Attrs.AP]+damageEvent.ext_dmg)/100*damageEvent.dmg_ratio; - if (this.debugMode) console.log(`[HeroAtkSystem] 物理伤害基础值: ${apBase}, 技能ap=${sConf.ap},施法者物理攻击力: ${CAttrs[Attrs.AP]},} + this.debugLog(`[HeroAtkSystem] 物理伤害基础值: ${apBase}, 技能ap=${sConf.ap},施法者物理攻击力: ${CAttrs[Attrs.AP]},} 额外伤害:${damageEvent.ext_dmg}, 额外伤害比例:${damageEvent.dmg_ratio}`); // 易伤 let DMG_INVUL = TAttrs[Attrs.DMG_INVUL]||0 @@ -459,7 +467,7 @@ export class HeroAtkSystem extends ecs.ComblockSystem implements ecs.ISystemUpd private absorbShield(TAttrsComp: HeroAttrsComp, damage: number): {remainingDamage: number, absorbedDamage: number} { if (TAttrsComp.shield <= 0) { - console.log("[HeroAtkSystem] 护盾值小于等于0,无法吸收伤害"); + this.debugLog("[HeroAtkSystem] 护盾值小于等于0,无法吸收伤害"); return {remainingDamage: damage, absorbedDamage: 0}; }; @@ -470,7 +478,7 @@ export class HeroAtkSystem extends ecs.ComblockSystem implements ecs.ISystemUpd TAttrsComp.Attrs[Attrs.SHIELD_MAX] = 0; } TAttrsComp.dirty_shield = true; - console.log(`[HeroAtkSystem] 护盾值完全吸收伤害 ${damage}`); + this.debugLog(`[HeroAtkSystem] 护盾值完全吸收伤害 ${damage}`); return {remainingDamage: 0, absorbedDamage: damage}; } else { const absorbedDamage = TAttrsComp.shield; @@ -478,7 +486,7 @@ export class HeroAtkSystem extends ecs.ComblockSystem implements ecs.ISystemUpd TAttrsComp.shield = 0; TAttrsComp.Attrs[Attrs.SHIELD_MAX] = 0; TAttrsComp.dirty_shield = true; - console.log(`[HeroAtkSystem] 护盾值部分吸收伤害 ${absorbedDamage}`); + this.debugLog(`[HeroAtkSystem] 护盾值部分吸收伤害 ${absorbedDamage}`); return {remainingDamage, absorbedDamage}; } } diff --git a/assets/script/game/hero/HeroViewComp.ts b/assets/script/game/hero/HeroViewComp.ts index cc72dc61..4463ff8d 100644 --- a/assets/script/game/hero/HeroViewComp.ts +++ b/assets/script/game/hero/HeroViewComp.ts @@ -1,6 +1,7 @@ import { Vec3, _decorator , v3,Collider2D,Contact2DType,Label ,Node,Prefab,instantiate,ProgressBar, Component, Material, Sprite, math, clamp, Game, tween, Tween, Color, BoxCollider2D, UITransform} from "cc"; import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS"; import { CCComp } from "../../../../extensions/oops-plugin-framework/assets/module/common/CCComp"; +import { Logger } from "../common/Logger"; import { HeroSpine } from "./HeroSpine"; import { BoxSet, FacSet } from "../common/config/GameSet"; import { smc } from "../common/SingletonModuleComp"; @@ -26,19 +27,16 @@ export interface BuffInfo { @ccclass('HeroViewComp') // 定义Cocos Creator 组件 @ecs.register('HeroView', false) // 定义ECS 组件 export class HeroViewComp extends CCComp { - private debugMode: boolean = false; // 是否启用调试模式 + @property({ tooltip: "是否启用调试日志" }) + private debugMode: boolean = true; // 是否启用调试模式 // 添加条件日志方法 private debugLog(...args: any[]): void { - if (this.debugMode) { - console.log(...args); - } + Logger.log(this.debugMode, 'HeroViewComp', ...args); } private debugWarn(...args: any[]): void { - if (this.debugMode) { - console.warn(...args); - } + Logger.warn(this.debugMode, 'HeroViewComp', ...args); } // ==================== View 层属性(表现相关)==================== as: HeroSpine = null! diff --git a/assets/script/game/skill/SkillView.ts b/assets/script/game/skill/SkillView.ts index 4eb0b825..597fa8ad 100644 --- a/assets/script/game/skill/SkillView.ts +++ b/assets/script/game/skill/SkillView.ts @@ -7,6 +7,7 @@ import { SDataCom } from "./SDataCom"; import { Attrs } from "../common/config/HeroAttrs"; import { HeroAttrsComp } from "../hero/HeroAttrsComp"; import { DamageQueueHelper } from "../hero/DamageQueueComp"; +import { Logger } from "../common/Logger"; const { ccclass, property } = _decorator; @@ -24,15 +25,11 @@ export class SkillView extends CCComp { private debugMode: boolean = false; private debugLog(...args: any[]): void { - if (this.debugMode) { - console.log(...args); - } + Logger.log(this.debugMode, 'SkillView', ...args); } private debugWarn(...args: any[]): void { - if (this.debugMode) { - console.warn(...args); - } + Logger.warn(this.debugMode, 'SkillView', ...args); } anim:Animation=null;