refactor(hero): 优化Buff和Debuff的数据结构及处理逻辑

- 新增移动速度相关属性:DBuff.SPEED和Attrs.SPEED
- 修改BattleMoveSystem中移动速度计算,支持速度百分比加成
- Buff和Debuff由数组改为基于属性键的对象存储,方便访问和合并处理
- 持久型Buff/Debuff累加数值或百分比,临时型则累加并刷新剩余时间
- 重新计算属性时遍历键值对,跳过状态类debuff(attrField=-1)
- 更新临时型Buff/Debuff剩余时间,过期时从对象中删除
- 代码结构更清晰,访问及更新性能提升
This commit is contained in:
2025-10-17 10:27:21 +08:00
parent d8ba69aada
commit 6386d6bd80
3 changed files with 134 additions and 84 deletions

View File

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

View File

@@ -92,7 +92,7 @@ export class BattleMoveSystem extends ecs.ComblockSystem implements ecs.ISystemU
if (Math.abs(currentX - finalTargetX) > 1) { if (Math.abs(currentX - finalTargetX) > 1) {
// 确定移动方向 // 确定移动方向
const direction = currentX > finalTargetX ? -1 : 1; const direction = currentX > finalTargetX ? -1 : 1;
const delta = ((view.speed-view.DEBUFF_SLOW)/3) * this.dt * direction; const delta = ((view.speed*(100+view.R_BUFFS.SPEED))/3) * this.dt * direction;
const newX = view.node.position.x + delta; const newX = view.node.position.x + delta;
// 设置朝向 // 设置朝向

View File

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