import { _decorator, Component, Node, Prefab, instantiate, Vec3, resources, sp } 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 { SpineComp } from "./SpineComp"; const { ccclass, property } = _decorator; /** 英雄视图组件 */ @ccclass('HeroViewComp') @ecs.register('HeroView', false) export class HeroViewComp extends CCComp { private _node: Node = null!; private _spineComp: SpineComp = null!; /** 加载角色模型 */ async load(spinePath: string) { // 加载Spine预制体 const prefab = await this.loadSpinePrefab(spinePath); // 实例化节点 this._node = instantiate(prefab); this.node.addChild(this._node); // 添加Spine组件 const spine = this._node.getComponent(sp.Skeleton)!; this._spineComp = this.ent.add(SpineComp).init(spine); } private loadSpinePrefab(path: string): Promise { return new Promise((resolve, reject) => { resources.load(path, Prefab, (err, prefab) => { err ? reject(err) : resolve(prefab!); }); }); } /** 更新位置 */ setPosition(pos: Vec3) { this.node.position = pos; } /** 播放动画 */ playAnimation(name: string) { this._spineComp.play(name); } /** 重置组件 */ reset() { this._node.destroy(); this.ent.remove(SpineComp); } }