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/PopGM/UIPopGM.ts

215 lines
7.9 KiB

3 weeks ago
//*********************
// create by 流云
// time: Mon Apr 20 2026
// desc:
//*********************
import { _decorator, Component, Node, find, Color, instantiate, Label, EditBox, RichText } from 'cc';
import { Auto_PopGM } from './Auto_PopGM';
import { TableNames } from '../../game/ConfigTableData';
import { WeaponComposeConfigProvider } from '../../battle/bag/WeaponComposeConfigProvider';
const { ccclass, property } = _decorator;
@ccclass('UIPopGM')
export class UIPopGM extends Auto_PopGM {
/** 武器/技能列表项底色,按品级 quality 1~4 对应;在 PopGM 预制体上配置 */
@property({ type: [Color], displayName: '品级底色(1~4)' })
colors: Color[] = [];
curentwpArr: number[] = [];
curentskillArr: number[] = [];
onLoad(): void {
super.onLoad();
}
onEnable(): void {
super.onEnable();
}
onDisable(): void {
super.onDisable();
}
onInit(): void {
this.typeNum = this.prema;
this.curentwpArr = [...(gg.game.playerWeaponIdArray ?? [])];
this.curentskillArr = this._extractSkillIds(gg.game.playerSkillIdArray);
if (this.typeNum == 1) {
this.click_btnWpSet();
} else {
this.click_btnSkill();
}
}
typeNum: number = 0;
click_btnClose(): void {
this.close();
}
click_btnClaer(): void {
if (this.ui_wpSelect.active) {
gg.game.playerWeaponIdArray = [];
gg.game.playerSkillIdArray = [];
this.curentwpArr = [];
this.curentskillArr = [];
this.updateWp();
}
}
click_btnSave(): void {
if (this.ui_setWp.active) {
for (let i = 0; i < this.ui_wpSetList.children.length; i++) {
let n = this.ui_wpSetList.children[i];
if (!n.active) continue;
let id = Number(n.name);
let level = Number(n.getChildByName('EditBox').getComponent(EditBox).string);
gg.data.project.unlockWeapon(id);
gg.data.project.setWeaponLevel(id, level);
}
gg.data.saveToServer();
gg.ui.showToast('武器等级已保存');
} else if (this.ui_wpSelect.active) {
gg.game.playerWeaponIdArray = [...this.curentwpArr];
gg.game.playerSkillIdArray = [...this.curentskillArr];
gg.ui.showToast('预设武器/技能已保存');
}
}
click_btnSkill(): void {
this.ui_setWp.active = false;
this.ui_wpSelect.active = true;
this.updateWp();
}
click_btnWpSet(): void {
this.ui_setWp.active = true;
this.ui_wpSelect.active = false;
this.updateSetLevel();
}
updateWp() {
const allList = this._getGmWeapon1List();
this.ui_wpList.children.map(x => x.active = false);
for (let i = 0; i < allList.length; i++) {
let w1 = allList[i];
let n: Node = null;
if (i < this.ui_wpList.children.length) {
n = this.ui_wpList.children[i];
} else {
n = instantiate(this.ui_wpList.children[0]);
this.ui_wpList.addChild(n);
}
n.active = true;
let bg = n.getChildByName('bg');
let lbName = n.getChildByName('lbName');
let select = n.getChildByName('select');
select.active = this.curentwpArr.includes(Number(w1.id));
lbName.getComponent(Label).string = w1.name;
bg.color = this.colors[w1.quality - 1];
n.targetOff(Node.EventType.TOUCH_END);
n.on(Node.EventType.TOUCH_END, this.onClickWp.bind(this, n, w1), this);
}
//已选武器
let selectedList = allList.filter(item => this.curentwpArr.includes(Number(item.id)));
//所有技能(词条表 Skill,按 weapon 字段关联 Weapon1)
let allSkillList = gg.data.table.getTableList<ITableSkill>(TableNames.Skill);
//已选武器的所有技能
let selectedSkillList = allSkillList.filter(item =>
selectedList.some(w => Number(w.id) === Number(item.weapon)),
);
this.ui_waSelectSkillList.children.map(x => x.active = false);
for (let i = 0; i < selectedSkillList.length; i++) {
const s2 = selectedSkillList[i];
const w2 = gg.data.project.getWeaponData(s2.weapon);
let n: Node = null;
if (i < this.ui_waSelectSkillList.children.length) {
n = this.ui_waSelectSkillList.children[i];
}
else {
n = instantiate(this.ui_waSelectSkillList.children[0]);
this.ui_waSelectSkillList.addChild(n);
}
n.active = true;
let bg = n.getChildByName('bg');
let lbName = n.getChildByName('lbName');
let lbDesc = n.getChildByName('lbDesc');
let select = n.getChildByName('select');
lbName.getComponent(Label).string = gg.lang.get(w2.name);
lbDesc.getComponent(RichText).string = gg.lang.get(s2.desc);
select.active = this.curentskillArr.includes(Number(s2.id));
bg.color = this.colors[s2.quality - 1];
n.targetOff(Node.EventType.TOUCH_END);
n.on(Node.EventType.TOUCH_END, this.onClickWpSelect.bind(this, n, s2), this);
}
}
updateSetLevel() {
const allList = this._getGmWeapon1List();
this.ui_wpSetList.children.map(x => x.active = false);
for (let i = 0; i < allList.length; i++) {
let w1 = allList[i];
let n: Node = null;
if (i < this.ui_wpSetList.children.length) {
n = this.ui_wpSetList.children[i];
} else {
n = instantiate(this.ui_wpSetList.children[0]);
this.ui_wpSetList.addChild(n);
}
n.active = true;
n.name = w1.id.toString();
let bg = n.getChildByName('bg');
let lbName = n.getChildByName('lbName');
let ed = n.getChildByName('EditBox').getComponent(EditBox);
lbName.getComponent(Label).string = w1.name;
bg.color = this.colors[w1.quality - 1];
ed.string = gg.data.project.getWeaponLevel(w1.id).toString();
}
}
onClickWp(n: Node, data: ITableHero) {
const id = Number(data.id);
if (!this.curentwpArr.includes(id)) {
this.curentwpArr.push(id);
} else {
this.curentwpArr.splice(this.curentwpArr.indexOf(id), 1);
}
this.updateWp();
}
onClickWpSelect(n: Node, data: ITableSkill) {
const skillId = Number(data.id);
if (this.curentskillArr.includes(skillId)) {
this.curentskillArr.splice(this.curentskillArr.indexOf(skillId), 1);
n.getChildByName('select').active = false;
} else {
this.curentskillArr.push(skillId);
n.getChildByName('select').active = true;
}
}
/** Weapon1 全表:有 group 配置的英雄/武器(含刀棍斧箭) */
private _getGmWeapon1List(): ITableHero[] {
const provider = WeaponComposeConfigProvider.instance;
provider.load();
return (gg.data.table.getTableList<ITableHero>(TableNames.Hero) ?? [])
.filter((w) => provider.getWeapon1Group(w.id).length > 0)
.sort((a, b) => Number(a.id) - Number(b.id));
}
/** 兼容 GM 预设:playerSkillIdArray 可能是 number[] 或带 id 的对象数组 */
private _extractSkillIds(raw: unknown): number[] {
if (!Array.isArray(raw)) return [];
const ids: number[] = [];
for (const entry of raw) {
const id = typeof entry === 'number' ? entry : Number((entry as { id?: number })?.id);
if (Number.isFinite(id) && id > 0 && !ids.includes(id)) {
ids.push(id);
}
}
return ids;
}
}