Files
pixelheros/assets/script/game/map/MissionComp.ts
walkpan 10e287c134 refactor(game): 重构英雄数据结构和添加状态栏更新功能
- 将hero_data重命名为hero并调整属性结构
- 添加hp/mp初始值和最大值
- 新增暴击率(crt)属性
- 增加金币初始值
- 在MissionComp中添加状态栏(hp/mp/exp)更新逻辑
2026-01-02 19:21:41 +08:00

171 lines
5.6 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, Vec3,Animation, instantiate, Prefab, Node, ProgressBar } 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 { smc } from "../common/SingletonModuleComp";
import { oops } from "../../../../extensions/oops-plugin-framework/assets/core/Oops";
import { FightSet} from "../common/config/GameSet";
import { GameEvent } from "../common/config/GameEvent";
import { HeroViewComp } from "../hero/HeroViewComp";
import { UIID } from "../common/config/GameUIConfig";
import { SkillView } from "../skill/SkillView";
const { ccclass, property } = _decorator;
//@todo this is a test
/** 视图层对象 */
@ccclass('MissionComp')
@ecs.register('MissionComp', false)
export class MissionComp extends CCComp {
// VictoryComp:any = null;
// reward:number = 0;
// reward_num:number = 0;
rewards:any[]=[]
info:any=null
hp_bar:any=null
mp_bar:any=null
exp_bar:any=null
game_data:any={
exp:0,
gold:0,
diamond:0
}
onLoad(){
this.on(GameEvent.MissionStart,this.mission_start,this)
this.on(GameEvent.MonDead,this.do_mon_dead,this)
this.on(GameEvent.HeroDead,this.do_hero_dead,this)
this.on(GameEvent.FightEnd,this.fight_end,this)
this.on(GameEvent.MissionEnd,this.mission_end,this)
this.on(GameEvent.DO_AD_BACK,this.do_ad,this)
this.info=this.node.getChildByName("info")
this.hp_bar=this.info.getChildByName("hp_bar").getChildByName("bar")
this.mp_bar=this.info.getChildByName("mp_bar").getChildByName("bar")
this.exp_bar=this.info.getChildByName("exp_bar").getChildByName("bar")
// this.on(GameEvent.CanUpdateLv,this.show_uplv_button,this)
}
protected update(dt: number): void {
if(!smc.mission.play||smc.mission.pause){
return
}
if(smc.mission.in_fight){
smc.vmdata.mission_data.fight_time+=dt
}
this.update_info()
}
update_info(){
this.hp_bar.getComponent(ProgressBar).progress=smc.vmdata.hero.hp/smc.vmdata.hero.hp_max
this.mp_bar.getComponent(ProgressBar).progress=smc.vmdata.hero.mp/smc.vmdata.hero.mp_max
this.exp_bar.getComponent(ProgressBar).progress=smc.vmdata.hero.exp/smc.vmdata.hero.exp_max
}
//奖励发放
do_reward(){
// 奖励发放
}
do_drop(drop_item:any[],game_data:any={exp:0,gold:0,diamond:0}){
// console.log("[MissionComp] do_drop",drop_item,game_data)
}
do_mon_dead(event:any,data:any){
// console.log("[MissionComp] do_mon_dead",event,data)
smc.vmdata.mission_data.mon_num--
}
do_hero_dead(event:any,data:any){
// console.log("[MissionComp] do_hero_dead",event,data)
// smc.vmdata.mission_data.hero_num--
// if(smc.vmdata.mission_data.hero_num<=0) {
// oops.message.dispatchEvent(GameEvent.FightEnd,{victory:false})
// oops.gui.open(UIID.Victory,{victory:false,rewards:this.rewards,game_data:this.game_data})
// }
}
do_ad(){
if(this.ad_back()){
oops.message.dispatchEvent(GameEvent.AD_BACK_TRUE)
smc.vmdata.mission_data.refresh_count+=FightSet.MORE_RC
}else{
oops.message.dispatchEvent(GameEvent.AD_BACK_FALSE)
}
}
ad_back(){
return true
}
async mission_start(){
// console.log("[MissionComp] ** 1 ** mission_start")
oops.message.dispatchEvent(GameEvent.FightReady)
this.node.active=true
this.data_init()
let loading=this.node.parent.getChildByName("loading")
loading.active=true
this.scheduleOnce(()=>{
loading.active=false
},0.5)
this.scheduleOnce(()=>{
this.to_fight()
},0.1)
}
to_fight(){
smc.mission.in_fight=true
oops.message.dispatchEvent(GameEvent.FightStart) //GameSetMonComp 监听刷怪
}
to_end_fight(){
oops.message.dispatchEvent(GameEvent.FightEnd,{victory:false})
oops.gui.open(UIID.Victory,{victory:false,rewards:this.rewards,game_data:this.game_data})
}
fight_end(){
// console.log("任务结束")
// 延迟0.5秒后执行任务结束逻辑
this.scheduleOnce(() => {
smc.mission.play=false
smc.mission.pause=false
this.cleanComponents()
}, 0.5)
}
mission_end(){
// console.log("[MissionComp] mission_end")
this.node.active=false
}
data_init(){
//局内数据初始化 smc 数据初始化
smc.mission.play = true;
smc.vmdata.mission_data.in_fight=false
smc.vmdata.mission_data.fight_time=0
smc.vmdata.mission_data.level=0
this.rewards=[] // 改为数组,用于存储掉落物品列表
// console.log("[MissionComp]局内数据初始化",smc.vmdata.mission_data)
}
private cleanComponents() {
// 优化销毁顺序直接销毁实体让ECS系统自动处理组件清理
// 这样可以避免在组件reset方法中访问已经被销毁的实体引用
ecs.query(ecs.allOf(HeroViewComp)).forEach(entity => {
entity.destroy();
});
ecs.query(ecs.allOf(SkillView)).forEach(entity => {
entity.destroy();
});
}
/** 视图层逻辑代码分离演示 */
/** 视图对象通过 ecs.Entity.remove(ModuleViewComp) 删除组件是触发组件处理自定义释放逻辑 */
reset() {
this.node.destroy();
}
}