实现英雄合并时的平滑移动动画,避免直接销毁的突兀感。为 Hero 类新增 mergeToBirthAndDestroy 方法,使英雄在销毁前移动到生成点。在 MissionHeroComp 中,合并逻辑改为异步等待动画完成,并添加防重复合并标志。
163 lines
5.7 KiB
TypeScript
163 lines
5.7 KiB
TypeScript
import { _decorator, v3, Vec3 } from "cc";
|
|
import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
|
|
import { CCComp } from "../../../../extensions/oops-plugin-framework/assets/module/common/CCComp";
|
|
import { Hero } from "../hero/Hero";
|
|
import { smc } from "../common/SingletonModuleComp";
|
|
import { Timer } from "db://oops-framework/core/common/timer/Timer";
|
|
import { GameEvent } from "../common/config/GameEvent";
|
|
import { HeroPos } from "../common/config/heroSet";
|
|
import { oops } from "../../../../extensions/oops-plugin-framework/assets/core/Oops";
|
|
import { HeroAttrsComp } from "../hero/HeroAttrsComp";
|
|
import { FacSet } from "../common/config/GameSet";
|
|
const { ccclass } = _decorator;
|
|
|
|
/** 视图层对象 */
|
|
@ccclass('MissionHeroCompComp')
|
|
@ecs.register('MissionHeroComp', false)
|
|
export class MissionHeroCompComp extends CCComp {
|
|
private static readonly HERO_DROP_HEIGHT = 260
|
|
timer:Timer=new Timer(2)
|
|
Friend_is_dead:boolean=false
|
|
current_hero_uuid:number=0
|
|
current_hero_num:number=-1
|
|
is_merging:boolean=false
|
|
heros:any=[]
|
|
onLoad(){
|
|
this.on(GameEvent.FightReady,this.fight_ready,this)
|
|
this.on(GameEvent.Zhaohuan,this.zhao_huan,this)
|
|
this.on(GameEvent.MissionEnd,this.clear_heros,this)
|
|
oops.message.on(GameEvent.CallHero,this.call_hero,this)
|
|
}
|
|
|
|
onDestroy(){
|
|
oops.message.off(GameEvent.CallHero,this.call_hero,this)
|
|
oops.message.off(GameEvent.FightReady,this.fight_ready,this)
|
|
oops.message.off(GameEvent.Zhaohuan,this.zhao_huan,this)
|
|
oops.message.off(GameEvent.MissionEnd,this.clear_heros,this)
|
|
}
|
|
|
|
start() {
|
|
// this.test_call()
|
|
}
|
|
clear_heros(){
|
|
const heroes = this.getAliveHeroes();
|
|
for (let i = 0; i < heroes.length; i++) {
|
|
heroes[i].destroy();
|
|
}
|
|
}
|
|
fight_ready(){
|
|
smc.vmdata.mission_data.hero_num=0
|
|
}
|
|
// protected update(dt: number): void {
|
|
|
|
// }
|
|
|
|
private zhao_huan(event: string, args: any){
|
|
|
|
}
|
|
|
|
private async call_hero(event: string, args: any){
|
|
const uuid = Number(args?.uuid ?? 1001);
|
|
const hero_lv = Math.max(1, Number(args?.hero_lv ?? 1));
|
|
const aliveHeroes = this.getAliveHeroes();
|
|
const mergeHeroes = this.pickMergeHeroes(aliveHeroes, uuid, hero_lv);
|
|
if (mergeHeroes.length === 2) {
|
|
if (this.is_merging) return;
|
|
this.is_merging = true;
|
|
try {
|
|
let sumAp = 0;
|
|
let sumHpMax = 0;
|
|
for (let i = 0; i < mergeHeroes.length; i++) {
|
|
const model = mergeHeroes[i].get(HeroAttrsComp);
|
|
if (!model) continue;
|
|
sumAp += model.ap;
|
|
sumHpMax += model.hp_max;
|
|
}
|
|
let hero_pos = 0;
|
|
const landingPos:Vec3 = HeroPos[hero_pos].pos;
|
|
const spawnPos:Vec3 = v3(landingPos.x, landingPos.y + MissionHeroCompComp.HERO_DROP_HEIGHT, 0);
|
|
await this.mergeDestroyAtBirth(mergeHeroes, spawnPos);
|
|
this.addMergedHero(uuid, hero_lv + 1, sumAp, sumHpMax);
|
|
} finally {
|
|
this.is_merging = false;
|
|
}
|
|
return;
|
|
}
|
|
this.addHero(uuid, hero_lv);
|
|
}
|
|
/** 添加英雄 */
|
|
private addHero(uuid:number=1001,hero_lv:number=1) {
|
|
console.log("addHero uuid:",uuid)
|
|
let hero_pos=0
|
|
let hero = ecs.getEntity<Hero>(Hero);
|
|
let scale = 1
|
|
let landingPos:Vec3 = HeroPos[hero_pos].pos;
|
|
let spawnPos:Vec3 = v3(landingPos.x, landingPos.y + MissionHeroCompComp.HERO_DROP_HEIGHT, 0);
|
|
hero.load(spawnPos,scale,uuid,landingPos.y,hero_lv);
|
|
return hero;
|
|
}
|
|
|
|
private addMergedHero(uuid:number, hero_lv:number, ap:number, hp_max:number) {
|
|
const hero = this.addHero(uuid, hero_lv);
|
|
const model = hero.get(HeroAttrsComp);
|
|
if (!model) return;
|
|
model.ap = Math.max(0, ap);
|
|
model.hp_max = Math.max(1, hp_max);
|
|
model.hp = model.hp_max;
|
|
model.dirty_hp = true;
|
|
}
|
|
|
|
private getAliveHeroes(): Hero[] {
|
|
const heroes: Hero[] = [];
|
|
ecs.query(ecs.allOf(HeroAttrsComp)).forEach((entity: ecs.Entity) => {
|
|
const model = entity.get(HeroAttrsComp);
|
|
if (!model) return;
|
|
if (model.fac !== FacSet.HERO) return;
|
|
if (model.is_dead) return;
|
|
heroes.push(entity as Hero);
|
|
});
|
|
return heroes;
|
|
}
|
|
|
|
private pickMergeHeroes(aliveHeroes: Hero[], uuid: number, hero_lv: number): Hero[] {
|
|
const mergeHeroes: Hero[] = [];
|
|
for (let i = 0; i < aliveHeroes.length; i++) {
|
|
const model = aliveHeroes[i].get(HeroAttrsComp);
|
|
if (!model) continue;
|
|
if (model.hero_uuid !== uuid) continue;
|
|
if (model.lv !== hero_lv) continue;
|
|
mergeHeroes.push(aliveHeroes[i]);
|
|
if (mergeHeroes.length === 2) break;
|
|
}
|
|
return mergeHeroes;
|
|
}
|
|
|
|
private mergeDestroyAtBirth(mergeHeroes: Hero[], spawnPos: Vec3): Promise<void> {
|
|
return new Promise((resolve) => {
|
|
let doneCount = 0;
|
|
const total = mergeHeroes.length;
|
|
if (total <= 0) {
|
|
resolve();
|
|
return;
|
|
}
|
|
const onDone = () => {
|
|
doneCount += 1;
|
|
if (doneCount >= total) {
|
|
resolve();
|
|
}
|
|
};
|
|
for (let i = 0; i < mergeHeroes.length; i++) {
|
|
mergeHeroes[i].mergeToBirthAndDestroy(spawnPos, onDone);
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 视图对象通过 ecs.Entity.remove(ModuleViewComp) 删除组件是触发组件处理自定义释放逻辑 */
|
|
reset() {
|
|
// this.node.destroy();
|
|
}
|
|
}
|