import { _decorator, Component, Label, Node, Sprite } from 'cc'; import { SpriteLoad } from 'db://assets/mx/components/SpriteLoad'; import { GBundle } from '../game/ConfigRes'; const { ccclass } = _decorator; const LOG = '[GemGrid]'; /** * 宝石格子:仓库 / 获得弹窗共用。 * bg=宝石框小{quality};icon=Weapon.icon;lbNum=数量。 */ @ccclass('GemGrid') export class GemGrid extends Component { /** 品质框 */ bg: Node = null!; /** 宝石图标 */ icon: Node = null!; /** 数量 */ lbNum: Label = null!; /** 当前宝石配表 */ data: ITableWeapon | null = null; protected onLoad(): void { this._bindNodes(); } /** * 绑定展示数据。 * @param data Weapon 行;null 表示空 * @param count 数量;传负数隐藏数量文案 */ setData(data: ITableWeapon | null, count = 0): void { this._bindNodes(); this.data = data; const quality = data ? Math.floor(Number(data.quality) || 0) : 0; if (quality < 0 || quality > 5) console.warn(LOG, '品质越界', data?.id, quality); this._setSprite(this.bg, `weapon/宝石框小${Math.max(0, Math.min(5, quality))}`); const iconName = data ? String(data.icon || '').trim() : ''; if (data && !iconName) console.warn(LOG, 'icon 缺失', data.id); this._setSprite(this.icon, iconName ? `weapon/${iconName}` : ''); if (this.icon?.isValid) this.icon.active = !!iconName; if (this.lbNum?.isValid) { const showNum = count >= 0; this.lbNum.node.active = showNum; if (showNum) this.lbNum.string = `${Math.max(0, count | 0)}`; } } /** 绑定子节点 */ private _bindNodes(): void { if (!this.bg?.isValid) this.bg = this.node.getChildByName('bg')!; if (!this.icon?.isValid) this.icon = this.node.getChildByName('icon')!; if (!this.lbNum?.isValid) { const n = this.node.getChildByName('lbNum'); this.lbNum = n?.getComponent(Label) ?? null!; } } /** icons/weapon 下图 */ private _setSprite(node: Node | null, path: string): void { if (!node?.isValid) return; if (!path) { node.getComponent(SpriteLoad)?.clear(); const sp = node.getComponent(Sprite); if (sp) sp.spriteFrame = null; return; } if (!node.getComponent(Sprite)) node.addComponent(Sprite); node.getOrAddComponent(SpriteLoad).setSprite(path, GBundle.Items); } }