Files
pixelheros/assets/script/game/component/hero/HeroView.ts
walkpan bffbb9077e dd
2025-02-01 00:14:25 +08:00

52 lines
1.6 KiB
TypeScript

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<Prefab> {
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);
}
}