var {GameNotificationKey} = require('../utils/GameEnum'); const PackApi = require('../net/PackApi'); const GameModule = require('../utils/GameModule'); cc.Class({ extends: cc.Component, properties: { packContentNode: cc.Node, packPagePrefab: cc.Prefab, pageView: cc.PageView, indicatorLayout: cc.Layout, indicatorPrefab: cc.Prefab, //装备卡片节点 propDescNode: cc.Node, itemNameLabel: cc.Label, itemDescRichText: cc.RichText, equipButton: cc.Button, }, // LIFE-CYCLE CALLBACKS: onLoad () { this.pageViewSize = this.pageView.node._contentSize; this.pageView.node.on('scrolling', this.pageScrolling, this); this.winSize = cc.view.getVisibleSize(); if (this.winSize.height <= 1100) { this.packContentNode.getComponent(cc.Widget).top = 70; this.packContentNode.height = 984; } this.setEventListener(); }, setEventListener() { GameEvent.on(GameNotificationKey.ShowPropDesc, this, (position, cardInfo) => { this.showPropDesc(position, cardInfo); }); GameEvent.on(GameNotificationKey.HidePropDesc, this, (position) => { this.hidePropDesc(); }); GameModule.homeGuide.on('Fire_state37', this.equipCardAction, this); }, start () { }, /* * 打开背包 * @param justOpen [Bool] justOpen是否只打开背包 * */ init(justOpen,uid) { this.uid = uid; this.justOpen = justOpen; this.node.parent = cc.find("Canvas"); this.node.setContentSize(cc.view.getVisibleSize()); this.node.active = false; if (justOpen == true) { this.equipButton.node.active = false; } else { this.equipButton.node.active = true; } this.loadUserPackInfo(); }, onDisable() { this.destoryData(); this.propDescNode.active = false; this.equipButton.enabled = true; }, //读取背包信息 loadUserPackInfo() { PackApi.getUserPackInfo((responseData) => { // console.log("responseData: " + JSON.stringify(responseData)); this.loadPackData(responseData.list); }, (error) => { console.log('userinformation error' + error); this.setupEmptyPack(); }); }, destoryData() { for (let child of this.pageView.content.children) { if (cc.isValid(child)) { child.destroy(); } } for (let child of this.indicatorLayout.node.children) { if (cc.isValid(child)) { child.destroy(); } } this.pageView.removeAllPages(); }, showPack() { this.node.zIndex += 1; this.node.active = true; }, setupEmptyPack() { this.destoryData(); for (var i = 0; i < 1; i++) { let item = cc.instantiate(this.packPagePrefab); item = item.getComponent('UserPackPage'); item.init(i); item.node.setContentSize(this.pageViewSize); this.pageView.addPage(item.node); let indicatorItem = cc.instantiate(this.indicatorPrefab); this.indicatorLayout.node.addChild(indicatorItem); let indicatorItemMng = indicatorItem.getComponent('UserPackIndicator'); indicatorItemMng.pageLabel.string = (i + 1).toString(); indicatorItemMng.indicatorIndex = i; if (i === 0) { indicatorItemMng.isSelected = true; } } }, loadPackData(list) { if (list && list.length > 0) { this.destoryData(); this.list = list; let page = Math.ceil(this.list.length / 20.0); for (var i = 0; i < page; i++) { let array = this.list.slice(i,((i+1) * 20)); let item = cc.instantiate(this.packPagePrefab); item = item.getComponent('UserPackPage'); item.init(array,i); item.node.setContentSize(this.pageViewSize); this.pageView.addPage(item.node); let indicatorItem = cc.instantiate(this.indicatorPrefab); this.indicatorLayout.node.addChild(indicatorItem); let indicatorItemMng = indicatorItem.getComponent('UserPackIndicator'); indicatorItemMng.pageLabel.string = (i + 1).toString(); indicatorItemMng.indicatorIndex = i; if (i === 0) { indicatorItemMng.isSelected = true; } } if (!this.justOpen) { GameModule.homeGuide.getComponent('HomeGuide').handleGuideStateNext('state32', 'state33'); } } else { this.setupEmptyPack(); } }, closeNodeAction() { this.node.active = false; }, pageScrolling() { if (this.propDescNode.active == true) { this.propDescNode.active = false; GameEvent.fire(GameNotificationKey.RefreshPackItem); } }, pageScrollEnd() { let selectedIndex = this.pageView.getCurrentPageIndex(); for (let child of this.indicatorLayout.node.children) { if (child.getComponent('UserPackIndicator') != undefined) { let indicatorItemMng = child.getComponent('UserPackIndicator'); if (selectedIndex == indicatorItemMng.indicatorIndex) { indicatorItemMng.isSelected = true; } else { indicatorItemMng.isSelected = false; } } } }, showPropDesc(position,cardInfo) { this.equipButton.enabled = true; this.cardInfo = cardInfo; var positionY = (-position.y + 210); if (this.winSize.height <= 1100 && -position.y >= 200) { positionY = -position.y + 120; } let position2 = cc.p(-position.x, positionY); this.propDescNode.setPosition(position2); this.propDescNode.active = true; this.itemNameLabel.string = cardInfo.name; var validTime = '永久'; if (cardInfo.validTime > 0) { validTime = DWTool.calculateTime(cardInfo.validTime / 1000); } this.itemDescRichText.string = cardInfo.funcInfo + '\n' + validTime +''; if (this.justOpen == false) { if (cardInfo.id > 0 && cardInfo.defend) { this.equipButton.node.active = true; } else { this.equipButton.node.active = false; } } }, hidePropDesc() { this.propDescNode.active = false; }, // 装备卡片 equipCardAction() { this.equipButton.enabled = false; this.postInsertCard(); }, postInsertCard() { PackApi.postInsertCard(this.uid, this.cardInfo.id, (responseData) => { // console.log("insert card: " + JSON.stringify(responseData)); this.insertCardSuccess(); }, (error) => { console.log('insert card error' + error); this.equipButton.enabled = true; }); }, insertCardSuccess() { this.equipButton.enabled = true; this.closeNodeAction(); this.propDescNode.active = false; GameEvent.fire(GameNotificationKey.InsertCardToUser,this.cardInfo); GameEvent.fire(GameNotificationKey.RefreshInsertCardsInfo,this.cardInfo,true); // GameEvent.fire(GameNotificationKey.RefreshPackInfo,this.cardInfo.id,false); GameModule.homeGuide.getComponent('HomeGuide').handleGuideStateNext('state37', 'state34'); }, //刷新背包内容 refreshPackInfo(itemId,isAdd) { this.destoryData(); for(var index in this.list) { let item = this.list[index]; if (item.itemId == itemId) { if (isAdd) { if (item.count >= 999) { let newItem = new Object(); newItem.itemId = itemId; newItem.count = 1; this.list.splice((index + 1), 0, newItem); } else { item.count += 1; } } else { if (item.count == 1) { this.list.splice(index,1); } else { item.count -= 1; } } break; } } let page = Math.ceil(this.list.length / 20.0); for (var i = 0; i < page; i++) { let array = this.list.slice(i,((i+1) * 20)); let item = cc.instantiate(this.packPagePrefab); item = item.getComponent('UserPackPage'); item.init(array,i); item.node.setContentSize(this.pageViewSize); this.pageView.addPage(item.node); let indicatorItem = cc.instantiate(this.indicatorPrefab); this.indicatorLayout.node.addChild(indicatorItem); let indicatorItemMng = indicatorItem.getComponent('UserPackIndicator'); indicatorItemMng.pageLabel.string = (i + 1).toString(); indicatorItemMng.indicatorIndex = i; if (i === 0) { indicatorItemMng.isSelected = true; } } }, // update (dt) {}, });