Files
heros/assets/script/game/monster/Monster.ts
2024-08-08 10:44:58 +08:00

87 lines
3.3 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.

/*
* @Author: dgflash
* @Date: 2021-11-18 17:47:56
* @LastEditors: dgflash
* @LastEditTime: 2022-08-04 15:43:04
*/
import { instantiate, Node, Prefab, Vec3 ,tween, v3,Collider2D,Contact2DType,PhysicsSystem2D,IPhysics2DContact, animation,Label,resources,SpriteFrame,Sprite} from "cc";
import { UICallbacks } from "../../../../extensions/oops-plugin-framework/assets/core/gui/layer/Defines";
import { oops } from "../../../../extensions/oops-plugin-framework/assets/core/Oops";
import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
import { UIID } from "../common/config/GameUIConfig";
import { smc } from "../common/SingletonModuleComp";
import { MonsterModelComp } from "./MonsterModelComp";
import { MonsterSpine } from "./MonsterSpine";
import { MonsterViewComp } from "./MonsterViewComp";
import { MoveToComp } from "../common/MoveTo";
/** 角色实体 */
@ecs.register(`Monster`)
export class Monster extends ecs.Entity {
// 数据层
MonsterModel!: MonsterModelComp;
// 视图层
MonsterView!: MonsterViewComp;
RoleMoveTo!: MoveToComp; // 移动
protected init() {
this.addComponents<ecs.Comp>(
MonsterModelComp);
}
destroy(): void {
this.remove(MonsterViewComp);
super.destroy();
}
/** 加载角色 */
load(pos: Vec3 = Vec3.ZERO,speed:number = 100,camp:number = 1,prefab_path:string = "monster",name:string="hero") {
// var path = "game/monster/"+prefab_path;
var path = "game/monster/hero";
var prefab: Prefab = oops.res.get(path, Prefab)!;
var node = instantiate(prefab);
var scene = smc.map.MapView.scene;
node.parent = scene.entityLayer!.node!;
var as = node.getComponent(MonsterSpine);
node.getChildByName("avatar").setScale(node.getChildByName("avatar").scale.x*camp, node.getChildByName("avatar").scale.y, node.getChildByName("avatar").scale.z);
node.setPosition(pos)
// console.log(node.getChildByName("avatar").getChildByName("TNode").getChildByName("bb").getComponent(Sprite))
const url = 'game/heros/hero/'+prefab_path+'/spriteFrame';
resources.load(url, SpriteFrame, (err: any, spriteFrame) => {
const sprite = node.getChildByName("avatar").getChildByName("TNode").getChildByName("bb").getComponent(Sprite);
sprite.spriteFrame = spriteFrame;
});
var mv = node.getComponent(MonsterViewComp)!;
mv.speed =mv.ospeed = speed;
mv.hero_name= name;
mv.camp = camp;
mv.Tpos = v3(0,0,0);
mv.change_name(name,camp)
this.add(mv);
if(camp == 1){
smc.heros_in.push({name:mv.ent.name,eid:mv.ent.eid,pos_x:0})
oops.message.dispatchEvent("hero_load",this)
}else{
smc.monsters_in.push({name:mv.ent.name,eid:mv.ent.eid,pos_x:0})
oops.message.dispatchEvent("monster_load",this)
}
console.log(smc.heros_in,smc.monsters_in)
}
/** 移动ECS System处理逻辑分享功能独立的业务代码 */
move(target: Vec3,speed:number = 100) {
var move = this.get(MoveToComp) || this.add(MoveToComp);
move.target = target;
move.node = this.MonsterView.node;
move.speed = speed;
}
}