SamsaraGame/assets/Script/game/ChuanGongUI.js
2025-04-24 17:03:28 +08:00

201 lines
6.8 KiB
JavaScript

import GameModel from "../ts/core/GameModel";
import GameUtil from "../ts/core/GameUtil";
import SKUIUtil from "../ts/gear_2.3.4/util/SKUIUtil";
let CPubFunction = require('./PubFunction');
cc.Class({
extends: cc.Component,
properties: {
svPartnerList: cc.Prefab,
btnPartner: cc.Prefab,
nodChuanGongSuccess: cc.Prefab,
PartnerUI: cc.Prefab,
},
onLoad() {
this.vecInfo = [];
this.nLeftSelect = -1;
this.nRightSelect = -1;
this.nodLeft = null;
this.nodRight = null;
this.nodAdd = null;
this.nCostWhat = 0;
},
Init(vecInfo, nCurSelect) {
this.vecInfo = vecInfo;
this.nLeftSelect = nCurSelect;
},
start() {
cc.find('btnClose', this.node).getComponent(cc.Button).clickEvents.push(CPubFunction.CreateEventHandler(this.node, "ChuanGongUI", "Close", 0));
//-------------------
this.btnUseMoney = cc.find('btnUseMoney', this.node);
this.btnUseItem = cc.find('btnUseItem', this.node);
this.nodLeft = cc.find('nodLeft', this.node);
this.nodRight = cc.find('nodRight', this.node);
this.nodAdd = cc.find('nodAdd', this.node);
cc.find('nodAdd/btnAdd', this.node).getComponent(cc.Button).clickEvents.push(CPubFunction.CreateEventHandler(this.node, "ChuanGongUI", "OpenPartnerList", 0));
for (let nIndex = 0; nIndex < 2; nIndex++) {
let strPath = `ToggleContainer/toggle${nIndex}`;
let goToggle = cc.find(strPath, this.node);
goToggle.getComponent(cc.Toggle).checkEvents.push(CPubFunction.CreateEventHandler(this.node, "ChuanGongUI", "OnCheck", nIndex));
}
this.OnCheck(0, 0);
cc.find('btnUseItem', this.node).getComponent(cc.Button).clickEvents.push(CPubFunction.CreateEventHandler(this.node, "ChuanGongUI", "OnOK", 0));
cc.find('btnUseMoney', this.node).getComponent(cc.Button).clickEvents.push(CPubFunction.CreateEventHandler(this.node, "ChuanGongUI", "OnOK", 0));
this.RefreshPartnerInfo();
},
OnCheck(e, nIndex) {
this.nCostWhat = nIndex;
CPubFunction.ChangeTeamNodeState([this.btnUseItem, this.btnUseMoney], nIndex);
},
RefreshPartnerInfo() {
if (this.nLeftSelect < 0 || this.nLeftSelect >= this.vecInfo.length)
return;
this.SetPartnerInfo(this.nodLeft, this.vecInfo[this.nLeftSelect]);
if (this.nRightSelect != -1) {
this.nodRight.active = true;
this.nodAdd.active = false;
this.SetPartnerInfo(this.nodRight, this.vecInfo[this.nRightSelect]);
}
else {
this.nodRight.active = false;
this.nodAdd.active = true;
}
},
OpenPartnerList() {
let svPartnerList = SKUIUtil.createSubNode(cc.find('Canvas/MainUI'), cc.v2(0, 0), this.svPartnerList, 'svPartnerList');
cc.find('btnClose', svPartnerList).getComponent(cc.Button).clickEvents.push(CPubFunction.CreateEventHandler(this.node, "ChuanGongUI", "ClosePartnerList", svPartnerList));
let content = cc.find('ScrollView/view/content', svPartnerList);
let nY = 30;
for (var it in this.vecInfo) {
if (it == this.nLeftSelect)
continue;
let stInfo = this.vecInfo[it];
nY -= 90;
let goPartner = SKUIUtil.createSubNode(content,cc.v2(140,nY), this.btnPartner, 'btnPartner');
goPartner.getComponent(cc.Button).clickEvents.push(CPubFunction.CreateEventHandler(this.node, "ChuanGongUI", "SetRight", it));
SKUIUtil.setSpriteFrame(cc.find('HeadPic/Icon', goPartner), 'Common/huoban', 'huoban_' + stInfo.resid);
cc.find('Name', goPartner).getComponent(cc.Label).string = stInfo.name;
let level_lb = cc.find('Level', goPartner).getComponent(cc.Label);
GameUtil.setReliveLabel(level_lb, 0, stInfo.relive, stInfo.level);
cc.find('picState', goPartner).active = false;
}
content.height = Math.max(content.height, this.vecInfo.length * 90);
},
ClosePartnerList(stEvent, goItem) {
goItem.destroy();
},
SetRight(stEvet, stData) {
this.nRightSelect = stData;
this.RefreshPartnerInfo();
CPubFunction.FindAndDeleteNode(cc.find('Canvas/MainUI'), 'svPartnerList');
},
SetPartnerInfo(nodPartner, stInfo) {
SKUIUtil.setSpriteFrame(cc.find('btnHeadPic', nodPartner), 'Common/huoban', 'huoban_' + stInfo.resid);
cc.find('Name', nodPartner).getComponent(cc.Label).string = stInfo.name;
let level_lb = cc.find('Level', nodPartner).getComponent(cc.Label);
GameUtil.setReliveLabel(level_lb, 0, stInfo.relive, stInfo.level);
let nPercent = Math.floor(this.GetPercent(stInfo) * 100);
let nNewLen = cc.find('picPecent', nodPartner).width * (nPercent / 100);
cc.find('picPecent/picBar', nodPartner).width = nNewLen;
cc.find('picPecent/Label', nodPartner).getComponent(cc.Label).string = `${nPercent}%`;
},
GetPercent(stInfo) {
let stData = CPubFunction.GetLevel(stInfo.exp);
return stData.nCur / stData.nMax;
},
Close() {
this.node.destroy();
},
OnOK() {
if (this.nLeftSelect < 0 || this.nLeftSelect >= this.vecInfo.length || this.nRightSelect < 0 || this.nRightSelect >= this.vecInfo.length)
return;
GameModel.send('c2s_partner_exchange_exp', { nRoleID: GameModel.player.roleid, nPartnerA: this.vecInfo[this.nLeftSelect].resid, nPartnerB: this.vecInfo[this.nRightSelect].resid, nCostWhat: this.nCostWhat });
},
GetInfoByID(nID) {
for (var it in this.vecInfo) {
if (this.vecInfo[it].resid == nID)
return this.vecInfo[it];
}
return null;
},
OnChuanGongOK(vecInfo, data) {
this.vecInfo = vecInfo; // 刷新vecInfo
let stAInfo = this.GetInfoByID(data.nPartnerA);
stAInfo.exp = data.nAExp;
let stBInfo = this.GetInfoByID(data.nPartnerB);
stBInfo.exp = data.nBExp;
let goSuccess = SKUIUtil.createSubNode(cc.find('Canvas/MainUI'),cc.v2(0,0), this.nodChuanGongSuccess, 'nodChuanGongSuccess');
this.SetPartnerInfo(cc.find('Panel/nodLeft', goSuccess), stAInfo);
this.SetPartnerInfo(cc.find('Panel/nodRight', goSuccess), stBInfo);
cc.find('btnClose', goSuccess).getComponent(cc.Button).clickEvents.push(CPubFunction.CreateEventHandler(cc.find('Canvas/MainUI'), "MainUI", "CloseUIByName", "nodChuanGongSuccess"));
let comPartnerUI = cc.find('Canvas/MainUI/PartnerUI');
if (!comPartnerUI) {
return;
}
let comPartner = comPartnerUI.getComponent('PartnerUI');
comPartner.ChangePartnerExp(data.nPartnerA, data.nAExp);
comPartner.ChangePartnerExp(data.nPartnerB, data.nBExp);
comPartner.ShowPartnerList();
this.Close();
},
});