Files
heros/assets/script/game/hero/Hero.ts
2025-10-16 16:53:34 +08:00

102 lines
3.6 KiB
TypeScript

import { instantiate, Node, Prefab, Vec3 ,v3,resources,SpriteFrame,Sprite,SpriteAtlas} from "cc";
import { oops } from "../../../../extensions/oops-plugin-framework/assets/core/Oops";
import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
import { smc } from "../common/SingletonModuleComp";
import { HeroModelComp } from "./HeroModelComp";
import { HeroViewComp } from "./HeroViewComp";
import { BoxSet, FacSet } from "../common/config/BoxSet";
import { HeroInfo, HeroPos, HType } from "../common/config/heroSet";
import { BattleMoveComp } from "../common/ecs/position/BattleMoveComp";
import { GameEvent } from "../common/config/GameEvent";
import { BuffAttr, getBuffNum, SkillSet, SType } from "../common/config/SkillSet";
/** 角色实体 */
@ecs.register(`Hero`)
export class Hero extends ecs.Entity {
HeroModel!: HeroModelComp;
HeroView!: HeroViewComp;
BattleMove!: BattleMoveComp;
protected init() {
this.addComponents<ecs.Comp>(
BattleMoveComp,
HeroModelComp,
);
}
destroy(): void {
this.remove(HeroViewComp);
this.remove(HeroModelComp);
super.destroy();
}
/** 加载角色 */
load(pos: Vec3 = Vec3.ZERO,scale:number = 1,uuid:number=1001,fight_pos:number=1) {
// console.log("英雄加载:",uuid,pos,scale,info)
scale = 1
// 查找空闲英雄槽位
var path = "game/heros/"+HeroInfo[uuid].path;
var prefab: Prefab = oops.res.get(path, Prefab)!;
var node = instantiate(prefab);
var scene = smc.map.MapView.scene;
node.parent = scene.entityLayer!.node!
node.setPosition(pos)
// console.log("hero load",pos)
var hv = this.hero_init(uuid,node)
this.add(hv);
oops.message.dispatchEvent(GameEvent.MasterCalled,{uuid:uuid})
const move = this.get(BattleMoveComp);
move.direction = 1; // 向右移动
move.targetX = 0; // 右边界'
if(HeroInfo[uuid].type==HType.remote){
move.targetX = -100; // 右边界'
}
if(HeroInfo[uuid].type==HType.mage){
move.targetX = -200; // 右边界'
}
smc.vmdata.mission_data.hero_num++
}
hero_init(uuid:number=1001,node:Node) {
var hv = node.getComponent(HeroViewComp)!;
let hero= HeroInfo[uuid] // 共用英雄数据
hv.scale = 1;
hv.is_master=true;
hv.lv=HeroInfo[uuid].lv?HeroInfo[uuid].lv:1
hv.fac = FacSet.HERO;
hv.type = hero.type;
hv.box_group = BoxSet.HERO;
hv.hero_uuid= uuid;
hv.hero_name= hero.name;
hv.hp=hv.hp_max=hv.hp_base=HeroInfo[uuid].hp;
hv.mp=hv.mp_max=hv.mp_base=HeroInfo[uuid].mp;
hv.Attrs=getBuffNum()
hv.Attrs[BuffAttr.SPEED]=hv.speed = hero.speed;
hv.Attrs[BuffAttr.DIS]=hv.dis=hero.dis;
hv.Attrs[BuffAttr.ATK_CD]=hv.cd=hero.cd
hv.Attrs[BuffAttr.AP]=hv.ap=HeroInfo[uuid].ap;
hv.Attrs[BuffAttr.DEF]=HeroInfo[uuid].def;
hero.buff.forEach((buff:any)=>{
hv.apply_buff(buff.type,buff.value)
})
for(let i=0;i<hero.skills.length;i++){
if(SkillSet[hero.skills[i]].SType==SType.buff){
let buffs=SkillSet[hero.skills[i]].buffs
buffs.forEach((buff:any)=>{
hv.apply_buff(buff.buff, buff.buV)
})
continue
}else{
hv.skills.push({
cd:0,
uuid:hero.skills[i],
cd_max:i==0?hero.cd:SkillSet[hero.skills[i]].cd
})
}
}
return hv
}
}