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/PopNewWeapon/UIPopNewWeapon.ts

96 lines
2.8 KiB

3 weeks ago
//*********************
// create by 流云
// time: Tue Sep 08 2026
// desc: 获得新武器弹窗
//*********************
import { _decorator, instantiate, Node } from 'cc';
import { Auto_PopNewWeapon } from './Auto_PopNewWeapon';
import { PopNewWeaponItemView } from './PopNewWeaponItemView';
const { ccclass } = _decorator;
const LOG = '[PopNewWeapon]';
@ccclass('UIPopNewWeapon')
export class UIPopNewWeapon extends Auto_PopNewWeapon {
/** 本次弹窗展示的武器 id(关闭后写入已展示存档) */
private _pendingIds: number[] = [];
/** 是否已写入已展示存档 */
private _markedShown = false;
/** 动态实例化的格子节点 */
private _itemNodes: Node[] = [];
onInit(): void {
this._pendingIds = this._resolveEquipIds();
this._renderWeapons(this._pendingIds);
}
onDisable(): void {
this._markShown();
this._clearItems();
super.onDisable();
}
click_btn_close(): void {
void this.close(2);
}
click_bgMask(): void {
void this.close(2);
}
/** 解析打开参数:number[] 或单个 id */
private _resolveEquipIds(): number[] {
const raw = this.prema;
if (Array.isArray(raw)) {
const out: number[] = [];
for (let i = 0; i < raw.length; i++) {
const id = Math.floor(Number(raw[i]) || 0);
if (id > 0 && out.indexOf(id) < 0) out.push(id);
}
out.sort((a, b) => a - b);
return out;
}
const one = Math.floor(Number(raw) || 0);
return one > 0 ? [one] : [];
}
/** 按 ui_itemNode 模板渲染 ui_content */
private _renderWeapons(ids: number[]): void {
const tpl = this.ui_itemNode;
const root = this.ui_content;
if (!tpl?.isValid || !root?.isValid) {
console.warn(LOG, 'ui_itemNode 或 ui_content 缺失');
return;
}
this._clearItems();
tpl.active = false;
if (!ids.length) return;
for (let i = 0; i < ids.length; i++) {
const node = instantiate(tpl);
node.parent = root;
node.active = true;
PopNewWeaponItemView.bind(node, ids[i]);
this._itemNodes.push(node);
}
}
/** 销毁动态格子 */
private _clearItems(): void {
for (let i = 0; i < this._itemNodes.length; i++) {
const node = this._itemNodes[i];
if (node?.isValid) node.destroy();
}
this._itemNodes.length = 0;
}
/** 关闭时记录已展示武器 id */
private _markShown(): void {
if (this._markedShown || !this._pendingIds.length) return;
this._markedShown = true;
gg.equip.markPopNewWeaponsShown(this._pendingIds);
this._pendingIds = [];
}
}