refactor(battle): 使用GameConst统一管理魔法数字

将战斗系统中的硬编码数字替换为GameConst中的常量定义,包括AI检测频率、技能延迟、索敌范围等
This commit is contained in:
walkpan
2026-01-03 09:05:13 +08:00
parent 20a7c85b14
commit a758d48914
3 changed files with 69 additions and 23 deletions

View File

@@ -0,0 +1,36 @@
/**
* 游戏通用常量定义
* 用于替换硬编码数字,统一管理魔法数字
*/
export const GameConst = {
/** 战斗系统常量 */
Battle: {
/** AI检测频率降低频率以优化性能 */
AI_CHECK_INTERVAL: 0.2,
/** 技能施放延迟(秒):用于动画表现衔接 */
SKILL_CAST_DELAY: 0.3,
/** 默认索敌/攻击范围 */
DEFAULT_SEARCH_RANGE: 300,
/** 默认目标X坐标右侧阵营 */
DEFAULT_TARGET_X_RIGHT: 400,
/** 默认目标X坐标左侧阵营 */
DEFAULT_TARGET_X_LEFT: -400,
/** 默认目标Z坐标 */
DEFAULT_TARGET_Z: 1,
/** 索敌时的Y轴偏移修正 */
SEARCH_Y_OFFSET: 30,
},
/** 技能相关常量 */
Skill: {
/** 最小目标数量 */
MIN_TARGET_COUNT: 1,
}
};

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.24",
"importer": "typescript",
"imported": true,
"uuid": "553dfb74-22f0-490d-a17e-b67757160d9b",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -9,6 +9,7 @@ import { smc } from "../common/SingletonModuleComp";
import { TalComp } from "./TalComp";
import { TalEffet, TriType } from "../common/config/TalSet";
import { BoxSet, FacSet } from "../common/config/GameSet";
import { GameConst } from "../common/config/GameConst";
import { Attrs } from "../common/config/HeroAttrs";
/**
@@ -40,7 +41,7 @@ export class SACastSystem extends ecs.ComblockSystem implements ecs.ISystemUpdat
// AI 降频每0.2秒执行一次
skills.ai_timer += this.dt;
if (skills.ai_timer < 0.2) return;
if (skills.ai_timer < GameConst.Battle.AI_CHECK_INTERVAL) return;
skills.ai_timer = 0;
const heroAttrs = e.get(HeroAttrsComp);
@@ -186,7 +187,7 @@ export class SACastSystem extends ecs.ComblockSystem implements ecs.ISystemUpdat
}
// 2.1 普通攻击逻辑
if (hset === HSSet.atk){
let delay = 0.3
let delay = GameConst.Battle.SKILL_CAST_DELAY
let ext_dmg = heroAttrs.useCountValTal(TalEffet.ATK_DMG);
heroView.scheduleOnce(() => {
this.createSkill(s_uuid, heroView,targets,ext_dmg);
@@ -194,7 +195,7 @@ export class SACastSystem extends ecs.ComblockSystem implements ecs.ISystemUpdat
//风怒wfuny 只针对 普通攻击起效
if (heroAttrs.useCountTal(TalEffet.WFUNY)){
let ext2_dmg = heroAttrs.useCountValTal(TalEffet.ATK_DMG);
let delay = 0.3
let delay = GameConst.Battle.SKILL_CAST_DELAY
heroView.playSkillEffect(s_uuid);
//需要再添加 风怒动画
heroView.scheduleOnce(() => {
@@ -204,7 +205,7 @@ export class SACastSystem extends ecs.ComblockSystem implements ecs.ISystemUpdat
}
// 2.2 技能攻击逻辑
if(hset === HSSet.skill){
let delay = 0.3
let delay = GameConst.Battle.SKILL_CAST_DELAY
let ext_dmg = heroAttrs.useCountValTal(TalEffet.SKILL_DMG);
heroView.scheduleOnce(() => {
this.createSkill(s_uuid, heroView,targets,ext_dmg);
@@ -212,7 +213,7 @@ export class SACastSystem extends ecs.ComblockSystem implements ecs.ISystemUpdat
// 双技能 只针对 技能起效
if(heroAttrs.useCountTal(TalEffet.D_SKILL)){
let ext2_dmg = heroAttrs.useCountValTal(TalEffet.SKILL_DMG);
let delay = 0.3
let delay = GameConst.Battle.SKILL_CAST_DELAY
heroView.playSkillEffect(s_uuid);
//需要再添加 双技能动画
heroView.scheduleOnce(() => {
@@ -222,7 +223,7 @@ export class SACastSystem extends ecs.ComblockSystem implements ecs.ISystemUpdat
}
// 2.3 必杀技能逻辑
if(hset === HSSet.max){
let delay = 0.3
let delay = GameConst.Battle.SKILL_CAST_DELAY
heroView.playSkillEffect(s_uuid);
//需要再添加 最大伤害动画
heroView.scheduleOnce(() => {
@@ -275,7 +276,7 @@ export class SACastSystem extends ecs.ComblockSystem implements ecs.ISystemUpdat
if (!heroAttrs) return [];
const config = SkillSet[s_uuid];
if (!config) return this.sDefaultTargets(caster, heroAttrs.fac);
const maxTargets = Math.max(1, config.t_num ?? 1);
const maxTargets = Math.max(GameConst.Skill.MIN_TARGET_COUNT, config.t_num ?? 1);
const targets = this.sDamageTargets(caster, config, maxTargets);
if (targets.length === 0) {
targets.push(...this.sDefaultTargets(caster, heroAttrs.fac));
@@ -291,7 +292,7 @@ export class SACastSystem extends ecs.ComblockSystem implements ecs.ISystemUpdat
const heroAttrs = caster.ent.get(HeroAttrsComp);
if (!heroAttrs) return targets;
const range = Number(config.dis ?? 300);
const range = Number(config.dis ?? GameConst.Battle.DEFAULT_SEARCH_RANGE);
const enemyPositions = this.findNearbyEnemies(caster, heroAttrs.fac, range);
// 选择最多maxTargets个目标
@@ -315,8 +316,8 @@ export class SACastSystem extends ecs.ComblockSystem implements ecs.ISystemUpdat
*/
private sDefaultTargets(caster: HeroViewComp, fac: number): Vec3[] {
const targets: Vec3[] = [];
const defaultX = fac === 0 ? 400 : -400;
targets.push(v3(defaultX, BoxSet.GAME_LINE, 1));
const defaultX = fac === 0 ? GameConst.Battle.DEFAULT_TARGET_X_RIGHT : GameConst.Battle.DEFAULT_TARGET_X_LEFT;
targets.push(v3(defaultX, BoxSet.GAME_LINE, GameConst.Battle.DEFAULT_TARGET_Z));
return targets;
}
@@ -335,7 +336,7 @@ export class SACastSystem extends ecs.ComblockSystem implements ecs.ISystemUpdat
if (model.is_dead) return false;
if (model.fac === fac) return false;
const pos = view.node.position.clone();
pos.y += 30;
pos.y += GameConst.Battle.SEARCH_Y_OFFSET;
const dist = Math.abs(currentPos.x - pos.x);
if (dist <= range) {
const laneBias = Math.abs(currentPos.y - pos.y);
@@ -427,7 +428,7 @@ export class SACastSystem extends ecs.ComblockSystem implements ecs.ISystemUpdat
const targets = this.sBuffTargets(casterEntity, heroView, hAttrsCom, config);
if (targets.length === 0) return false;
const delay = 0.3;
const delay = GameConst.Battle.SKILL_CAST_DELAY;
heroView.scheduleOnce(() => {
for (const targetEntity of targets) {
@@ -451,7 +452,7 @@ export class SACastSystem extends ecs.ComblockSystem implements ecs.ISystemUpdat
/**
* 选择Buff目标
*/
private sBuffTargets(casterEntity: ecs.Entity, casterView: HeroViewComp, heroAttrs: HeroAttrsComp, config: any): ecs.Entity[] {
private sBuffTargets(casterEntity: ecs.Entity, casterView: HeroViewComp, heroAttrs: HeroAttrsComp, config: SkillConfig): ecs.Entity[] {
const targets: ecs.Entity[] = [];
const tGroup = config.TGroup;
@@ -463,8 +464,8 @@ export class SACastSystem extends ecs.ComblockSystem implements ecs.ISystemUpdat
// 2. 团队/友军
if (tGroup === TGroup.Team || tGroup === TGroup.Ally) {
const maxTargets = Math.max(1, Number(config.t_num ?? 1));
const range = Number(config.dis ?? 300);
const maxTargets = Math.max(GameConst.Skill.MIN_TARGET_COUNT, Number(config.t_num ?? 1));
const range = Number(config.dis ?? GameConst.Battle.DEFAULT_SEARCH_RANGE);
ecs.query(ecs.allOf(HeroAttrsComp, HeroViewComp)).forEach(e => {
const model = e.get(HeroAttrsComp);
@@ -497,7 +498,7 @@ export class SACastSystem extends ecs.ComblockSystem implements ecs.ISystemUpdat
if (targets.length === 0) return false;
const healAmount = config.ap * hAttrsCom.Attrs[Attrs.HP_MAX]/100;
const delay = 0.3;
const delay = GameConst.Battle.SKILL_CAST_DELAY;
heroView.scheduleOnce(() => {
for (const targetEntity of targets) {
@@ -526,7 +527,7 @@ export class SACastSystem extends ecs.ComblockSystem implements ecs.ISystemUpdat
if (targets.length === 0) return false;
const shieldAmount = config.ap * hAttrsCom.Attrs[Attrs.HP_MAX]/100;
const delay = 0.3;
const delay = GameConst.Battle.SKILL_CAST_DELAY;
heroView.scheduleOnce(() => {
for (const targetEntity of targets) {
@@ -546,10 +547,10 @@ export class SACastSystem extends ecs.ComblockSystem implements ecs.ISystemUpdat
/**
* 选择治疗目标
*/
private sHealTargets(caster: HeroViewComp, heroAttrs: HeroAttrsComp, config: any): ecs.Entity[] {
private sHealTargets(caster: HeroViewComp, heroAttrs: HeroAttrsComp, config: SkillConfig): ecs.Entity[] {
const targets: ecs.Entity[] = [];
const maxTargets = Math.max(1, Number(config.t_num ?? 1));
const range = Number(config.dis ?? 300);
const maxTargets = Math.max(GameConst.Skill.MIN_TARGET_COUNT, Number(config.t_num ?? 1));
const range = Number(config.dis ?? GameConst.Battle.DEFAULT_SEARCH_RANGE);
ecs.query(ecs.allOf(HeroAttrsComp, HeroViewComp)).forEach(e => {
const model = e.get(HeroAttrsComp);
@@ -577,10 +578,10 @@ export class SACastSystem extends ecs.ComblockSystem implements ecs.ISystemUpdat
/**
* 选择护盾目标
*/
private sShieldTargets(caster: HeroViewComp, heroAttrs: HeroAttrsComp, config: any): ecs.Entity[] {
private sShieldTargets(caster: HeroViewComp, heroAttrs: HeroAttrsComp, config: SkillConfig): ecs.Entity[] {
const targets: ecs.Entity[] = [];
const maxTargets = Math.max(1, Number(config.t_num ?? 1));
const range = Number(config.dis ?? 300);
const maxTargets = Math.max(GameConst.Skill.MIN_TARGET_COUNT, Number(config.t_num ?? 1));
const range = Number(config.dis ?? GameConst.Battle.DEFAULT_SEARCH_RANGE);
ecs.query(ecs.allOf(HeroAttrsComp, HeroViewComp)).forEach(e => {
const model = e.get(HeroAttrsComp);