205 lines
6.3 KiB
JavaScript
205 lines
6.3 KiB
JavaScript
|
import GameModel from "../ts/core/GameModel";
|
||
|
import SKDataUtil from "../ts/gear_2.3.4/util/SKDataUtil";
|
||
|
|
||
|
cc.Class({
|
||
|
extends: cc.Component,
|
||
|
|
||
|
properties: {
|
||
|
itemContent: cc.Node,
|
||
|
equipNodes: [cc.Node],
|
||
|
roleNode: cc.Node,
|
||
|
bagItem: cc.Prefab,
|
||
|
equipItem: cc.Prefab,
|
||
|
},
|
||
|
|
||
|
onLoad() {
|
||
|
this.currentY = 0;
|
||
|
this.currentSelected = -1;
|
||
|
// GameModel.send('c2s_get_bagitem', { roleid: GameModel.player.roleid });
|
||
|
|
||
|
this.roleNode.getComponent('UIRole').setInfo(GameModel.player);
|
||
|
},
|
||
|
|
||
|
start() {
|
||
|
|
||
|
},
|
||
|
|
||
|
loadInfo(data) {
|
||
|
this.itemNodes = [];
|
||
|
this.equipList = [];
|
||
|
this.curEquips = {};
|
||
|
console.log('--------------------裝備hello');
|
||
|
|
||
|
this.curEquips = data.content.curEquips;
|
||
|
this.loadEquips();
|
||
|
|
||
|
if (GameModel.player.getItemList() == null || GameModel.player.getItemNum() == 0) {
|
||
|
GameModel.send('c2s_get_bagitem', {
|
||
|
roleid: GameModel.player.roleid
|
||
|
});
|
||
|
} else {
|
||
|
this.loadBagList();
|
||
|
}
|
||
|
},
|
||
|
|
||
|
loadEquips() {
|
||
|
this.equipNodes.forEach(e => {
|
||
|
e.getComponent('EquipItem').reset();
|
||
|
});
|
||
|
for (var key in this.curEquips) {
|
||
|
let isShow = false;
|
||
|
//如果當前裝備被刪除,不存在(既不在使用裝備裡,也不在背包裝備裡),則不顯示
|
||
|
for (let k in GameModel.equipData.use) {
|
||
|
if (GameModel.equipData.use.hasOwnProperty(k) && GameModel.equipData.use[k] == this.curEquips[key])
|
||
|
isShow = true;
|
||
|
}
|
||
|
if (GameModel.equipData.list.indexOf(this.curEquips[key]) != -1) {
|
||
|
isShow = true;
|
||
|
}
|
||
|
|
||
|
if (isShow) {
|
||
|
let equipid = this.curEquips[key];
|
||
|
let equipNode = this.equipNodes[key - 1];
|
||
|
if (equipNode) {
|
||
|
equipNode.getComponent('EquipItem').reset();
|
||
|
if (equipid && GameModel.equipData.info[equipid]) {
|
||
|
equipNode.getChildByName('itembg').active = true;
|
||
|
equipNode.getComponent('EquipItem').loadInfo(GameModel.equipData.info[equipid], 1, true);
|
||
|
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
},
|
||
|
|
||
|
checkEquipsOnUse(equipId) {
|
||
|
for (var key in this.curEquips) {
|
||
|
if (equipId == this.curEquips[key])
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
return false;
|
||
|
},
|
||
|
|
||
|
loadBagList() {
|
||
|
this.itemContent.destroyAllChildren();
|
||
|
this.itemNodes = [];
|
||
|
if (this.equipList && this.equipList.length == 0) {
|
||
|
if (GameModel.equipData) {
|
||
|
let equipBagList = GameModel.equipData.list.slice(0);
|
||
|
let useList = GameModel.equipData.use;
|
||
|
//將已穿上的裝備也加入到背包列表裡
|
||
|
for (var it in useList) {
|
||
|
if (useList.hasOwnProperty(it)) {
|
||
|
equipBagList.push(useList[it]);
|
||
|
}
|
||
|
}
|
||
|
//去重
|
||
|
var singleEquipList = [];
|
||
|
for (var i = 0; i < equipBagList.length; i++) {
|
||
|
if (equipBagList.indexOf(equipBagList[i]) == i) {
|
||
|
singleEquipList.push(equipBagList[i]);
|
||
|
}
|
||
|
}
|
||
|
for (const equip of singleEquipList) {
|
||
|
if (GameModel.equipData.info[equip]) {
|
||
|
this.equipList.push({
|
||
|
itemid: equip,
|
||
|
info: GameModel.equipData.info[equip],
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
let equipIndex = -1;
|
||
|
|
||
|
for (var key in this.curEquips) {
|
||
|
equipIndex = -1;
|
||
|
this.equipList.find((e, index) => {
|
||
|
if (e.itemid == this.curEquips[key]) {
|
||
|
equipIndex = index;
|
||
|
return true;
|
||
|
} else {
|
||
|
return false;
|
||
|
}
|
||
|
});
|
||
|
|
||
|
if (equipIndex != -1) {
|
||
|
this.equipList.splice(equipIndex, 1);
|
||
|
}
|
||
|
}
|
||
|
if (!SKDataUtil.isArray(this.equipList)) {
|
||
|
cc.warn(`$警告:無裝備數組!`);
|
||
|
return;
|
||
|
}
|
||
|
this.itemContent.height = Math.ceil(this.equipList.length / 3) * 75;
|
||
|
for (let index = 0; index < this.equipList.length; index++) {
|
||
|
let item = null;
|
||
|
if (index < this.equipList.length) {
|
||
|
item = cc.instantiate(this.equipItem);
|
||
|
item.parent = this.itemContent;
|
||
|
item.getComponent('EquipItem').loadInfo(this.equipList[index].info, 1);
|
||
|
}
|
||
|
if (item) {
|
||
|
item.x = -75 + (index % 3) * 75;
|
||
|
item.y = -37.5 - Math.floor(index / 3) * 75;
|
||
|
|
||
|
let btn = item.getComponent(cc.Button);
|
||
|
var clickEventHandler = new cc.Component.EventHandler();
|
||
|
clickEventHandler.target = this.node;
|
||
|
clickEventHandler.component = "SchemeEquipsPanel";
|
||
|
clickEventHandler.customEventData = index;
|
||
|
clickEventHandler.handler = "bagItemClicked";
|
||
|
btn.clickEvents.push(clickEventHandler);
|
||
|
|
||
|
this.itemNodes.push(item);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
},
|
||
|
|
||
|
updateEquipsList() {
|
||
|
},
|
||
|
|
||
|
useEquipClicked(e, d) {
|
||
|
e.target.getComponent('EquipItem').selected();
|
||
|
},
|
||
|
|
||
|
bagItemClicked(e, d) {
|
||
|
if (this.currentSelected == d) {
|
||
|
return;
|
||
|
}
|
||
|
for (const item of this.itemNodes) {
|
||
|
if (e.target == item) {
|
||
|
continue;
|
||
|
}
|
||
|
item.getComponent('BagItem') && item.getComponent('BagItem').unSelected();
|
||
|
item.getComponent('EquipItem') && item.getComponent('EquipItem').unSelected();
|
||
|
}
|
||
|
},
|
||
|
|
||
|
|
||
|
updateEquips(data) {
|
||
|
this.curEquips = JSON.parse(data.curEquips);
|
||
|
this.loadEquips();
|
||
|
if (data.unloadEquipId != -1) {
|
||
|
if (GameModel.equipData.info[data.unloadEquipId]) {
|
||
|
this.equipList.push({
|
||
|
itemid: data.unloadEquipId,
|
||
|
info: GameModel.equipData.info[data.unloadEquipId],
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
this.loadBagList();
|
||
|
|
||
|
},
|
||
|
|
||
|
clear() {
|
||
|
this.roleNode.getComponent('UIRole').clear();
|
||
|
},
|
||
|
});
|