From 974a6d26b2861c480bd71b4b5261dfca6f721d78 Mon Sep 17 00:00:00 2001 From: walkpan Date: Tue, 6 Jan 2026 14:28:48 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E4=BC=A4=E5=AE=B3=E7=B3=BB=E7=BB=9F):?= =?UTF-8?q?=20=E5=B0=86caster=E4=BB=8EHeroViewComp=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E4=BD=BF=E7=94=A8casterEid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 统一伤害系统中施法者的标识方式,从直接使用HeroViewComp改为使用实体ID(casterEid) 修复反伤逻辑中可能存在的空指针问题 --- assets/script/game/hero/DamageQueueComp.ts | 13 ++++++------- assets/script/game/hero/HeroAtkSystem.ts | 15 ++++++++------- assets/script/game/skill/SkillView.ts | 2 +- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/assets/script/game/hero/DamageQueueComp.ts b/assets/script/game/hero/DamageQueueComp.ts index 95d4289d..213fd9f8 100644 --- a/assets/script/game/hero/DamageQueueComp.ts +++ b/assets/script/game/hero/DamageQueueComp.ts @@ -10,8 +10,8 @@ export interface DamageEvent { /** 伤害属性数据 */ Attrs: any; - /** 施法者 */ - caster: HeroViewComp; + /** 施法者ID */ + casterEid: number; /** 技能UUID */ s_uuid: number; @@ -58,14 +58,13 @@ export class DamageQueueComp extends ecs.Comp { /** * 添加伤害事件到队列 */ - addDamageEvent(attrs: any, caster: HeroViewComp, s_uuid: number,ext_dmg:number=0,dmg_ratio:number=1): void { + addDamageEvent(attrs: any, casterEid: number, s_uuid: number,ext_dmg:number=0,dmg_ratio:number=1): void { const timestamp = Date.now(); - const casterEid = caster?.ent?.eid ?? -1; const eventId = `${casterEid}_${s_uuid}_${timestamp}_${Math.random()}`; const damageEvent: DamageEvent = { Attrs: attrs ? { ...attrs } : null, // 深拷贝属性数据 - caster: caster, + casterEid: casterEid, s_uuid: s_uuid, timestamp: timestamp, eventId: eventId, @@ -161,7 +160,7 @@ export class DamageQueueHelper { * 为实体添加伤害事件 * 如果实体没有伤害队列组件,会自动创建 */ - static addDamageToEntity(entity: ecs.Entity, attrs: any, caster: HeroViewComp, s_uuid: number,ext_dmg:number=0,dmg_ratio:number=1): void { + static addDamageToEntity(entity: ecs.Entity, attrs: any, casterEid: number, s_uuid: number,ext_dmg:number=0,dmg_ratio:number=1): void { let damageQueue = entity.get(DamageQueueComp); // 如果实体没有伤害队列组件,创建一个 @@ -170,7 +169,7 @@ export class DamageQueueHelper { } // 添加伤害事件到队列 - damageQueue.addDamageEvent(attrs, caster, s_uuid,ext_dmg,dmg_ratio); + damageQueue.addDamageEvent(attrs, casterEid, s_uuid,ext_dmg,dmg_ratio); } /** diff --git a/assets/script/game/hero/HeroAtkSystem.ts b/assets/script/game/hero/HeroAtkSystem.ts index 96c97771..540b688e 100644 --- a/assets/script/game/hero/HeroAtkSystem.ts +++ b/assets/script/game/hero/HeroAtkSystem.ts @@ -130,8 +130,9 @@ export class HeroAtkSystem extends ecs.ComblockSystem implements ecs.ISystemUpd } if (!TAttrsComp || TAttrsComp.is_dead || TAttrsComp.is_reviving) return reDate; - const caster = damageEvent.caster; - const CAttrsComp = caster?.ent?.get(HeroAttrsComp); + const casterEid = damageEvent.casterEid; + const caster = ecs.getEntityByEid(casterEid); + const CAttrsComp = caster?.get(HeroAttrsComp); // 获取技能配置 const skillConf = SkillSet[damageEvent.s_uuid]; @@ -186,12 +187,11 @@ export class HeroAtkSystem extends ecs.ComblockSystem implements ecs.ISystemUpd smc.updateHeroInfo(TAttrsComp); // 更新英雄数据到 VM if (this.debugMode) { - const casterName = damageEvent.caster?.ent?.get(HeroAttrsComp)?.hero_name || "未知"; - const casterUuid = damageEvent.caster?.ent?.get(HeroAttrsComp)?.hero_uuid || 0; - console.log(`[HeroAtkSystem] 英雄${TAttrsComp.hero_name} (uuid: ${TAttrsComp.hero_uuid}) 受到 ${casterName}(uuid: ${casterUuid})的 伤害 ${damage},${isCrit?"暴击":"普通"}攻击,技能ID ${damageEvent.s_uuid}`); + const casterName = CAttrsComp?.hero_name || "未知"; + console.log(`[HeroAtkSystem] 英雄${TAttrsComp.hero_name} (uuid: ${TAttrsComp.hero_uuid}) 受到 ${casterName}(eid: ${casterEid})的 伤害 ${damage},${isCrit?"暴击":"普通"}攻击,技能ID ${damageEvent.s_uuid}`); } //反伤判定 并应用到施法者 - this.check_thorns(TAttrsComp, caster?.ent,damage); + this.check_thorns(TAttrsComp, caster, damage); // 击退判定 // 使用施法者的击退概率属性(damageEvent.Attrs 快照) - 被攻击者的控制抗性 // 击退成功后需要清理施法者的相关天赋buff @@ -249,7 +249,8 @@ export class HeroAtkSystem extends ecs.ComblockSystem implements ecs.ISystemUpd } check_thorns(TAttrsComp:HeroAttrsComp, caster: ecs.Entity, damage:number) { // 检查目标是否有反伤属性,这里受伤的时时施法者 - if (!caster||damage<=0) return; + // 修复:如果施法者已销毁(caster为空),则不执行反伤逻辑 + if (!caster || damage<=0) return; let thornsDamage=0; thornsDamage=TAttrsComp.Attrs[Attrs.THORNS]||0+TAttrsComp.useCountValTal(Attrs.THORNS); let CAttrs=caster.get(HeroAttrsComp); diff --git a/assets/script/game/skill/SkillView.ts b/assets/script/game/skill/SkillView.ts index 6c187342..37093cb1 100644 --- a/assets/script/game/skill/SkillView.ts +++ b/assets/script/game/skill/SkillView.ts @@ -163,7 +163,7 @@ export class SkillView extends CCComp { DamageQueueHelper.addDamageToEntity( target.ent, this.sData.Attrs, - this.sData.caster, + this.sData.casterEid, this.sData.s_uuid, this.sData.ext_dmg, this.sData.dmg_ratio,