const GameModule = require("../utils/GameModule"); const {GameNotificationKey} = require('../utils/GameEnum'); const AlertManager = require('../utils/AlertManager'); const TapTapTool = require("../utils/TapTapTool"); var Promise = require('../lib/es6-promise').Promise; cc.Class({ extends: cc.Component, properties: { scrollView: cc.ScrollView, clickAddMoneyPrefab: cc.Prefab, _unlockBuilding: [], showOffLineUI: true, }, // LIFE-CYCLE CALLBACKS: onLoad () { this.minContentPosition = 0; this.buildings = []; this.unlockBuilding = []; this.setEventListener(); this.addMoneyPool = new cc.NodePool(); this.scrollViewMng = this.scrollView.getComponent('LevelHomeListAdapter'); // this._adState = 0; // this.initRoomAd(); }, setEventListener() { GameEvent.on(GameNotificationKey.RefreshBuildingData, this, (buildingModel) => { this.refreshAllbuildingGoldRate(buildingModel); }); GameEvent.on(GameNotificationKey.UnlockLevelHome, this, (buildingModel) => { this.unlockLevelHome(buildingModel); }); GameEvent.on(GameNotificationKey.GetRoomAward, this, (responseData) => { this.getRoomAward(responseData); }); GameModule.homeGuide.on('Fire_state33', this.configSignIn, this); }, /** * home 初始化方法, 所有的初始化操作在这里操作, 必须在加入父节点之前调用 * */ init(uid) { this.uid = uid; this.node.setContentSize(cc.view.getVisibleSize()); this.buildingInfos = []; this.node.parent = cc.find("Canvas/game"); // this.refreshTheme(); this.getNetworkData(); GameModule.homeGuide.getComponent('HomeGuide').handleGuideStateNext('state1','state4'); }, // clickAddMoney (position) { let item = null; if (this.addMoneyPool.size() > 0) { item = this.addMoneyPool.get(); } else { item = cc.instantiate(this.clickAddMoneyPrefab); } this.node.addChild(item); item.x = position.x; item.y = position.y; item.active = true; let itemManager = item.getComponent('ClickAddMoney'); itemManager.showAddMoney( () => { this.addMoneyPool.put(item); }); }, start () { }, sortNumber(a,b) { return a - b }, //数据排序,roomId小的在前 compareFunction (a, b) { if (a.roomId < b.roomId) { return 1; // a排在b的前面 } else if (a.roomId > b.roomId) { return -1; // a排在b的后面 } else { return 0; // a和b的位置保持不变 } }, getNetworkData(callback) { this.getUserBuildings() .then((userRooms) => { // 清空数据 this.buildingInfos = []; let sortArray = userRooms.sort(this.compareFunction); // 离线收益金币数量 // let offlineGrossIncome = 0; sortArray.map((value, index, array) => { let model = GameGlobal.BuildingManager.getBuildingInfo(value.roomId, value.roomLevel); this._unlockBuilding[index] = model.isUnlocked ? 1 : 0; if (value.roomStars == undefined) { model.roomStars = []; } else { model.roomStars = value.roomStars; } model.isUnlocked = value.isUnlocked; model.roomMt = value.roomMt; model.awardCount = value.awardCount; this.buildingInfos.push(model); }); this._unlockBuilding = this._unlockBuilding.reverse(); // GameModule.userInfo.setUserInfo(responseData.user); // this.resetPaddingBottom(); callback && callback(); // 开始设置建筑 this.configBuildings(); if (GameModule.homeGuide.getComponent('HomeGuide').isPassGuideState('state1') && GameModule.homeGuide.getComponent('HomeGuide').isPassGuideState('state4')) { // 离线收益处理 this.configOffIncome(); //每日签到奖励 this.configSignIn(); } //第一层楼是否已经解锁 if (!GameGlobal.BuildingManager.getRoomIsUnlocked(1) || GameGlobal.BuildingManager.getRoomLevel(1) < 5) { let guide = GameModule.homeGuide.getComponent('HomeGuide'); if (guide.isPassGuideState('state15') || guide.isPassGuideState('state16') || guide.isPassGuideState('state17') || guide.isPassGuideState('state20')) { GameModule.homeGuide.getComponent('HomeGuide').handleState('state21'); } this.scrollViewMng.configGuide(); } else if (GameGlobal.BuildingManager.getRoomLevel(1) < 25) { GameModule.homeGuide.getComponent('HomeGuide').handleGuideStateNext('state21', 'state24'); this.scrollViewMng.configGuide(); } }).catch((err) => { console.log(err); }); }, getUserBuildings() { return new Promise((resolve, reject) => { // 返回用户的建筑等级 if (GameGlobal.BuildingManager.networkRooms && GameGlobal.BuildingManager.networkRooms.length > 0) { resolve(GameGlobal.BuildingManager.networkRooms); } else { reject('error'); } }) }, configBuildings() { this.refreshAllbuildingGoldRate(); let sortLockRoom = 0; this.buildingInfos.forEach(n => { if (n.isUnlocked == 0) { sortLockRoom += 1; } }); if (sortLockRoom == 0) { this.scrollViewMng.configBuildings(this.buildingInfos); } else { let sortArray = this.buildingInfos.slice((sortLockRoom - 1),this.buildingInfos.length); this.scrollViewMng.configBuildings(sortArray); } GameModule.roomInfo = this.buildingInfos; }, unlockLevelHome(buildingModel) { this.buildingInfos.forEach((item, index) => { if (item.roomId == buildingModel.roomId) { this.buildingInfos[index] = buildingModel; } }); let sortLockRoom = 0; this.buildingInfos.forEach(n => { if (n.isUnlocked == 0) { sortLockRoom += 1; } }); // 等于0为所有建筑已开锁 if (sortLockRoom != 0) { let sortArray = this.buildingInfos.slice((sortLockRoom - 1),this.buildingInfos.length); this.scrollViewMng.configBuildings(sortArray, true); } GameModule.roomInfo = this.buildingInfos; }, /** * 离线收益处理 * @param {Array} sortArray 用户当前城市buildingInfos数组 * @param {Number} offlineGrossIncome 离线收益金币数量 */ configOffIncome() { let offlineGold = GameGlobal.offlineGold; // AlertManager.showOfflineGrossIncome(offlineGold); // return; let lastTime = cc.sys.localStorage.getItem('offlineLastTime'); if (!lastTime) { //缓存被删除后离线收益也要弹出 lastTime = new Date().getTime(); cc.sys.localStorage.setItem('offlineLastTime', lastTime); if (offlineGold.n <= 0 ) { return; } // 显示离线收益的条件: // 2. 总部大楼大于25级 // 3. 已拥有1个明星 // 4. 已签到过一次 let unLockStatus2 = GameModule.userInfo.buildingLevel >= 25; let unLockStatus3 = GameModule.userInfo.buyStarCount > 0; let unLockStatus4 = GameGlobal.signCount > 0; // showOffLineUI: 用户每次进入游戏离线收益只会显示1次 if (this.showOffLineUI && unLockStatus2 && unLockStatus3 && unLockStatus4) { this.showOffLineUI = false; AlertManager.showOfflineGrossIncome(offlineGold); } } else { if (offlineGold.n <= 0 ) { return; } let curTime = new Date().getTime(); cc.sys.localStorage.setItem('offlineLastTime', curTime); // 显示离线收益的条件: // 1. 离上一次显示超过5分钟 // 2. 总部大楼大于25级 // 3. 已拥有1个明星 // 4. 已签到过一次 let unLockStatus1 = curTime - lastTime > 300 * 1000; // let unLockStatus1 = curTime - lastTime > 1 * 1000; let unLockStatus2 = GameModule.userInfo.buildingLevel >= 25; let unLockStatus3 = GameModule.userInfo.buyStarCount > 0; let unLockStatus4 = GameGlobal.signCount > 0; // showOffLineUI: 用户每次进入游戏离线收益只会显示1次 if (this.showOffLineUI && unLockStatus1 && unLockStatus2 && unLockStatus3 && unLockStatus4) { this.showOffLineUI = false; AlertManager.showOfflineGrossIncome(offlineGold); } } }, configSignIn() { // 1. 总部大楼大于25级 // 2. 已拥有1个明星 let unLockStatus1 = GameModule.userInfo.buildingLevel >= 25; let unLockStatus2 = GameModule.userInfo.buyStarCount > 0; if (!GameGlobal.isSignAward && unLockStatus1 && unLockStatus2) { if (GameGlobal.signCount == 0) { GameModule.homeGuide.getComponent('HomeGuide').handleGuideStateNext('state31', 'state34'); } AlertManager.showSignInAlert(); } }, //刷新建筑每秒生产金币 refreshAllbuildingGoldRate(buildingModel) { if (buildingModel) { this.buildingInfos.forEach((item, index) => { if (item.roomId == buildingModel.roomId) { this.buildingInfos[index] = buildingModel; } }); this.scrollViewMng.refreshRoomData(buildingModel); } var totalRate = {"n": 0, 'e': 0}; this.buildingInfos.forEach(n => { if (n.isUnlocked) { let gold1 = TapTapTool.goldStrToClass(n.gold1); let gold2 = TapTapTool.goldStrToClass(n.gold2); let roomMt = TapTapTool.goldStrToClass(n.roomMt); var roomRate = TapTapTool.multiple(gold1, n.level); roomRate = TapTapTool.add(roomRate, gold2); roomRate = TapTapTool.multiple(roomRate,roomMt); totalRate = TapTapTool.add(totalRate, roomRate); } }); GameModule.userInfo.rateGold = totalRate; GameEvent.fire(GameNotificationKey.UpBuildingLevel); cc.sys.localStorage.setItem(`localRooms_${GameGlobal.user.uid}`, JSON.stringify(this.buildingInfos)); }, //获取房间里程碑 getRoomAward(responseData) { this.buildingInfos.forEach((item, index) => { if (item.roomId == responseData.roomId) { item.awardCount = responseData.awardCount; item.roomMt = responseData.roomMt; } }); this.refreshAllbuildingGoldRate(); } });