fix(战斗系统): 修复伤害数值出错问题

- 修复在DamageEvent接口和DamageQueueComp缺少ext_dmg和dmg_ratio字段问题
- 修复HeroAtkSystem的伤害计算逻辑
- 优化HeroViewComp的hp_show和mp_show方法,直接使用model数据
- 默认显示血条并增加调试日志输出
- 移除冗余的debug日志,优化伤害计算流程
This commit is contained in:
2025-11-28 09:59:01 +08:00
parent 40c430546c
commit 7a7a6fa02c
4 changed files with 55 additions and 40 deletions

View File

@@ -21,6 +21,9 @@ export interface DamageEvent {
/** 伤害事件ID用于去重和调试 */
eventId: string;
ext_dmg:number //额外伤害
dmg_ratio:number//伤害比例
}
/**
@@ -55,7 +58,7 @@ export class DamageQueueComp extends ecs.Comp {
/**
* 添加伤害事件到队列
*/
addDamageEvent(attrs: any, caster: HeroViewComp, s_uuid: number): void {
addDamageEvent(attrs: any, caster: HeroViewComp, s_uuid: number,ext_dmg:number=0,dmg_ratio:number=1): void {
const timestamp = Date.now();
const eventId = `${caster.ent.eid}_${s_uuid}_${timestamp}_${Math.random()}`;
@@ -64,7 +67,9 @@ export class DamageQueueComp extends ecs.Comp {
caster: caster,
s_uuid: s_uuid,
timestamp: timestamp,
eventId: eventId
eventId: eventId,
ext_dmg:ext_dmg,
dmg_ratio:dmg_ratio,
};
this.damageEvents.push(damageEvent);
@@ -155,7 +160,7 @@ export class DamageQueueHelper {
* 为实体添加伤害事件
* 如果实体没有伤害队列组件,会自动创建
*/
static addDamageToEntity(entity: ecs.Entity, attrs: any, caster: HeroViewComp, s_uuid: number): void {
static addDamageToEntity(entity: ecs.Entity, attrs: any, caster: HeroViewComp, s_uuid: number,ext_dmg:number=0,dmg_ratio:number=1): void {
let damageQueue = entity.get(DamageQueueComp);
// 如果实体没有伤害队列组件,创建一个
@@ -164,7 +169,7 @@ export class DamageQueueHelper {
}
// 添加伤害事件到队列
damageQueue.addDamageEvent(attrs, caster, s_uuid);
damageQueue.addDamageEvent(attrs, caster, s_uuid,ext_dmg,dmg_ratio);
}
/**

View File

@@ -66,16 +66,9 @@ export class HeroAtkSystem extends ecs.ComblockSystem implements ecs.ISystemUpd
if (!damageEvent) break;
// 处理单个伤害事件
const FDData = this.doAttack(e, damageEvent);
this.doAttack(e, damageEvent);
processedCount++;
damageQueue.processedCount++;
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})的 伤害 ${FDData.damage},${FDData.isCrit?"暴击":"普通"}攻击技能ID ${damageEvent.s_uuid}`);
}
// 如果目标已死亡,停止处理后续伤害
if (TAttrsComp.is_dead) {
if (this.debugMode) {
@@ -158,6 +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 (isCrit) {
// 暴击伤害计算
// 使用施法者的暴击伤害加成属性damageEvent.Attrs 快照)
@@ -168,12 +162,18 @@ export class HeroAtkSystem extends ecs.ComblockSystem implements ecs.ISystemUpd
CAttrsComp?.useValueTalByAttr(Attrs.CRITICAL); // 清除施法者的暴击buff
}
console.log("[HeroAtkSystem] after crit",damage)
// 护盾吸收
damage =Math.floor(this.absorbShield(TAttrsComp, damage))
console.log("[HeroAtkSystem] after shield",damage)
if (damage <= 0) return reDate;
// 应用伤害到数据层
TAttrsComp.hp -= damage;
TAttrsComp.atked_count++;
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}`);
}
//反伤判定 并应用到施法者
this.check_thorns(TAttrsComp, caster?.ent,damage);
// 击退判定
@@ -198,9 +198,8 @@ export class HeroAtkSystem extends ecs.ComblockSystem implements ecs.ISystemUpd
}
}
if (this.debugMode) {
console.log(`[HeroAtkSystem] ${TAttrsComp.hero_name} 受到 ${damage} 点伤害 (暴击: ${isCrit})`);
}
if (this.debugMode) console.log(`[HeroAtkSystem] ${TAttrsComp.hero_name} 受到 ${damage} 点伤害 (暴击: ${isCrit})`);
reDate.damage=damage;
return reDate;
@@ -256,19 +255,21 @@ export class HeroAtkSystem extends ecs.ComblockSystem implements ecs.ISystemUpd
* - 所有除法和乘法计算后都进行取整操作,确保游戏中的伤害值为整数
* - 元素伤害只应用于魔法伤害部分
*/
private dmgCount(damageEvent:any,TAttrsComp:HeroAttrsComp){
private dmgCount(damageEvent:DamageEvent,TAttrsComp:HeroAttrsComp){
// 1. 获取技能配置 - 如果技能不存在直接返回0伤害
const CAttrs=damageEvent.Attrs;
const TAttrs=TAttrsComp.Attrs;
let sConf = SkillSet[damageEvent.s_uuid];
if (!sConf) return 0;
if (this.debugMode) console.log(`[HeroAtkSystem] 伤害处理对象`,CAttrs,TAttrs);
// 2. 计算原始物理伤害和魔法伤害
// 物理伤害基础值 = 技能物理倍率 * (施法者物理攻击力 + 额外物理伤害) / 100 * 伤害比例
// 物理伤害基础值 = 技能物理倍率 * (施法者物理攻击力 + 额外伤害) / 100 * 额外伤害比例
let apBase = (sConf.ap||0)*(CAttrs[Attrs.AP]+damageEvent.ext_dmg)/100*damageEvent.dmg_ratio;
// 魔法伤害基础值 = 技能魔法倍率 * (施法者魔法攻击力 + 额外魔法伤害) / 100 * 伤害比例
// 魔法伤害基础值 = 技能魔法倍率 * (施法者魔法攻击力 + 额外伤害) / 100 * 额外伤害比例
let mapBase = (sConf.map||0)*(CAttrs[Attrs.MAP]+damageEvent.ext_dmg)/100*damageEvent.dmg_ratio;
if (this.debugMode) console.log(`[HeroAtkSystem] 物理伤害基础值: ${apBase}, 技能ap=${sConf.ap},施法者物理攻击力: ${CAttrs[Attrs.AP]}, 魔法伤害基础值: ${mapBase}技能map=${sConf.map}
额外伤害:${damageEvent.ext_dmg}, 额外伤害比例:${damageEvent.dmg_ratio}`);
// 3. 获取目标防御属性
const def = (TAttrs[Attrs.DEF]||0); // 目标物理防御
const mdef = (TAttrs[Attrs.MDEF]||0); // 目标魔法防御
@@ -281,14 +282,15 @@ export class HeroAtkSystem extends ecs.ComblockSystem implements ecs.ISystemUpd
let apAfter = Math.floor(apBase * (1 - apRed)); // 物理伤害 - 防御减免
let mapAfter = Math.floor(mapBase * (1 - mapRed)); // 魔法伤害 - 防御减免
if (this.debugMode) console.log(`[HeroAtkSystem] 物理伤害基础值: ${apBase}, 物理伤害 - 防御减免: ${apAfter} 魔法伤害基础值: ${mapBase}, 魔法伤害 - 防御减免: ${mapAfter}`);
// 6. 应用物理/魔法攻击力和抗性修正
// 物理伤害修正:基础伤害 * (1 + 物理攻击力加成%) * (1 - 目标物理抗性%)
apAfter = this.applyPR(apAfter, CAttrs[Attrs.PHYS_POWER]||0, TAttrs[Attrs.PHYS_RES]||0);
// 魔法伤害修正:基础伤害 * (1 + 魔法攻击力加成%) * (1 - 目标魔法抗性%)
mapAfter = this.applyPR(mapAfter, CAttrs[Attrs.MAGIC_POWER]||0, TAttrs[Attrs.MAGIC_RES]||0);
if (this.debugMode) console.log(`[HeroAtkSystem] 物理伤害修正后: ${apAfter} 魔法伤害修正后: ${mapAfter} `);
// 7. 根据技能元素类型,应用元素属性加成和抗性修正
switch (sConf.DType) {
case DType.ICE:
@@ -304,6 +306,7 @@ export class HeroAtkSystem extends ecs.ComblockSystem implements ecs.ISystemUpd
mapAfter = this.applyPR(mapAfter, CAttrs[Attrs.WIND_POWER]||0, TAttrs[Attrs.WIND_RES]||0);
break;
}
if (this.debugMode) console.log(`[HeroAtkSystem] 物理伤害修正后: ${apAfter} 元素伤害修正后: ${mapAfter}`);
// 8. 计算最终总伤害(物理伤害 + 魔法伤害)
let total = apAfter + mapAfter;
@@ -315,7 +318,7 @@ export class HeroAtkSystem extends ecs.ComblockSystem implements ecs.ISystemUpd
total = Math.max(0,total);
//11. 易伤减免 免伤属性免伤+天赋免伤
total = Math.floor(total * (1 + ((DMG_INVUL-DMG_RED)/100)));
if (this.debugMode) console.log(`[HeroAtkSystem] 易伤减免后: ${total}`);
return Math.max(0,total);
}
@@ -461,7 +464,7 @@ export class HeroAtkSystem extends ecs.ComblockSystem implements ecs.ISystemUpd
private onAttacked(entity: ecs.Entity): void {
const TAttrsComp = entity.get(HeroAttrsComp);
if (!TAttrsComp || TAttrsComp.is_dead) return;
TAttrsComp.atked_count++;
// 这里可以添加被攻击时的特殊处理逻辑
if (TAttrsComp.fac === FacSet.MON) return;

View File

@@ -26,6 +26,7 @@ export interface BuffInfo {
@ccclass('HeroViewComp') // 定义Cocos Creator 组件
@ecs.register('HeroView', false) // 定义ECS 组件
export class HeroViewComp extends CCComp {
private debugMode: boolean = false; // 是否启用调试模式
// ==================== View 层属性(表现相关)====================
as: HeroSpine = null!
status:String = "idle"
@@ -89,7 +90,8 @@ export class HeroViewComp extends CCComp {
this.top_node.getChildByName("hp").active = true;
this.top_node.getChildByName("mp").active = true;
// 初始隐藏血条(被攻击后才显示)
this.top_node.active = false;
this.top_node.active = true;
}
/** 初始化 UI 节点引用 */
@@ -136,8 +138,8 @@ export class HeroViewComp extends CCComp {
// ✅ 更新 UI 显示(数据由 HeroAttrSystem 更新)
// 移除了每帧调用的 hp_show改为仅在需要时调用
this.hp_show(this.model.hp, this.model.Attrs[Attrs.HP_MAX]);
this.mp_show(this.model.mp, this.model.Attrs[Attrs.MP_MAX]);
this.hp_show();
this.mp_show();
this.show_shield(this.model.shield, this.model.Attrs[Attrs.SHIELD_MAX]);
}
@@ -155,20 +157,23 @@ export class HeroViewComp extends CCComp {
}
/** 显示血量 */
private hp_show(hp: number, hp_max: number) {
private hp_show() {
// 不再基于血量是否满来决定显示状态,只更新进度条
if(!this.top_node.active) return;
let hp_progress = hp / hp_max;
this.top_node.getChildByName("hp").getComponent(ProgressBar).progress = hp_progress;
let hp=this.model.hp;
let hp_max=this.model.Attrs[Attrs.HP_MAX];
console.log("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_progress;
this.top_node.getChildByName("hp").getChildByName("hpb").getComponent(ProgressBar).progress = hp / hp_max;;
}, 0.15);
}
/** 显示魔法值 */
private mp_show(mp: number, mp_max: number) {
private mp_show() {
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.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;
@@ -288,13 +293,13 @@ export class HeroViewComp extends CCComp {
this.heathed();
this.hp_tip(TooltipTypes.health, hp.toFixed(0));
this.top_node.active=true
this.hp_show(this.model.hp, this.model.Attrs[Attrs.HP_MAX]);
this.hp_show();
}
mp_add(mp: number = 0) {
// 生命值更新由 Model 层处理,这里只负责视图表现
this.hp_tip(TooltipTypes.addmp, mp.toFixed(0));
this.top_node.active=true
this.mp_show(this.model.mp, this.model.Attrs[Attrs.MP_MAX]);
this.mp_show();
}
alive(){
@@ -439,7 +444,7 @@ export class HeroViewComp extends CCComp {
private showDamageImmediate(damage: number, isCrit: boolean, anm:string="atked") {
if (!this.model) return;
this.hp_show(this.model.hp, this.model.Attrs[Attrs.HP_MAX]);
this.hp_show();
this.in_atked(anm, this.model.fac==FacSet.HERO?1:-1);
if (isCrit) {
this.hp_tip(TooltipTypes.crit, damage.toFixed(0));

View File

@@ -149,7 +149,9 @@ export class SkillView extends CCComp {
target.ent,
this.sData.Attrs,
this.sData.caster,
this.sData.s_uuid
this.sData.s_uuid,
this.sData.ext_dmg,
this.sData.dmg_ratio,
);
// 更新技能命中次数
this.sData.hit_count++