refactor(hero): 优化Buff和Debuff的数据结构及处理逻辑
- 新增移动速度相关属性:DBuff.SPEED和Attrs.SPEED - 修改BattleMoveSystem中移动速度计算,支持速度百分比加成 - Buff和Debuff由数组改为基于属性键的对象存储,方便访问和合并处理 - 持久型Buff/Debuff累加数值或百分比,临时型则累加并刷新剩余时间 - 重新计算属性时遍历键值对,跳过状态类debuff(attrField=-1) - 更新临时型Buff/Debuff剩余时间,过期时从对象中删除 - 代码结构更清晰,访问及更新性能提升
This commit is contained in:
@@ -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];
|
||||
|
||||
@@ -92,7 +92,7 @@ export class BattleMoveSystem extends ecs.ComblockSystem implements ecs.ISystemU
|
||||
if (Math.abs(currentX - finalTargetX) > 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;
|
||||
|
||||
// 设置朝向
|
||||
|
||||
@@ -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_BUFF[attrKey] = {...buffConf};
|
||||
}
|
||||
} else {
|
||||
// 临时型 - 添加剩余时间属性
|
||||
this.V_BUFFS.push({
|
||||
...buffConf,
|
||||
remainTime: buffConf.buC
|
||||
});
|
||||
// 临时型 - 如果已存在,累加数值并重置时间
|
||||
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_BUFF[attrKey] = {...buffConf};
|
||||
}
|
||||
} else {
|
||||
// 临时型 - 添加剩余时间属性
|
||||
this.R_BUFFS.push({
|
||||
...buffConf,
|
||||
remainTime: buffConf.buC
|
||||
});
|
||||
// 临时型 - 如果已存在,累加百分比并重置时间
|
||||
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
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
// 立即重新计算属性
|
||||
@@ -250,39 +269,60 @@ export class HeroViewComp extends CCComp {
|
||||
// attrField = -1 表示状态类 debuff(只缓存,不修改属性)
|
||||
// attrField >= 0 表示属性类 debuff(会修改属性)
|
||||
const attrField = getAttrFieldFromDebuff(dbuffConf.debuff);
|
||||
|
||||
// 使用 debuff 类型作为键(支持状态类 debuff,attrField = -1)
|
||||
const debuffKey = dbuffConf.debuff;
|
||||
|
||||
// 基于类型和持续时间分类存储
|
||||
if (dbuffConf.BType === BType.VALUE) {
|
||||
// 数值型 debuff
|
||||
if (dbuffConf.deC === 0) {
|
||||
// 持久型
|
||||
this.V_DBUFF.push({
|
||||
...dbuffConf,
|
||||
attrField: attrField
|
||||
});
|
||||
// 持久型 - 如果已存在,累加数值
|
||||
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({
|
||||
...dbuffConf,
|
||||
attrField: attrField,
|
||||
remainTime: dbuffConf.deC
|
||||
});
|
||||
// 临时型 - 如果已存在,累加数值并重置时间
|
||||
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({
|
||||
...dbuffConf,
|
||||
attrField: attrField
|
||||
});
|
||||
// 持久型 - 如果已存在,累加百分比
|
||||
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({
|
||||
...dbuffConf,
|
||||
attrField: attrField,
|
||||
remainTime: dbuffConf.deC
|
||||
});
|
||||
// 临时型 - 如果已存在,累加百分比并重置时间
|
||||
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,7 +427,8 @@ 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];
|
||||
// 跳过状态类 debuff(attrField === -1)
|
||||
if (debuff.attrField !== undefined && debuff.attrField >= 0) {
|
||||
this.Attrs[debuff.attrField] -= debuff.dev;
|
||||
@@ -391,7 +436,8 @@ export class HeroViewComp extends CCComp {
|
||||
}
|
||||
|
||||
// 临时型 debuff
|
||||
for (const debuff of this.V_DBUFFS) {
|
||||
for (const debuffKey in this.V_DBUFFS) {
|
||||
const debuff = this.V_DBUFFS[debuffKey];
|
||||
// 跳过状态类 debuff(attrField === -1)
|
||||
if (debuff.attrField !== undefined && debuff.attrField >= 0) {
|
||||
this.Attrs[debuff.attrField] -= debuff.dev;
|
||||
@@ -413,7 +459,8 @@ 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];
|
||||
// 跳过状态类 debuff(attrField === -1)
|
||||
if (debuff.attrField !== undefined && debuff.attrField >= 0) {
|
||||
const baseVal = baseValues[debuff.attrField] || this.Attrs[debuff.attrField];
|
||||
@@ -422,7 +469,8 @@ export class HeroViewComp extends CCComp {
|
||||
}
|
||||
|
||||
// 临时型 debuff
|
||||
for (const debuff of this.R_DBUFFS) {
|
||||
for (const debuffKey in this.R_DBUFFS) {
|
||||
const debuff = this.R_DBUFFS[debuffKey];
|
||||
// 跳过状态类 debuff(attrField === -1)
|
||||
if (debuff.attrField !== undefined && debuff.attrField >= 0) {
|
||||
const baseVal = baseValues[debuff.attrField] || this.Attrs[debuff.attrField];
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user