refactor(monster&hero): 重构三路分层逻辑与渲染排序

1. 移除飞行怪特殊判定,统一按Y轴高度处理三路渲染
2. 重命名飞行层相关变量为更准确的路次命名
3. 新增英雄自动分路均衡分配逻辑
4. 调整渲染排序规则,按Y轴高度决定上下层显示顺序
5. 修复怪物入场动画与刷怪分路逻辑
This commit is contained in:
panw
2026-05-12 16:32:25 +08:00
parent 86363f50b0
commit 20e9b1d484
5 changed files with 147 additions and 52 deletions

View File

@@ -58,6 +58,12 @@ export class MissionHeroCompComp extends CCComp {
private static readonly HERO_SPAWN_START_MELEE_X = -320
/** 远程(含中程)英雄起始出生 X 坐标 */
private static readonly HERO_SPAWN_START_RANGED_X = -320
/** 三路高度偏移(上路, 中路, 下路) */
private static readonly HERO_LANE_Y_OFFSETS = [100, 0, -100]
/** 每路前排容量 */
private static readonly HERO_LANE_CAP = 3
/** 同路内 X 间距 */
private static readonly HERO_GAP_X = 100
// ======================== 运行时属性 ========================
@@ -124,7 +130,8 @@ export class MissionHeroCompComp extends CCComp {
if (model && view) {
if (model.is_dead) {
view.alive();
const landingPos = this.resolveHeroLandingPos(model.hero_uuid);
const { lane, indexInLane } = this.pickLaneForHero(model.hero_uuid, [hero.eid]);
const landingPos = this.resolveHeroLandingPos(model.hero_uuid, lane, indexInLane);
// 不再直接设置位置,而是播放下落入场动画
// 计算出出生点(空中)
const spawnPos: Vec3 = v3(landingPos.x, landingPos.y + MissionHeroCompComp.HERO_DROP_HEIGHT, 0);
@@ -159,6 +166,53 @@ export class MissionHeroCompComp extends CCComp {
// ======================== 英雄生成 ========================
/**
* 动态分配英雄上场的路和排位(优先中路 -> 上路 -> 下路)
* @param uuid 英雄 UUID
* @param excludeEids 排除计算的实体ID数组避免复活或合成时把自己算成占据的位置
*/
private pickLaneForHero(uuid: number, excludeEids: number[] = []): { lane: number; indexInLane: number } {
const heroes = this.getAllHeroes().filter(h => {
const m = h.get(HeroAttrsComp);
return m && !m.is_dead && !excludeEids.includes(h.eid);
});
const counts = [0, 0, 0];
const baseY = HeroPos[0].pos.y;
for (const h of heroes) {
const view = h.get(HeroViewComp);
if (!view || !view.node) continue;
let y = view.node.position.y;
// 处理正在掉落状态的英雄y 值偏高)
if (y > baseY + 150) {
y -= MissionHeroCompComp.HERO_DROP_HEIGHT;
}
let nearest = 1, best = Infinity;
for (let i = 0; i < 3; i++) {
const d = Math.abs(y - (baseY + MissionHeroCompComp.HERO_LANE_Y_OFFSETS[i]));
if (d < best) {
best = d;
nearest = i;
}
}
counts[nearest]++;
}
// 优先中路(1) -> 上路(0) -> 下路(2)
const priority = [1, 0, 2];
for (const lane of priority) {
if (counts[lane] < MissionHeroCompComp.HERO_LANE_CAP) {
return { lane, indexInLane: counts[lane] };
}
}
// 溢出:仍放中路,沿 X 继续排
return { lane: 1, indexInLane: counts[1] };
}
/**
* 生成一个英雄 ECS 实体:
* - 计算出生点(空中)和落点(地面)。
@@ -173,7 +227,8 @@ export class MissionHeroCompComp extends CCComp {
console.log("addHero uuid:",uuid)
let hero = ecs.getEntity<Hero>(Hero);
let scale = 1
const landingPos = this.resolveHeroLandingPos(uuid);
const { lane, indexInLane } = this.pickLaneForHero(uuid);
const landingPos = this.resolveHeroLandingPos(uuid, lane, indexInLane);
let spawnPos:Vec3 = v3(landingPos.x, landingPos.y + MissionHeroCompComp.HERO_DROP_HEIGHT, 0);
hero.load(spawnPos,scale,uuid,landingPos.y,hero_lv,pool_lv);
@@ -193,14 +248,16 @@ export class MissionHeroCompComp extends CCComp {
* 计算英雄落点位置。
* Y 坐标来自 HeroPos 配置X 坐标根据英雄类型(近战/远程)决定。
*
* @param uuid 英雄 UUID
* @returns 落点 Vec3
* @param uuid 英雄 UUID
* @param lane 分配到的路 (0: 上, 1: 中, 2: 下)
* @param indexInLane 该路排位
* @returns 落点 Vec3
*/
private resolveHeroLandingPos(uuid: number): Vec3 {
private resolveHeroLandingPos(uuid: number, lane: number, indexInLane: number): Vec3 {
const hero_pos = 0;
const baseY = HeroPos[hero_pos].pos.y;
const baseY = HeroPos[hero_pos].pos.y + MissionHeroCompComp.HERO_LANE_Y_OFFSETS[lane];
const startX = this.resolveSpawnStartX(uuid);
return v3(startX, baseY, 0);
return v3(startX + indexInLane * MissionHeroCompComp.HERO_GAP_X, baseY, 0);
}
/**
@@ -223,17 +280,42 @@ export class MissionHeroCompComp extends CCComp {
* @param pool_lv 卡池等级
* @param ap 聚合后攻击力
* @param hp_max 聚合后最大生命值
* @param targetLane 指定生成路
* @param targetIndex 指定该路排位
* @returns 实际生成的英雄等级
*/
private addMergedHero(uuid:number, hero_lv:number, pool_lv:number, ap:number, hp_max:number): number {
const hero = this.addHero(uuid, hero_lv, pool_lv);
private addMergedHero(uuid:number, hero_lv:number, pool_lv:number, ap:number, hp_max:number, targetLane?: number, targetIndex?: number): number {
console.log("addMergedHero uuid:",uuid)
let hero = ecs.getEntity<Hero>(Hero);
let scale = 1
// 如果未指定路,则按普通添加英雄处理
let lane = targetLane;
let indexInLane = targetIndex;
if (lane === undefined || indexInLane === undefined) {
const res = this.pickLaneForHero(uuid);
lane = res.lane;
indexInLane = res.indexInLane;
}
const landingPos = this.resolveHeroLandingPos(uuid, lane, indexInLane);
let spawnPos:Vec3 = v3(landingPos.x, landingPos.y + MissionHeroCompComp.HERO_DROP_HEIGHT, 0);
hero.load(spawnPos,scale,uuid,landingPos.y,hero_lv,pool_lv);
// 召唤完成后,派发事件以更新英雄面板
const model = hero.get(HeroAttrsComp);
if (!model) return hero_lv;
model.ap = Math.max(0, ap);
model.hp_max = Math.max(1, hp_max);
model.hp = model.hp_max;
model.dirty_hp = true;
return model.lv;
if (model) {
model.ap = Math.max(0, ap);
model.hp_max = Math.max(1, hp_max);
model.hp = model.hp_max;
model.dirty_hp = true;
oops.message.dispatchEvent(GameEvent.MasterCalled, {
eid: hero.eid,
model: model
});
return model.lv;
}
return hero_lv;
}
// ======================== 英雄查询 ========================
@@ -448,19 +530,24 @@ export class MissionHeroCompComp extends CCComp {
// 聚合属性
let sumAp = 0;
let sumHpMax = 0;
const mergeEids = [];
for (let i = 0; i < mergeHeroes.length; i++) {
const model = mergeHeroes[i].get(HeroAttrsComp);
mergeEids.push(mergeHeroes[i].eid);
if (!model) continue;
sumAp += model.ap;
sumHpMax += model.hp_max;
}
// 计算出生点
const landingPos = this.resolveHeroLandingPos(uuid);
// 计算目标出生点(提前排除素材英雄所占的位置)
const { lane, indexInLane } = this.pickLaneForHero(uuid, mergeEids);
const landingPos = this.resolveHeroLandingPos(uuid, lane, indexInLane);
const spawnPos:Vec3 = v3(landingPos.x, landingPos.y + MissionHeroCompComp.HERO_DROP_HEIGHT, 0);
// 汇聚 → 特效 → 生成
await this.mergeDestroyAtBirth(mergeHeroes, spawnPos);
await this.playMergeBoomFx(spawnPos);
return this.addMergedHero(uuid, Math.min(this.merge_max_lv, hero_lv + 1), pool_lv, sumAp, sumHpMax);
return this.addMergedHero(uuid, Math.min(this.merge_max_lv, hero_lv + 1), pool_lv, sumAp, sumHpMax, lane, indexInLane);
}
/**