123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- 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<color=#F01A26>' + validTime +'</color>';
- 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) {},
- });
|