2 Commits

Author SHA1 Message Date
e36abeb380 refactor(skill): 统一并更正deV字段命名
- 将DbuffConf接口中的字段名称dev修正为deV
- 修复HeroViewComp中所有对dev字段的引用为deV
- 调整BattleMoveSystem中速度计算逻辑,使用正确的buff叠加值
- 统一代码注释中减少值的字段名称为deV
- 保持功能逻辑不变,提升代码一致性和可读性
2025-10-17 10:31:58 +08:00
6386d6bd80 refactor(hero): 优化Buff和Debuff的数据结构及处理逻辑
- 新增移动速度相关属性:DBuff.SPEED和Attrs.SPEED
- 修改BattleMoveSystem中移动速度计算,支持速度百分比加成
- Buff和Debuff由数组改为基于属性键的对象存储,方便访问和合并处理
- 持久型Buff/Debuff累加数值或百分比,临时型则累加并刷新剩余时间
- 重新计算属性时遍历键值对,跳过状态类debuff(attrField=-1)
- 更新临时型Buff/Debuff剩余时间,过期时从对象中删除
- 代码结构更清晰,访问及更新性能提升
2025-10-17 10:27:21 +08:00
3 changed files with 143 additions and 91 deletions

View File

@@ -111,6 +111,7 @@ export enum DBuff {
DODGE = 12, //-闪避 - 对应Attrs.DODGE (闪避), BType.RATIO
DBUFFUP=13, //edbuff效果提升
BUFF_DOWN = 14,// buff效果减弱
SPEED = 15, //移动速度下降 - 对应Attrs.MOVE_SPEED (移动速度), BType.RATIO
}
@@ -142,6 +143,7 @@ export enum Attrs {
BUFF_UP = 24, //buff效果提升
DBUFF_UP=25, //debuff效果提升
DIS=26, //攻击距离
SPEED = 27, //移动速度加成,默认都是百分比
}
export const getAttrs=()=>{
@@ -172,9 +174,7 @@ export const getAttrFieldFromDebuff = (debuffType: DBuff): number => {
// 状态类 debuff只需缓存不影响属性
const stateDebuffSet = new Set<DBuff>([
DBuff.STUN,
DBuff.SLOW,
DBuff.FROST,
DBuff.BURN,
]);
// 检查是否是状态类 debuff
@@ -200,6 +200,8 @@ export const getAttrFieldFromDebuff = (debuffType: DBuff): number => {
[DBuff.DODGE]: Attrs.DODGE, // -闪避 -> 闪避
[DBuff.DBUFFUP]: Attrs.DBUFF_UP, // debuff提升 -> debuff提升
[DBuff.BUFF_DOWN]: Attrs.BUFF_UP, // buff减弱 -> buff效果
[DBuff.SPEED]: Attrs.SPEED, // 移动速度下降 -> 移动速度
};
const attrField = debuffAttrMap[debuffType];
@@ -287,9 +289,9 @@ export const HeroSkillList = [6001,6001,6001,6001,6001,6001]
export interface DbuffConf {
debuff: DBuff; // debuff类型
BType:BType //buff是数值型还是百分比型
dev: number; // 效果值 (原deV)
deV: number; // 效果值
deC: number; // 持续时间
deR: number; // 触发概率 (原deR)
deR: number; // 触发概率
}
export interface BuffConf {
buff:Attrs;

View File

@@ -91,8 +91,10 @@ export class BattleMoveSystem extends ecs.ComblockSystem implements ecs.ISystemU
// 如果不在目标位置,移动到目标位置
if (Math.abs(currentX - finalTargetX) > 1) {
// 确定移动方向
let B_SPEED=view.R_BUFFS[Attrs.SPEED]?view.R_BUFFS[Attrs.SPEED]:0;
let D_SPEED=view.R_DBUFFS[Attrs.SPEED]?view.R_DBUFFS[Attrs.SPEED]:0;
const direction = currentX > finalTargetX ? -1 : 1;
const delta = ((view.speed-view.DEBUFF_SLOW)/3) * this.dt * direction;
const delta = ((view.speed*(100+B_SPEED+D_SPEED))/3) * this.dt * direction;
const newX = view.node.position.x + delta;
// 设置朝向

View File

@@ -40,7 +40,7 @@ const { ccclass, property } = _decorator;
* const dbuffConf: DbuffConf = {
* debuff: DBuff.STUN, // 眩晕
* BType: BType.VALUE, // 数值型
* dev: 20, // 减少值
* deV: 20, // 减少值
* deC: 3, // 持续3秒
* deR: 100 // 触发概率(100%)
* };
@@ -107,18 +107,17 @@ export class HeroViewComp extends CCComp {
base_mp: number = 100;
Attrs:any=[]
//数值型debuff
V_DBUFF:any[]=[] //持久
V_DBUFFS:any[]=[] //临时 带时间
//百分比型debuff
R_DBUFF:any[]=[] //持久
R_DBUFFS:any[]=[] //临时 带时间
//数值型buff
V_BUFF:any[]=[] //持久
V_BUFFS:any[]=[] //临时 带时间
//百分比型buff
R_BUFF:any[]=[] //持久
R_BUFFS:any[]=[] //临时 带时间
//数值型debuff - 改为键值对形式,可通过 V_DBUFF[Attrs.HP_MAX] 直接访问
V_DBUFF:Record<number, any> = {} //持久
R_DBUFF:Record<number, any> = {} //持久
V_BUFF:Record<number, any> = {} //持久
R_BUFF:Record<number, any> = {} //持久
V_DBUFFS:Record<number, any> = {} //临时 带时间
R_DBUFFS:Record<number, any> = {} //临时 带时间
V_BUFFS:Record<number, any> = {} //临时 带时间
R_BUFFS:Record<number, any> = {} //临时 带时间
atk_count: number = 0;
atked_count: number = 0;
@@ -173,14 +172,14 @@ export class HeroViewComp extends CCComp {
if (!heroInfo) return;
// 清空现有 buff/debuff
this.V_BUFF = [];
this.V_BUFFS = [];
this.R_BUFF = [];
this.R_BUFFS = [];
this.V_DBUFF = [];
this.V_DBUFFS = [];
this.R_DBUFF = [];
this.R_DBUFFS = [];
this.V_BUFF = {};
this.V_BUFFS = {};
this.R_BUFF = {};
this.R_BUFFS = {};
this.V_DBUFF = {};
this.V_DBUFFS = {};
this.R_DBUFF = {};
this.R_DBUFFS = {};
// 加载初始 buff
if (heroInfo.buff && heroInfo.buff.length > 0) {
@@ -206,30 +205,50 @@ export class HeroViewComp extends CCComp {
* @param buffConf buff 配置 (来自 SkillSet.BuffConf 或 heroSet.buff)
*/
addBuff(buffConf: BuffConf) {
// 基于类型和持续时间分类存储
// 基于类型和持续时间分类存储,使用属性作为键
const attrKey = buffConf.buff;
if (buffConf.BType === BType.VALUE) {
// 数值型 buff
if (buffConf.buC === 0) {
// 持久型
this.V_BUFF.push({...buffConf});
// 持久型 - 如果已存在,累加数值
if (this.V_BUFF[attrKey]) {
this.V_BUFF[attrKey].buV += buffConf.buV;
} else {
// 临时型 - 添加剩余时间属性
this.V_BUFFS.push({
this.V_BUFF[attrKey] = {...buffConf};
}
} else {
// 临时型 - 如果已存在,累加数值并重置时间
if (this.V_BUFFS[attrKey]) {
this.V_BUFFS[attrKey].buV += buffConf.buV;
this.V_BUFFS[attrKey].remainTime = Math.max(this.V_BUFFS[attrKey].remainTime, buffConf.buC);
} else {
this.V_BUFFS[attrKey] = {
...buffConf,
remainTime: buffConf.buC
});
};
}
}
} else {
// 百分比型 buff
if (buffConf.buC === 0) {
// 持久型
this.R_BUFF.push({...buffConf});
// 持久型 - 如果已存在,累加百分比
if (this.R_BUFF[attrKey]) {
this.R_BUFF[attrKey].buV += buffConf.buV;
} else {
// 临时型 - 添加剩余时间属性
this.R_BUFFS.push({
this.R_BUFF[attrKey] = {...buffConf};
}
} else {
// 临时型 - 如果已存在,累加百分比并重置时间
if (this.R_BUFFS[attrKey]) {
this.R_BUFFS[attrKey].buV += buffConf.buV;
this.R_BUFFS[attrKey].remainTime = Math.max(this.R_BUFFS[attrKey].remainTime, buffConf.buC);
} else {
this.R_BUFFS[attrKey] = {
...buffConf,
remainTime: buffConf.buC
});
};
}
}
}
// 立即重新计算属性
@@ -251,38 +270,59 @@ export class HeroViewComp extends CCComp {
// attrField >= 0 表示属性类 debuff会修改属性
const attrField = getAttrFieldFromDebuff(dbuffConf.debuff);
// 使用 debuff 类型作为键(支持状态类 debuffattrField = -1
const debuffKey = dbuffConf.debuff;
// 基于类型和持续时间分类存储
if (dbuffConf.BType === BType.VALUE) {
// 数值型 debuff
if (dbuffConf.deC === 0) {
// 持久型
this.V_DBUFF.push({
// 持久型 - 如果已存在,累加数值
if (this.V_DBUFF[debuffKey]) {
this.V_DBUFF[debuffKey].deV += dbuffConf.deV;
} else {
this.V_DBUFF[debuffKey] = {
...dbuffConf,
attrField: attrField
});
};
}
} else {
// 临时型 - 添加剩余时间属性
this.V_DBUFFS.push({
// 临时型 - 如果已存在,累加数值并重置时间
if (this.V_DBUFFS[debuffKey]) {
this.V_DBUFFS[debuffKey].deV += dbuffConf.deV;
this.V_DBUFFS[debuffKey].remainTime = Math.max(this.V_DBUFFS[debuffKey].remainTime, dbuffConf.deC);
} else {
this.V_DBUFFS[debuffKey] = {
...dbuffConf,
attrField: attrField,
remainTime: dbuffConf.deC
});
};
}
}
} else {
// 百分比型 debuff
if (dbuffConf.deC === 0) {
// 持久型
this.R_DBUFF.push({
// 持久型 - 如果已存在,累加百分比
if (this.R_DBUFF[debuffKey]) {
this.R_DBUFF[debuffKey].deV += dbuffConf.deV;
} else {
this.R_DBUFF[debuffKey] = {
...dbuffConf,
attrField: attrField
});
};
}
} else {
// 临时型 - 添加剩余时间属性
this.R_DBUFFS.push({
// 临时型 - 如果已存在,累加百分比并重置时间
if (this.R_DBUFFS[debuffKey]) {
this.R_DBUFFS[debuffKey].deV += dbuffConf.deV;
this.R_DBUFFS[debuffKey].remainTime = Math.max(this.R_DBUFFS[debuffKey].remainTime, dbuffConf.deC);
} else {
this.R_DBUFFS[debuffKey] = {
...dbuffConf,
attrField: attrField,
remainTime: dbuffConf.deC
});
};
}
}
}
// 立即重新计算属性
@@ -334,14 +374,16 @@ export class HeroViewComp extends CCComp {
*/
private applyValueBuffs() {
// 持久型 buff
for (const buff of this.V_BUFF) {
for (const attrKey in this.V_BUFF) {
const buff = this.V_BUFF[attrKey];
if (buff.buff !== undefined) {
this.Attrs[buff.buff] += buff.buV;
}
}
// 临时型 buff
for (const buff of this.V_BUFFS) {
for (const attrKey in this.V_BUFFS) {
const buff = this.V_BUFFS[attrKey];
if (buff.buff !== undefined) {
this.Attrs[buff.buff] += buff.buV;
}
@@ -362,7 +404,8 @@ export class HeroViewComp extends CCComp {
baseValues[Attrs.MAP] = this.base_map;
// 持久型 buff
for (const buff of this.R_BUFF) {
for (const attrKey in this.R_BUFF) {
const buff = this.R_BUFF[attrKey];
if (buff.buff !== undefined) {
const baseVal = baseValues[buff.buff] || this.Attrs[buff.buff];
this.Attrs[buff.buff] += Math.floor(baseVal * (buff.buV / 100));
@@ -370,7 +413,8 @@ export class HeroViewComp extends CCComp {
}
// 临时型 buff
for (const buff of this.R_BUFFS) {
for (const attrKey in this.R_BUFFS) {
const buff = this.R_BUFFS[attrKey];
if (buff.buff !== undefined) {
const baseVal = baseValues[buff.buff] || this.Attrs[buff.buff];
this.Attrs[buff.buff] += Math.floor(baseVal * (buff.buV / 100));
@@ -383,18 +427,20 @@ export class HeroViewComp extends CCComp {
*/
private applyValueDebuffs() {
// 持久型 debuff
for (const debuff of this.V_DBUFF) {
for (const debuffKey in this.V_DBUFF) {
const debuff = this.V_DBUFF[debuffKey];
// 跳过状态类 debuffattrField === -1
if (debuff.attrField !== undefined && debuff.attrField >= 0) {
this.Attrs[debuff.attrField] -= debuff.dev;
this.Attrs[debuff.attrField] -= debuff.deV;
}
}
// 临时型 debuff
for (const debuff of this.V_DBUFFS) {
for (const debuffKey in this.V_DBUFFS) {
const debuff = this.V_DBUFFS[debuffKey];
// 跳过状态类 debuffattrField === -1
if (debuff.attrField !== undefined && debuff.attrField >= 0) {
this.Attrs[debuff.attrField] -= debuff.dev;
this.Attrs[debuff.attrField] -= debuff.deV;
}
}
}
@@ -413,20 +459,22 @@ export class HeroViewComp extends CCComp {
baseValues[Attrs.MAP] = this.base_map;
// 持久型 debuff
for (const debuff of this.R_DBUFF) {
for (const debuffKey in this.R_DBUFF) {
const debuff = this.R_DBUFF[debuffKey];
// 跳过状态类 debuffattrField === -1
if (debuff.attrField !== undefined && debuff.attrField >= 0) {
const baseVal = baseValues[debuff.attrField] || this.Attrs[debuff.attrField];
this.Attrs[debuff.attrField] -= Math.floor(baseVal * (debuff.dev / 100));
this.Attrs[debuff.attrField] -= Math.floor(baseVal * (debuff.deV / 100));
}
}
// 临时型 debuff
for (const debuff of this.R_DBUFFS) {
for (const debuffKey in this.R_DBUFFS) {
const debuff = this.R_DBUFFS[debuffKey];
// 跳过状态类 debuffattrField === -1
if (debuff.attrField !== undefined && debuff.attrField >= 0) {
const baseVal = baseValues[debuff.attrField] || this.Attrs[debuff.attrField];
this.Attrs[debuff.attrField] -= Math.floor(baseVal * (debuff.dev / 100));
this.Attrs[debuff.attrField] -= Math.floor(baseVal * (debuff.deV / 100));
}
}
}
@@ -461,37 +509,37 @@ export class HeroViewComp extends CCComp {
let needRecalculate = false;
// 更新临时型数值 buff
for (let i = this.V_BUFFS.length - 1; i >= 0; i--) {
this.V_BUFFS[i].remainTime -= dt;
if (this.V_BUFFS[i].remainTime <= 0) {
this.V_BUFFS.splice(i, 1);
for (const attrKey in this.V_BUFFS) {
this.V_BUFFS[attrKey].remainTime -= dt;
if (this.V_BUFFS[attrKey].remainTime <= 0) {
delete this.V_BUFFS[attrKey];
needRecalculate = true;
}
}
// 更新临时型百分比 buff
for (let i = this.R_BUFFS.length - 1; i >= 0; i--) {
this.R_BUFFS[i].remainTime -= dt;
if (this.R_BUFFS[i].remainTime <= 0) {
this.R_BUFFS.splice(i, 1);
for (const attrKey in this.R_BUFFS) {
this.R_BUFFS[attrKey].remainTime -= dt;
if (this.R_BUFFS[attrKey].remainTime <= 0) {
delete this.R_BUFFS[attrKey];
needRecalculate = true;
}
}
// 更新临时型数值 debuff
for (let i = this.V_DBUFFS.length - 1; i >= 0; i--) {
this.V_DBUFFS[i].remainTime -= dt;
if (this.V_DBUFFS[i].remainTime <= 0) {
this.V_DBUFFS.splice(i, 1);
for (const debuffKey in this.V_DBUFFS) {
this.V_DBUFFS[debuffKey].remainTime -= dt;
if (this.V_DBUFFS[debuffKey].remainTime <= 0) {
delete this.V_DBUFFS[debuffKey];
needRecalculate = true;
}
}
// 更新临时型百分比 debuff
for (let i = this.R_DBUFFS.length - 1; i >= 0; i--) {
this.R_DBUFFS[i].remainTime -= dt;
if (this.R_DBUFFS[i].remainTime <= 0) {
this.R_DBUFFS.splice(i, 1);
for (const debuffKey in this.R_DBUFFS) {
this.R_DBUFFS[debuffKey].remainTime -= dt;
if (this.R_DBUFFS[debuffKey].remainTime <= 0) {
delete this.R_DBUFFS[debuffKey];
needRecalculate = true;
}
}