MatchMaster/assets/Script/WinCat.ts
2026-07-30 12:22:02 +08:00

63 lines
2.2 KiB
TypeScript
Raw Permalink 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.

// Learn TypeScript:
// - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
// - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html
const { ccclass, property } = cc._decorator;
@ccclass
export default class WinCat extends cc.Component {
@property(cc.SpriteAtlas)
catUI: cc.SpriteAtlas = null;
start() {
this.refreshUserCat();
}
/**
* 结算页的两只猫都使用当前用户选择的 useravatarCat。
* useravatarCat 保存格式为 cat_1而 catUI 图集中的帧名为 cat1。
*/
public refreshUserCat() {
if (!this.catUI) {
cc.warn("WinCat: catUI 图集未绑定");
return;
}
const useravatarCat = cc.fx.GameConfig.GM_INFO.useravatarCat;
const catNumber = String(useravatarCat || "").match(/\d+/);
const frameName = catNumber ? "cat" + parseInt(catNumber[0], 10) : "cat1";
const spriteFrame = this.catUI.getSpriteFrame(frameName)
|| this.catUI.getSpriteFrame("cat1");
if (!spriteFrame) {
cc.warn("WinCat: catUI 中找不到猫图片", frameName);
return;
}
// Win/Career/bg2/cat
const careerBg = this.node.getChildByName("bg2");
const careerCat = careerBg && careerBg.getChildByName("cat");
this.setCatSprite(careerCat, spriteFrame, "Career/bg2/cat");
// Win/tween/Ruzhi/bg2Map.gameOverNode 下的入职结算猫)
const winNode = this.node.parent;
const tweenNode = winNode && winNode.getChildByName("tween");
const ruzhiNode = tweenNode && tweenNode.getChildByName("Ruzhi");
const ruzhiCat = ruzhiNode && ruzhiNode.getChildByName("bg2");
this.setCatSprite(ruzhiCat, spriteFrame, "tween/Ruzhi/bg2");
}
private setCatSprite(node: cc.Node, spriteFrame: cc.SpriteFrame, nodePath: string) {
const sprite = node && node.getComponent(cc.Sprite);
if (!sprite) {
cc.warn("WinCat: 找不到猫节点或 Sprite", nodePath);
return;
}
sprite.spriteFrame = spriteFrame;
}
}