Files
heros/assets/script/game/map/CardComp.ts
2025-05-14 10:11:51 +08:00

94 lines
3.9 KiB
TypeScript

import { _decorator, Animation, Label, resources, SpriteAtlas,Sprite } 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 { GameEvent } from "../common/config/GameEvent";
import { HeroInfo, HeroList } from "../common/config/heroSet";
import { RandomManager } from "db://oops-framework/core/common/random/RandomManager";
import { oops } from "db://oops-framework/core/Oops";
import { HeroModelComp } from "../hero/HeroModelComp";
import { HeroViewComp } from "../hero/HeroViewComp";
const { ccclass, property } = _decorator;
/** 视图层对象 */
@ccclass('CardCompp')
@ecs.register('Card', false)
export class CardComp extends CCComp {
c_uuid:number=0;
c_type:number=0;
is_used:boolean=false;
onLoad(){
this.on(GameEvent.CardRefresh,this.onCardRefresh,this)
}
start() {
this.init_card()
}
init_card(){
this.is_used=true
this.node.getChildByName("Button").active=false
this.node.getChildByName("show").active=false
}
onCardRefresh(event: string, args: any){
this.is_used=false
let hero_list =HeroList
let x=RandomManager.instance.getRandomInt(0,hero_list.length,1)
this.c_uuid=hero_list[x]
console.log("onCardRefresh c_uuid:"+this.c_uuid)
this.update_card(this.c_uuid)
this.node.getChildByName("show").active=false
this.node.getChildByName("anim").getChildByName("up").getComponent(Animation).play('carsup')
this.scheduleOnce(() => {
this.node.getChildByName("show").active=true
this.node.getChildByName("Button").active=true
}, 0.1);
}
update_card(uuid:number){
let show=this.node.getChildByName("show")
show.getChildByName("name").getComponent(Label).string=HeroInfo[uuid].name
show.getChildByName("ap").getChildByName("num").getComponent(Label).string=HeroInfo[uuid].ap.toString()
show.getChildByName("hp").getChildByName("num").getComponent(Label).string=HeroInfo[uuid].hp.toString()
show.getChildByName("type").getChildByName("war").active=HeroInfo[uuid].type==0
show.getChildByName("type").getChildByName("bow").active=HeroInfo[uuid].type==1
show.getChildByName("type").getChildByName("mag").active=HeroInfo[uuid].type==2
show.getChildByName("lv").getChildByName("num").getComponent(Label).string=HeroInfo[uuid].lv.toString()
var icon_path = "game/heros/herois"
resources.load(icon_path, SpriteAtlas, (err: any, atlas) => {
const sprite = show.getChildByName("mask").getChildByName("icon").getComponent(Sprite);
sprite.spriteFrame = atlas.getSpriteFrame(HeroInfo[uuid].path);
});
this.node.getChildByName("Button").getChildByName("Label").getComponent(Label).string=this.check_heros()
}
check_heros(){
let heros=ecs.query(ecs.allOf(HeroModelComp))
for(let hero of heros){
if(hero.get(HeroViewComp).hero_uuid == this.c_uuid){
return "升级"
}
if(hero.get(HeroViewComp).type==HeroInfo[this.c_uuid].type){
return "替换"
}
}
return "召唤"
}
use_card(){
if(this.is_used) return
switch(this.c_type){
case 0:
oops.message.dispatchEvent(GameEvent.UseHeroCard,{uuid:this.c_uuid})
break
case 1:
oops.message.dispatchEvent(GameEvent.UseSkillCard,{uuid:this.c_uuid})
break
case 2:
oops.message.dispatchEvent(GameEvent.UseCard,{uuid:this.c_uuid})
break
}
this.node.getChildByName("show").active=false
this.is_used=true
this.node.getChildByName("Button").active=false
}
reset() {
this.node.destroy();
}
}