refactor(map): 重构关卡刷怪与计时逻辑,适配30波新流程

1. 替换原固定战斗倒计时为正向计时clearTime
2. 移除旧波次配置,改用spawningEngine生成自适应怪物
3. 将波次上限从20调整为30,更新对应判断逻辑
4. 实现增量分批刷怪和自适应难度调整
5. 重置战斗状态时重置新引擎实例
This commit is contained in:
walkpan
2026-05-15 13:46:27 +08:00
parent cede98eab9
commit 8d61c67c1d
2 changed files with 93 additions and 162 deletions

View File

@@ -35,7 +35,7 @@ import { HeroInfo, HType } from "../common/config/heroSet";
import { smc } from "../common/SingletonModuleComp";
import { GameEvent } from "../common/config/GameEvent";
import {BoxSet, FacSet } from "../common/config/GameSet";
import { MonList, MonType, SpawnPowerBias, StageBossGrow, StageGrow, UpType, WaveSlotConfig, DefaultWaveSlot, IWaveSlot } from "./RogueConfig";
import { spawningEngine, GeneratedMonster, AffixType, MonType, MonList } from "./RogueConfig";
import { HeroAttrsComp } from "../hero/HeroAttrsComp";
import { MonMoveComp } from "../hero/MonMoveComp";
const { ccclass, property } = _decorator;
@@ -94,6 +94,10 @@ export class MissionMonCompComp extends CCComp {
private waveTargetCount: number = 0;
/** 当前波已生成的怪物数量 */
private waveSpawnedCount: number = 0;
/** 等待生成的怪物队列(由新肉鸽引擎提供) */
private pendingMonsters: GeneratedMonster[] = [];
/** 增量刷怪计时器 */
private spawnTimer: number = 0;
// ======================== 生命周期 ========================
@@ -108,14 +112,35 @@ export class MissionMonCompComp extends CCComp {
* 帧更新:
* 1. 检查游戏是否运行中。
* 2. 处理插队刷怪队列。
* 3. 逐步从 pendingMonsters 队列中生成怪物(受 stop_spawn_mon 限制)。
*/
protected update(dt: number): void {
if(!smc.mission.play) return
if(smc.mission.pause) return
if(smc.mission.stop_mon_action) return;
if(!smc.mission.in_fight) return;
if(smc.mission.stop_spawn_mon) return;
this.updateSpecialQueue(dt);
if(smc.mission.stop_spawn_mon) return;
// 逐步刷怪逻辑
if (this.pendingMonsters.length > 0) {
this.spawnTimer += dt;
// 控制刷怪速率:例如每 0.2 秒刷 1-2 只
if (this.spawnTimer > 0.2) {
this.spawnTimer = 0;
// 一次出 2 只,加快进度
for (let i = 0; i < 2; i++) {
if (this.pendingMonsters.length === 0) break;
const monData = this.pendingMonsters.shift()!;
const lane = this.pickBalancedLane();
this.addMonsterAt(lane, this.laneIndices[lane], monData);
this.laneIndices[lane]++;
this.waveSpawnedCount++;
}
}
}
}
// ======================== 事件处理 ========================
@@ -152,15 +177,16 @@ export class MissionMonCompComp extends CCComp {
this.waveTargetCount = 0
this.waveSpawnedCount = 0
this.MonQueue = []
this.pendingMonsters = []
this.spawnTimer = 0
this.laneIndices = [0, 0, 0];
let hasBoss = false;
const config = WaveSlotConfig[this.currentWave] || DefaultWaveSlot;
for (const slot of config) {
if (slot.type === MonType.MeleeBoss || slot.type === MonType.LongBoss || slot.type === MonType.FlyBoss) {
hasBoss = true;
}
}
// 预生成第一波数据以获取数量和 Boss 信息
const monsters = spawningEngine.generateWave(this.currentWave);
this.pendingMonsters = monsters;
this.waveTargetCount = monsters.length;
let hasBoss = monsters.some(m => m.isBoss);
oops.message.dispatchEvent(GameEvent.NewWave, {
wave: this.currentWave,
total: this.waveTargetCount,
@@ -199,13 +225,22 @@ export class MissionMonCompComp extends CCComp {
this.queueTimer = 0;
const isBoss = MonList[MonType.MeleeBoss].includes(item.uuid) ||
MonList[MonType.LongBoss].includes(item.uuid) ||
(MonList[MonType.FlyBoss] && MonList[MonType.FlyBoss].includes(item.uuid));
MonList[MonType.LongBoss].includes(item.uuid);
const upType = this.getRandomUpType();
const lane = item.flyLane !== undefined && item.flyLane >= 0 && item.flyLane <= 2 ? item.flyLane : this.pickBalancedLane();
this.addMonsterAt(lane, this.laneIndices[lane], item.uuid, isBoss, upType, Math.max(1, Number(item.level ?? 1)));
// 构造一个模拟的 GeneratedMonster 数据传递给 addMonsterAt
const base = HeroInfo[item.uuid];
const monData: GeneratedMonster = {
uuid: item.uuid,
type: MonType.Melee, // 简化的兜底,真实逻辑依赖 heroSet 配置
hp: base ? base.hp : 100,
ap: base ? base.ap : 10,
affixes: [],
isBoss: isBoss,
spawnIndex: 0
};
this.addMonsterAt(lane, this.laneIndices[lane], monData, item.level);
this.laneIndices[lane]++;
}
@@ -214,25 +249,21 @@ export class MissionMonCompComp extends CCComp {
/**
* 开始下一波:
* 1. 波数 +1 并更新全局数据。
* 2. 重置槽位并根据配置生成本波所有怪物
* 3. 分发 NewWave 事件。
* 2. 分发 NewWave 事件(实际的生成在 resetSlotSpawnData 中触发)
*/
private onTimeUpAdvanceWave() {
this.currentWave += 1;
smc.vmdata.mission_data.level = this.currentWave;
// 检查本波是否有 Boss
let hasBoss = false;
const config = WaveSlotConfig[this.currentWave] || DefaultWaveSlot;
for (const slot of config) {
if (slot.type === MonType.MeleeBoss || slot.type === MonType.LongBoss || slot.type === MonType.FlyBoss) {
hasBoss = true;
}
}
// 预生成新一波数据以获取数量和 Boss 信息
const monsters = spawningEngine.generateWave(this.currentWave);
this.pendingMonsters = monsters;
this.waveTargetCount = monsters.length;
let hasBoss = monsters.some(m => m.isBoss);
oops.message.dispatchEvent(GameEvent.NewWave, {
wave: this.currentWave,
total: this.waveTargetCount, // 此时还是上一波的怪物数量,但可以不传或后续修正
total: this.waveTargetCount,
bossWave: hasBoss,
});
}
@@ -241,60 +272,12 @@ export class MissionMonCompComp extends CCComp {
this.resetSlotSpawnData(this.currentWave);
}
/** 获取当前阶段stage = wave - 1用于属性成长计算 */
private getCurrentStage(): number {
return Math.max(0, this.currentWave - 1);
}
// ======================== 随机选取 ========================
/** 随机选取一种成长类型 */
private getRandomUpType(): UpType {
const keys = Object.keys(StageGrow).map(v => Number(v) as UpType);
const index = Math.floor(Math.random() * keys.length);
return keys[index] ?? UpType.AP1_HP1;
}
/**
* 根据怪物类型从对应池中随机选取 UUID。
* @param monType 怪物类型MonType 枚举值)
* @returns 怪物 UUID
*/
private getRandomUuidByType(monType: number): number {
const pool = (MonList as any)[monType] || MonList[MonType.Melee];
if (!pool || pool.length === 0) return 6001;
const index = Math.floor(Math.random() * pool.length);
return pool[index];
}
/**
* 计算怪物属性成长值对。
* Boss 在普通成长基础上叠加 StageBossGrow。
*
* @param upType 成长类型
* @param isBoss 是否为 Boss
* @returns [AP 成长值, HP 成长值]
*/
private resolveGrowPair(upType: UpType, isBoss: boolean): [number, number] {
const grow = StageGrow[upType] || StageGrow[UpType.AP1_HP1];
if (!isBoss) return [grow[0], grow[1]];
const bossGrow = StageBossGrow[upType] || StageBossGrow[UpType.AP1_HP1];
return [grow[0] + bossGrow[0], grow[1] + bossGrow[1]];
}
/** 获取全局刷怪强度系数 */
private getSpawnPowerBias(): number {
return SpawnPowerBias;
}
// ======================== 槽位管理 ========================
/**
* 重新分配并生成本波所有怪物:
* 重新分配本波所有怪物状态
* 1. 清理上一波残留怪物。
* 2. 读取波次配置
* 3. 依据配置和 flyLane 属性,为每只怪物分配自增索引。
* 4. 立即实例化所有怪物。
* 2. pendingMonsters 已在 onTimeUpAdvanceWave / fight_ready 中准备好,只需重置 laneIndices 即可
*
* @param wave 当前波数
*/
@@ -306,44 +289,10 @@ export class MissionMonCompComp extends CCComp {
e.destroy();
}
});
// 2. 读取波次配置
const config: IWaveSlot[] = WaveSlotConfig[wave] || DefaultWaveSlot;
// 3. 重置排号索引
// 2. 重置排号索引
this.laneIndices = [0, 0, 0];
let allMons: any[] = [];
// 解析配置
for (const slot of config) {
const isBoss = slot.type === MonType.MeleeBoss || slot.type === MonType.LongBoss || slot.type === MonType.FlyBoss;
for (let i = 0; i < slot.count; i++) {
const uuid = this.getRandomUuidByType(slot.type);
const upType = this.getRandomUpType();
// 优先使用配置的 lane否则均衡分配
let lane = slot.flyLane !== undefined ? slot.flyLane : this.pickBalancedLane();
lane = Math.max(0, Math.min(2, lane));
const req = { uuid, isBoss, upType, monLv: wave, lane };
allMons.push(req);
// 提前累加 laneIndices以便本波内的均衡分配能正确计算
this.laneIndices[lane]++;
}
}
this.waveTargetCount = allMons.length;
this.waveSpawnedCount = 0;
// 由于上面循环中已经累加了 laneIndices这里需要重置以便下面真正生成时再累加或者直接利用 allMons 生成)
this.laneIndices = [0, 0, 0];
// 4. 立即生成本波所有怪物
for (const req of allMons) {
this.addMonsterAt(req.lane, this.laneIndices[req.lane], req.uuid, req.isBoss, req.upType, req.monLv);
this.laneIndices[req.lane]++;
}
}
// ======================== 怪物生成 ========================
@@ -353,17 +302,13 @@ export class MissionMonCompComp extends CCComp {
*
* @param laneIndex 三路索引 (0 上, 1 中, 2 下)
* @param monIndex 该路级的第几个怪 (0, 1, 2...)
* @param uuid 怪物 UUID
* @param isBoss 是否为 Boss
* @param upType 属性成长类型
* @param monLv 怪物等级
* @param monData 新引擎生成的怪物数据 (含 uuid, hp, ap, affixes 等)
* @param monLv 怪物等级 (仅对旧有的 level 参数做兼容,实际属性由 monData 决定)
*/
private addMonsterAt(
laneIndex: number,
monIndex: number,
uuid: number = 1001,
isBoss: boolean = false,
upType: UpType = UpType.AP1_HP1,
monData: GeneratedMonster,
monLv: number = 1
) {
let mon = ecs.getEntity<Monster>(Monster);
@@ -371,11 +316,11 @@ export class MissionMonCompComp extends CCComp {
// 计算坐标
const spawnX = MissionMonCompComp.MON_SPAWN_START_X + monIndex * MissionMonCompComp.MON_SPAWN_GAP_X;
const landingY = BoxSet.GAME_LINE + MissionMonCompComp.LANE_Y_OFFSETS[laneIndex] + (isBoss ? 6 : 0);
const landingY = BoxSet.GAME_LINE + MissionMonCompComp.LANE_Y_OFFSETS[laneIndex] + (monData.isBoss ? 6 : 0);
const spawnPos: Vec3 = v3(spawnX, landingY + MissionMonCompComp.MON_DROP_HEIGHT, 0);
this.globalSpawnOrder = (this.globalSpawnOrder + 1) % 999;
mon.load(spawnPos, scale, uuid, isBoss, landingY, monLv, laneIndex);
mon.load(spawnPos, scale, monData.uuid, monData.isBoss, landingY, monLv, laneIndex);
// 设置渲染排序
const move = mon.get(MonMoveComp);
@@ -383,16 +328,15 @@ export class MissionMonCompComp extends CCComp {
move.spawnOrder = this.globalSpawnOrder;
}
// 计算最终属性
// 应用新引擎计算好的最终属性和词缀
const model = mon.get(HeroAttrsComp);
const base = HeroInfo[uuid];
if (!model || !base) return;
const stage = this.getCurrentStage();
const grow = this.resolveGrowPair(upType, isBoss);
const bias = Math.max(0.1, this.getSpawnPowerBias());
model.ap = Math.max(1, Math.floor((base.ap + stage * grow[0]) * bias));
model.hp_max = Math.max(1, Math.floor((base.hp + stage * grow[1]) * bias));
if (!model) return;
model.ap = monData.ap;
model.hp_max = monData.hp;
model.hp = model.hp_max;
// 将词缀记录到属性组件上,供战斗层使用
(model as any).affixes = monData.affixes || [];
}
/** ECS 组件移除时触发(当前不销毁节点) */