Files
Max-Cocos-Demo/assets/games/scripts/pets/GlarePart.ts
2025-11-26 22:57:07 +08:00

91 lines
2.8 KiB
TypeScript

import { easing, instantiate, Node, Prefab, sp, tween, Tween, Vec3 } from "cc";
import { BasePart } from "./BasePart";
import { DRESS_PART, DRESS_SOURCE_TYPE, IDressInfo } from "./Types";
import ResManager from "@max-studio/core/res/ResManager";
const GLARE_NODE_NAME = "glare";
const GLARE_DEFAULT_POSITION = new Vec3(-170, 0, 0);
export class GlarePart extends BasePart {
private glareNode: Node = null;
private glareSpine: sp.Skeleton = null;
private glarePrefab: Prefab = null;
constructor(ske: sp.Skeleton, part: DRESS_PART) {
super(ske, part);
this.getGlareNode();
}
public async putOn(info: IDressInfo) {
this.takeOff();
this.info = info;
const loadInfo = await ResManager.getInstance().loadAsset({
bundle: "dress-spine",
path: `DressGlare/${info.sourceName}`,
type: Prefab,
});
if (loadInfo.err) {
console.error("加载宠物_槽位_失败", loadInfo.err);
return;
}
this.glarePrefab = loadInfo.asset;
const node = instantiate(loadInfo.asset);
node.name = "sp";
node.setParent(this.glareNode);
if (node == null) {
return;
}
this.glareSpine = node.getComponent(sp.Skeleton);
this.glareSpine.setAnimation(0, "animation", false);
this.glareNode.setScale(this.skeleton.node.scale);
Tween.stopAllByTarget(this.glareNode);
const dir = this.glareNode.scale.x;
GLARE_DEFAULT_POSITION.x = dir > 0 ? -170 : 170;
tween(this.glareNode).to(0.35, { position: GLARE_DEFAULT_POSITION }, { easing: easing.sineOut }).start();
if (this.info.sourceLocationType == DRESS_SOURCE_TYPE.NONE) {
this.glareNode.setSiblingIndex(0);
} else {
this.glareNode.setSiblingIndex(this.glareNode.parent.children.length - 1);
}
}
public takeOff() {
if (this.glareSpine == null) {
return;
}
if (this.glareSpine) {
this.glareSpine.node.destroy();
this.glareSpine = null;
}
if (this.glarePrefab) {
ResManager.getInstance().releaseAsset(this.glarePrefab);
this.glarePrefab = null;
}
if (this.glareNode) {
Tween.stopAllByTarget(this.glareNode);
}
this.info = null;
}
private getGlareNode() {
if (this.glareNode) {
return this.glareNode;
}
const root = this.skeleton.node.parent;
if (!root) {
console.error("没有找到根节点");
return null;
}
this.glareNode = new Node(GLARE_NODE_NAME);
this.glareNode.setParent(root);
this.glareNode.setPosition(GLARE_DEFAULT_POSITION);
this.glareNode.setSiblingIndex(0);
return this.glareNode;
}
}