cb/assets/career/script/CareerItem.ts
2026-01-28 11:57:05 +08:00

241 lines
11 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.

import RoundBox from "../../Script/module/Tool/RoundBox";
import NumberToImage from "../../Script/NumberToImage";
const { ccclass, property } = cc._decorator;
@ccclass
export default class CareerItem extends cc.Component {
/**数据 */
public data: any = null;
/**索引 0表示第一项*/
public itemIndex: number = 0;
@property(cc.SpriteFrame)
defaultsprite: cc.SpriteFrame = null;
@property(cc.SpriteAtlas)
UI: cc.SpriteAtlas = null;
@property(cc.SpriteAtlas)
UI2: cc.SpriteAtlas = null;
randerChildren: any;
@property(cc.Prefab)
iconPrefab: cc.Prefab = null;
/**数据改变时调用 */
public dataChanged() {
this.randerChildren = [];
for (let i = 4; i < 12; i++) {
this.node.children[i].getChildByName("name").getComponent(cc.Label).string = "";
this.node.children[i].getChildByName("level").getComponent(cc.Label).string = "";
this.node.children[i].getChildByName("mask").getChildByName("icon").getComponent(cc.Sprite).spriteFrame = null;
this.node.children[i].getChildByName("kong").active = true;
}
if (this.data != undefined) {
this.node.opacity = 255;
this.node.getChildByName("rank").getComponent(cc.Label).string = this.data.count + "";
this.node.getChildByName("cityName").getComponent(cc.Sprite).spriteFrame = this.UI2.getSpriteFrame(this.data.name);
this.node.getChildByName("ranking").getChildByName("one").active = false;
this.node.getChildByName("ranking").getChildByName("two").active = false;
this.node.getChildByName("ranking").getChildByName("three").active = false;
this.node.getChildByName("ranking").getChildByName("nomal").active = false;
if (this.data.rank == 1) {
this.node.getChildByName("ranking").getChildByName("one").active = true;
}
else if (this.data.rank == 2) {
this.node.getChildByName("ranking").getChildByName("two").active = true;
}
else if (this.data.rank == 3) {
this.node.getChildByName("ranking").getChildByName("three").active = true;
}
else {
this.node.getChildByName("ranking").getChildByName("nomal").active = true;
NumberToImage.numberToImageNodes4(this.data.rank, 43, 15, "company_", this.node.getChildByName("ranking").getChildByName("nomal").getChildByName("rank"), true);
let rank = this.node.getChildByName("ranking").getChildByName("nomal").getChildByName("rank");
if (rank && rank.children) {
if (rank.children.length > 0) {
for (let i = 0; i < rank.children.length; i++) {
rank.children[i].color = cc.Color.WHITE;
}
}
}
}
this.node.zIndex = this.data.rank;
if (this.randerChildren) {
if (this.randerChildren.length == 0) {
this.randerChildren = [];
let lengthTemp = 0;
if (this.data.rankingData) {
if (this.data.rankingData.length) {
lengthTemp = this.data.rankingData.length;
}
}
let length = lengthTemp + 4;
if (length > 12) length = 12;
for (let i = 4; i < length; i++) {
this.randerChildren.push(this.node.children[i]);
let username = cc.fx.GameTool.subName(this.data.rankingData[i - 4].username, 5);
if (username == "user") username = "匿名玩家";
// console.log("用户名字:", username);
this.node.children[i].getChildByName("kong").active = false;
this.node.children[i].getChildByName("name").getComponent(cc.Label).string = username + "";
this.node.children[i].getChildByName("level").getComponent(cc.Label).string = this.data.rankingData[i - 4].addLevel;
if (this.data.rankingData[i - 4].useravatar == "" || this.data.rankingData[i - 4].useravatar == null || this.data.rankingData[i - 4].useravatar == undefined
) {
// this.firstRender.children[i].getChildByName("icon").getComponent(cc.Sprite).spriteFrame = this.defaultsprite;
}
else if (this.data.rankingData[i - 4].useravatar == "0" || this.data.rankingData[i - 4].useravatar == "1" || this.data.rankingData[i - 4].useravatar == "2"
|| this.data.rankingData[i - 4].useravatar == "3" || this.data.rankingData[i - 4].useravatar == "4" || this.data.rankingData[i - 4].useravatar == "5" || this.data.rankingData[i - 4].useravatar == "6"
|| this.data.rankingData[i - 4].useravatar == "7" || this.data.rankingData[i - 4].useravatar == "8" || this.data.rankingData[i - 4].useravatar == "9" ||
this.data.rankingData[i - 4].useravatar == "10" || this.data.rankingData[i - 4].useravatar == "11" || this.data.rankingData[i - 4].useravatar == "12" || this.data.rankingData[i - 4].useravatar == "13" || this.data.rankingData[i - 4].useravatar == "14"
|| this.data.rankingData[i - 4].useravatar == "15" || this.data.rankingData[i - 4].useravatar == "16" || this.data.rankingData[i - 4].useravatar == "17"
) {
this.node.children[i].getChildByName("mask").getChildByName("icon").active = false;
this.node.children[i].getChildByName("mask").getChildByName("sp").active = true;
let useravatar = this.data.rankingData[i - 4].useravatar;
if (this.node.children[i]) {
//this.firstRender.children[i].getChildByName("mask").getChildByName("icon").getComponent(cc.Sprite).spriteFrame = this.UI.getSpriteFrame(useravatarTemp);
let actionName = this.getActionName(useravatar);
this.node.children[i].getChildByName("mask").getChildByName("sp").scale = 0.85;
this.loadSpineSimple(this.node.children[i].getChildByName("mask").getChildByName("sp"), actionName);
// this.node.children[i].getChildByName("mask").getChildByName("icon").width = 150;
// this.node.children[i].getChildByName("mask").getChildByName("icon").height = 150;
}
// let useravatar = this.data.rankingData[i - 4].useravatar;
// let useravatarTemp = "icon_" + useravatar;
// this.node.children[i].getChildByName("mask").getChildByName("icon").getComponent(cc.Sprite).spriteFrame = this.UI.getSpriteFrame(useravatarTemp);
}
else {
this.node.children[i].getChildByName("mask").getChildByName("icon").active = true;
this.node.children[i].getChildByName("mask").getChildByName("sp").active = false;
this.setPic(this.data.rankingData[i - 4].useravatar, this.node.children[i].getChildByName("mask").getChildByName("icon"));
}
}
}
}
}
else {
this.node.opacity = 0;
}
}
getActionName(useravatar: string) {
let actionName = "action01";
switch (useravatar) {
case "0":
actionName = "action03";
break;
case "1":
actionName = "action01";
break;
case "2":
actionName = "action09";
break;
case "3":
actionName = "action07";
break;
case "4":
actionName = "action04";
break;
case "5":
actionName = "action04";
break;
case "6":
actionName = "action05";
break;
case "7":
actionName = "action06";
break;
case "8":
actionName = "action02";
break;
case "9":
actionName = "action11";
break;
case "10":
actionName = "action06";
break;
case "11":
actionName = "action15";
break;
case "12":
actionName = "action13";
break;
case "13":
actionName = "action16";
break;
case "14":
actionName = "action06";
break;
case "15":
actionName = "action14";
break;
case "16":
actionName = "action18";
break;
}
return actionName;
}
loadSpineSimple(iconNode: cc.Node, spineName: string) {
// 构建spine资源路径
const spinePath = `spine/${spineName}/skeleton`;
// 加载spine资源
cc.resources.load(spinePath, sp.SkeletonData, (err: Error, skeletonData: sp.SkeletonData) => {
if (err) {
console.error(`加载spine资源失败: ${spinePath}`, err);
return;
}
// 检查节点是否已经有spine组件
let spineComponent = iconNode.getComponent(sp.Skeleton);
if (!spineComponent) {
// 如果没有spine组件添加一个
spineComponent = iconNode.addComponent(sp.Skeleton);
}
// 设置skeleton数据
spineComponent.skeletonData = skeletonData;
let actionNumber = "";
if (spineName.startsWith("action")) {
actionNumber = spineName.substring(6); // 提取"action"后面的部分
}
spineComponent.setAnimation(0, actionNumber, true);
// 设置spine组件属性
spineComponent.premultipliedAlpha = true;
spineComponent.timeScale = 1.0;
});
}
public setPic(url, node) {
// this.node.getChildByName("pic").getChildByName("icon").active = false;
// this.node.getChildByName("pic").active = false;
var self = this;
// let url = this.data.useravatar;
cc.assetManager.loadRemote(url, { ext: '.png' }, (err, texture: cc.Texture2D) => {
if (texture && node) {
// this.node.getChildByName("pic").active = true;
var sprite = node.getComponent(cc.Sprite);
sprite.spriteFrame = new cc.SpriteFrame(texture);
}
else {
// console.log("加载图片失败" + url);
}
})
}
}