Files
heros/assets/script/game/map/GoodsComp.ts
2025-08-20 17:46:06 +08:00

74 lines
2.6 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 { _decorator, Component, Node, Label, Sprite, SpriteFrame, resources } from 'cc';
import { Goods, GType, CType } from '../common/config/Goods';
import { Items } from '../common/config/Items';
import { NumberFormatter } from '../common/config/BoxSet';
const { ccclass, property } = _decorator;
@ccclass('GoodsComp')
export class GoodsComp extends Component {
// 数据(仅用于更新显示)
private goodsData: any = null;
private itemData: any = null;
private currentUuid: number = 0;
/**
* 更新物品数据
* @param uuid 物品UUID
*/
update_data(uuid: number) {
this.currentUuid = uuid;
this.goodsData = Goods[uuid];
if (!this.goodsData) {
console.error(`Goods data not found for uuid: ${uuid}`);
return;
}
this.itemData = Items[this.goodsData.i_uuid];
if (!this.itemData) {
console.error(`Item data not found for i_uuid: ${this.goodsData.i_uuid}`);
return;
}
this.updateIcon();
this.updateTexts();
this.update_btn(this.goodsData.c_type)
}
update_btn(type:CType){
this.node.getChildByName("ad").active=type==CType.AD
this.node.getChildByName("free").active=type==CType.FREE
this.node.getChildByName("cast").active=type==(CType.DIAMOND||CType.GOLD)
this.node.getChildByName("cast").getChildByName("diamond").active=type==CType.DIAMOND
this.node.getChildByName("cast").getChildByName("gold").active=type==CType.GOLD
this.node.getChildByName("cast").getChildByName("num").getComponent(Label).string=NumberFormatter.formatNumber(this.goodsData.cast)
}
/**
* 更新图标
*/
private updateIcon() {
const iconSprite = this.node.getChildByName("icon")?.getComponent(Sprite);
if (!iconSprite) return;
const path = `gui/items/${this.itemData.path}`;
resources.load(path, SpriteFrame, (err, spriteFrame) => {
if (err) {
console.warn(`icon load failed: ${path}`, err);
return;
}
iconSprite.spriteFrame = spriteFrame;
});
}
/** 仅更新文字(名称与数量) */
private updateTexts() {
// 名称
const nameLabel = this.node.getChildByName("name")?.getComponent(Label);
if (nameLabel) nameLabel.string = this.itemData.name;
// 数量(根节点下的 num
const mainNumLabel = this.node.getChildByName("num")?.getComponent(Label);
if (mainNumLabel) mainNumLabel.string = NumberFormatter.formatNumber(this.goodsData.num);
}
}