Files
pixelheros/assets/script/game/map/VictoryComp.ts
walkpan 2d79e9d064 fix(游戏逻辑): 修复任务结束时的UI移除时机并重置英雄数据
调整VictoryComp中UI节点移除的时机,确保在MissionStart事件之后执行
在MissionComp中添加英雄数据和金币的初始化逻辑,确保每局游戏开始时状态重置
2026-01-03 20:20:49 +08:00

161 lines
4.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { _decorator, instantiate, Label ,Prefab,Node} 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 { oops } from "../../../../extensions/oops-plugin-framework/assets/core/Oops";
import { smc } from "../common/SingletonModuleComp";
import { GameEvent } from "../common/config/GameEvent";
import { it } from "node:test";
import { HeroAttrsComp } from "../hero/HeroAttrsComp";
import { HeroViewComp } from "../hero/HeroViewComp";
import { FacSet } from "../common/config/GameSet";
import { Attrs } from "../common/config/HeroAttrs";
const { ccclass, property } = _decorator;
/** 视图层对象 */
@ccclass('VictoryComp')
@ecs.register('Victory', false)
export class VictoryComp extends CCComp {
reward_lv:number=1
reward_num:number=2
rewards:any[]=[]
game_data:any={
exp:0,
gold:0,
diamond:0
}
// 复活相关配置
private canRevive: boolean = false; // 是否可以复活由MissionComp传入
// private reviveCount: number = 0; // 已复活次数 - 移交 MissionComp 管理
/** 视图层逻辑代码分离演示 */
protected onLoad(): void {
// this.canRevive = true;
// this.reviveCount = 0;
}
onAdded(args: any) {
console.log("[VictoryComp] onAdded",args)
if(args.game_data){
this.game_data=args.game_data
}
// 接收复活参数
if (args.can_revive !== undefined) {
this.canRevive = args.can_revive;
} else {
this.canRevive = false; // 默认不可复活
}
this.node.getChildByName("btns").getChildByName("next").active=!args.can_revive
this.node.getChildByName("btns").getChildByName("alive").active=args.can_revive
}
victory_end(){
this.clear_data()
oops.message.dispatchEvent(GameEvent.MissionEnd)
oops.gui.removeByNode(this.node)
}
clear_data(){
smc.mission.pause=false
}
//看广告双倍
watch_ad(){
return true
}
double_reward(){
// console.log("[VictoryComp]double_reward",smc.items,this.rewards)
}
/** 看广告复活 */
watch_ad_revive() {
if (!this.canRevive) {
console.log("已经复活过,无法再次复活");
return;
}
// TODO: 接入广告SDK这里先模拟广告播放成功回调
this.onAdReviveSuccess();
}
/** 广告复活成功回调 */
private onAdReviveSuccess() {
console.log("[VictoryComp] 广告复活成功");
// 1. 标记已复活
// this.reviveCount++;
this.canRevive = false;
// 2. 执行复活逻辑
this.doReviveHero();
// 2.1 通知 MissionComp 扣除次数
oops.message.dispatchEvent(GameEvent.ReviveSuccess);
// 3. 恢复游戏状态
// smc.mission.play = true;
smc.mission.pause = false;
// 4. 关闭结算界面
oops.gui.removeByNode(this.node);
}
/** 执行英雄复活逻辑 */
private doReviveHero() {
// 查找所有英雄实体并复活
const heroes = ecs.query(ecs.allOf(HeroAttrsComp, HeroViewComp));
let hasRevived = false;
heroes.forEach(e => {
const attrs = e.get(HeroAttrsComp);
const view = e.get(HeroViewComp);
if (attrs.fac === FacSet.HERO && attrs.is_dead) {
// 重置属性
attrs.is_dead = false;
attrs.hp = attrs.Attrs[Attrs.HP_MAX]; // 满血复活
attrs.mp = attrs.Attrs[Attrs.MP_MAX];
attrs.is_reviving = false;
// 视图层复活
if (view) {
view.alive();
}
hasRevived = true;
console.log(`[VictoryComp] 复活英雄: ${attrs.hero_name}`);
}
});
if (hasRevived) {
// 发送复活事件(如果有需要)
// oops.message.dispatchEvent(GameEvent.HeroRevived);
} else {
console.warn("[VictoryComp] 未找到可复活的英雄实体");
}
}
restart(){
this.clear_data()
// 确保游戏结束事件被触发,以便重置状态
oops.message.dispatchEvent(GameEvent.MissionEnd)
this.scheduleOnce(()=>{
oops.message.dispatchEvent(GameEvent.MissionStart)
oops.gui.removeByNode(this.node)
},0.5)
}
item_show(e:any,val:any){
// console.log("item_show",val)
}
protected onDestroy(): void {
// console.log("释放胜利界面");
}
/** 视图对象通过 ecs.Entity.remove(ModuleViewComp) 删除组件是触发组件处理自定义释放逻辑 */
reset() {
this.node.destroy()
}
}