fix: 修复英雄召唤合并逻辑的竞态条件

引入召唤队列和异步处理机制,确保英雄召唤和合并操作顺序执行,避免因并发调用导致的逻辑错误。同时将合并所需英雄数量从3调整为2以匹配新的处理流程。
This commit is contained in:
panw
2026-03-27 15:03:32 +08:00
parent ccccf025c5
commit 829866fe60

View File

@@ -21,9 +21,11 @@ export class MissionHeroCompComp extends CCComp {
Friend_is_dead:boolean=false
current_hero_uuid:number=0
current_hero_num:number=-1
merge_need_count:number=3
merge_need_count:number=2
merge_max_lv:number=3
is_merging:boolean=false
is_processing_queue:boolean=false
summon_queue:{ uuid: number; hero_lv: number }[]=[]
heros:any=[]
onLoad(){
this.on(GameEvent.FightReady,this.fight_ready,this)
@@ -60,24 +62,10 @@ export class MissionHeroCompComp extends CCComp {
}
private async call_hero(event: string, args: any){
if (this.is_merging) return;
const uuid = Number(args?.uuid ?? 1001);
const hero_lv = Math.max(1, Number(args?.hero_lv ?? 1));
this.addHero(uuid, hero_lv);
if (!this.canMergeLevel(hero_lv)) return;
const needCount = this.getMergeNeedCount();
const aliveHeroes = this.getAliveHeroes();
const mergeHeroes = this.pickMergeHeroes(aliveHeroes, uuid, hero_lv, needCount);
if (mergeHeroes.length === needCount) {
this.is_merging = true;
try {
const mergedLv = await this.mergeGroupHeroes(mergeHeroes, uuid, hero_lv);
await this.tryChainMerge(uuid, mergedLv);
} finally {
this.is_merging = false;
}
return;
}
this.summon_queue.push({ uuid, hero_lv });
this.processSummonQueue();
}
/** 添加英雄 */
private addHero(uuid:number=1001,hero_lv:number=1) {
@@ -147,6 +135,36 @@ export class MissionHeroCompComp extends CCComp {
return hero_lv < Math.max(1, this.merge_max_lv);
}
private async processSummonQueue() {
if (this.is_processing_queue) return;
this.is_processing_queue = true;
try {
while (this.summon_queue.length > 0) {
const payload = this.summon_queue.shift();
if (!payload) continue;
await this.handleSingleSummon(payload.uuid, payload.hero_lv);
}
} finally {
this.is_processing_queue = false;
}
}
private async handleSingleSummon(uuid: number, hero_lv: number) {
this.addHero(uuid, hero_lv);
if (!this.canMergeLevel(hero_lv)) return;
const needCount = this.getMergeNeedCount();
const aliveHeroes = this.getAliveHeroes();
const mergeHeroes = this.pickMergeHeroes(aliveHeroes, uuid, hero_lv, needCount);
if (mergeHeroes.length !== needCount) return;
this.is_merging = true;
try {
const mergedLv = await this.mergeGroupHeroes(mergeHeroes, uuid, hero_lv);
await this.tryChainMerge(uuid, mergedLv);
} finally {
this.is_merging = false;
}
}
private mergeDestroyAtBirth(mergeHeroes: Hero[], spawnPos: Vec3): Promise<void> {
return new Promise((resolve) => {
let doneCount = 0;