You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
AdLike2048/assets/script/ui/popHuishou/UIpopHuishou.ts

145 lines
4.7 KiB

3 weeks ago
//*********************
// create by 流云
// time: Thu Aug 13 2026
// desc:
//*********************
import { _decorator, Label, Node } from 'cc';
import { Auto_popHuishou } from './Auto_popHuishou';
const { ccclass } = _decorator;
/** 预制体节点仍名 white/green/blue,文案为绿/蓝/紫,品质 1/2/3(约定 0 白…4 橙) */
const QUALITY_BTNS: { name: string; quality: number }[] = [
{ name: 'huishoubai_white', quality: 1 },
{ name: 'huishoubai_green', quality: 2 },
{ name: 'huishoubai_blue', quality: 3 },
];
/**
* 回收武器界面
* ui_layout/huishoubai_*:勾选绿(1)/蓝(2)/紫(3)未穿戴成品与武器碎片
* lb_num0~2:已选件数;lb_money:预计回收金币(成品 Weapon.price,碎片 Item.price)
*/
@ccclass('UIpopHuishou')
export class UIpopHuishou extends Auto_popHuishou {
/** 品质按钮节点(与 QUALITY_BTNS 下标对齐) */
private _qualityBtnNodes: Node[] = [];
/** 已勾选品质 */
private _selected: number[] = [];
/** 预览金币 */
private _money = 0;
/** 出售中,防连点 */
private _selling = false;
onLoad(): void {
super.onLoad();
this._qualityBtnNodes = [];
for (let i = 0; i < QUALITY_BTNS.length; i++) {
const row = QUALITY_BTNS[i];
const n = this.ui_layout?.getChildByName(row.name) ?? null;
if (n) n['recycleQuality'] = row.quality;
this._qualityBtnNodes.push(n);
}
}
onEnable(): void {
super.onEnable();
for (let i = 0; i < this._qualityBtnNodes.length; i++) {
this._qualityBtnNodes[i]?.on(Node.EventType.TOUCH_END, this._onQualityTouch, this);
}
}
onDisable(): void {
for (let i = 0; i < this._qualityBtnNodes.length; i++) {
this._qualityBtnNodes[i]?.off(Node.EventType.TOUCH_END, this._onQualityTouch, this);
}
this._selling = false;
super.onDisable();
}
onInit(): void {
this._selling = false;
this._selected.length = 0;
this._syncQualityCheck();
this._refreshPreview();
}
click_btn_close(): void {
this.close(2);
}
click_btn_sell(): void {
if (this._selling) return;
if (this._selected.length <= 0 || this._money <= 0) {
gg.ui.showToast('请选择要回收的武器或碎片');
return;
}
this._selling = true;
const { money } = gg.equip.recycleFreeEquips(this._selected);
if (money <= 0) {
this._selling = false;
gg.ui.showToast('没有可回收的武器或碎片');
this._refreshPreview();
return;
}
this.close(2);
}
private _onQualityTouch(e: { currentTarget?: Node }): void {
const q = Number(e.currentTarget?.['recycleQuality']);
if (q !== 1 && q !== 2 && q !== 3) {
console.log('[popHuishou] 回收品质无效', q);
return;
}
this._toggleQuality(q);
}
private _toggleQuality(quality: number): void {
const idx = this._selected.indexOf(quality);
if (idx >= 0) {
this._selected.splice(idx, 1);
} else {
this._selected.push(quality);
}
this._syncQualityCheck();
this._refreshPreview();
}
private _syncQualityCheck(): void {
for (let i = 0; i < QUALITY_BTNS.length; i++) {
const n = this._qualityBtnNodes[i];
if (!n) continue;
const gou = n.getChildByName('gouxuan');
if (gou) gou.active = this._selected.indexOf(QUALITY_BTNS[i].quality) >= 0;
}
}
private _refreshPreview(): void {
const preview = gg.equip.getRecyclePreview(this._selected);
this._money = preview.money;
this._setSelectedNum(this.lb_num0, 1);
this._setSelectedNum(this.lb_num1, 2);
this._setSelectedNum(this.lb_num2, 3);
if (this.lb_money) this.lb_money.string = String(this._money);
}
/** 刷新某品质已选件数文案 */
private _setSelectedNum(lb: Label | null, quality: number): void {
if (!lb) return;
const selected = this._selected.indexOf(quality) >= 0;
const count = selected ? this._countRecycleByQuality(quality) : 0;
lb.string = `已选${count}件`;
}
/** 统计某品质可回收件数(未勾选时为 0,由调用方控制) */
private _countRecycleByQuality(quality: number): number {
const preview = gg.equip.getRecyclePreview([quality]);
let count = 0;
for (let i = 0; i < preview.list.length; i++) {
count += Math.max(0, preview.list[i].count);
}
return count;
}
}