const HomeApi = require("../net/HomeApi"); const GameModule = require("../utils/GameModule"); const { GameNotificationKey } = require("../utils/GameEnum") const AlertManager = require('../utils/AlertManager') const CityList = require("../data/city"); cc.Class({ extends: cc.Component, properties: { scrollView: cc.ScrollView, levelHomeItem: cc.Prefab, levelHomeTop: cc.Prefab, levelHomeBottom: cc.Prefab, minContentPosition: -150, _unlockBuilding: [], showOffLineUI: true, mask: cc.Node, actGift: { tooltip: '领取弹出层', default: null, type: cc.Node }, }, /** * home 初始化方法, 所有的初始化操作在这里操作, 必须在加入父节点之前调用 * */ init(uid, cityId) { this.uid = uid; this.cityId = cityId; this.buildingInfos = []; this.node.parent = cc.find("Canvas/game"); this.scrollView.scrollToBottom(); this.refreshTheme(); this.getNetworkData(); }, refreshTheme() { if (this.topScript) { this.topScript.init(this.cityId); } if (this.buildings) { for (let i = 0; i < this.buildings.length; i++) { let itemScript = this.buildings[i]; itemScript.init(this, this.cityId, i + 1); } } if (this.bottomScript) { this.bottomScript.init(this.cityId); } }, // LIFE-CYCLE CALLBACKS: onLoad() { this.buildings = []; this.unlockBuilding = []; this.matchScreenSize(); let topNode = cc.instantiate(this.levelHomeTop); this.topScript = topNode.getComponent('LevelHomeTop'); this.topScript.init(this.cityId); this.scrollView.content.addChild(topNode); for (let i = 0; i < 5; i++) { let item = cc.instantiate(this.levelHomeItem); let itemScript = item.getComponent('LevelHomeItem'); itemScript.init(this, this.cityId, i + 1); this.scrollView.content.addChild(item); this.buildings.push(itemScript); } let bottomNode = cc.instantiate(this.levelHomeBottom); this.bottomScript = bottomNode.getComponent('LevelHomeBottom'); this.bottomScript.init(this.cityId); this.scrollView.content.addChild(bottomNode); this.setEventLisenter(); // 监听小游戏隐藏到后台事件 if (CC_WECHATGAME) { wx.onHide(() => { this.wxHide = true // 离线收益计算重置 this.showOffLineUI = true let lastTime = new Date().getTime() cc.sys.localStorage.setItem('offlineLastTime', lastTime); // 退出前立刻调用一次上报 GameModule.userInfo.doReport() }) wx.onShow(() => { if (this.wxHide) { this.refreshTheme(); this.getNetworkData(); this.wxHide = false } }) } }, setEventLisenter() { this.scrollView.node.on("scrolling", (event) => { if (this.scrollView._isOutOfBoundary()) { if (this.scrollView._outOfBoundaryAmount.y > 0) { // 超出上面的界限 this.scrollView._outOfBoundaryAmount.y = 0; this.scrollView._adjustContentOutOfBoundary(); } else { // 超出下面的界限 if (this.scrollView._outOfBoundaryAmount.y < this.minContentPosition) { if (this.recordScrollViewPosition) { this.scrollView.content.setPosition(this.recordScrollViewPosition); return; } else { this.recordScrollViewPosition = this.scrollView.getContentPosition(); } } else { this.recordScrollViewPosition = null; } } } }, this); GameEvent.on(GameNotificationKey.showCatFlyAnimation, this, () => { this.scrollView.scrollToTop(0.2); }); GameEvent.on(GameNotificationKey.ReloadLevelHomeData, this, () => { this.refreshTheme(); this.getNetworkData(); }); GameEvent.on(GameNotificationKey.ResetLevelHomePaddingBottom, this, () => { this.resetPaddingBottom(); }); //房间解锁通知,用来触发是否显示入驻艺人通知(因为通知需要随机出现在没有艺人的房间,所以需要在LevelHome这一层来处理) GameEvent.on(GameNotificationKey.LevelHomeItemUnlock, this, this.randomResidentTip); }, /** * 适配不同高度的屏幕尺寸 */ matchScreenSize() { let initHeight = 1624; let vsize = cc.view.getVisibleSize() let paddingY = (initHeight - vsize.height) / 2; if (paddingY < 0) { paddingY = 0; } this.scrollView.content.getComponent(cc.Layout).paddingTop = paddingY; this.scrollView.elastic = false; // 底部加多一块100px的内边距, 让底楼不要完全显示出来 this.scrollView.content.getComponent(cc.Layout).paddingBottom = paddingY - 100; }, resetPaddingBottom() { let initHeight = 1624; let vsize = cc.view.getVisibleSize() let paddingY = (initHeight - vsize.height) / 2; if (GameModule.userInfo.stars >= 20) { this.scrollView.elastic = true; // 底部加多一块100px的内边距, 让底楼不要完全显示出来 this.scrollView.content.getComponent(cc.Layout).paddingBottom = paddingY - 100; } else { this.scrollView.elastic = false; // 底部加多一块100px的内边距, 让底楼不要完全显示出来 this.scrollView.content.getComponent(cc.Layout).paddingBottom = paddingY - 275; } this.scrollView.scrollToBottom(0); }, // 用来访问自己家园时, 重置scrollView位置 start() { this.scrollView.scrollToBottom(0.0); }, getNetworkData(callback) { this.getUserBuildings() .then((responseData) => { // 清空数据 this.buildingInfos = []; // 满级后去别的城市就置零 GameModule.userInfo.levelHomeItemFullCount = 0; let sortArray = responseData.buildings.sort((a, b) => { return a.buildingId < b.buildingId; }); // 离线收益金币数量 let offlineGrossIncome = 0; sortArray.map((value, index, array) => { let model = Global.BuildingManager.getBuildingInfo(this.cityId, value.buildingId, value.level) this._unlockBuilding[index] = model.isUnlocked ? 1 : 0; if (model.isFull() && this.cityId === Global.devCityId) { try { if (this.cityId != CityList[CityList.length-1].id) { GameEvent.fire(GameNotificationKey.LevelHomeItemBuildingFull); } } catch (error) { console.log(error); } } model.coinCount = value.coinCount; model.artists = value.artists || []; if (model.artists.length > 0) { // 有艺人入驻的情况 let addition = 0; for (let i = 0; i < model.artists.length; i++) { let artist = model.artists[i]; addition += artist.stationJobLevel + 1; } offlineGrossIncome += (model.coinCount * (model.rate * addition)); } else { // 无艺人入驻时 offlineGrossIncome += (model.coinCount * model.rate); } this.buildingInfos.push(model); }); this._unlockBuilding = this._unlockBuilding.reverse(); // GameModule.userInfo.setUserInfo(responseData.user); // this.resetPaddingBottom(); callback && callback(); // 开始设置建筑 this.configBuildings(); // 离线收益处理 this.configOffIncome(sortArray, offlineGrossIncome) return this.getBuildingItems(); }) .then((buildingItems) => { for (const buildingId in buildingItems) { let itemScript; let filterList = this.buildings.filter(item => item.buildingInfo.buildingId === parseInt(buildingId)) || []; if (filterList.length > 0) { itemScript = filterList[0]; if (buildingItems.hasOwnProperty(buildingId)) { let prop = buildingItems[buildingId]; itemScript.showProp(prop, false); } } } }) .catch((err) => { console.log(err); }); }, getUserBuildings() { return new Promise((resolve, reject) => { // 获取目标用户的建筑 HomeApi.getUserBuildings(this.uid, this.cityId, (responseData) => { resolve(responseData); }, (error) => { reject(error); }); }) }, getBuildingItems() { return new Promise((resolve, reject) => { HomeApi.getBuildingItems((buildingItems) => { resolve(buildingItems); }, (err) => { reject(err); }) }); }, configBuildings() { for (let i = 0; i < this.buildings.length; i++) { let itemScript = this.buildings[i]; itemScript.config(this.buildingInfos[i], this.uid, this._unlockBuilding, (newBuildingInfo) => { this.buildingInfos[i] = newBuildingInfo; }); } this.randomResidentTip(); }, /** * 离线收益处理 * @param {Array} sortArray 用户当前城市buildingInfos数组 * @param {Number} offlineGrossIncome 离线收益金币数量 */ configOffIncome(sortArray, offlineGrossIncome) { let lastTime = cc.sys.localStorage.getItem('offlineLastTime'); if (!lastTime) { lastTime = new Date().getTime() cc.sys.localStorage.setItem('offlineLastTime', lastTime); } else { let curTime = new Date().getTime() cc.sys.localStorage.setItem('offlineLastTime', curTime); // 显示离线收益的条件: // 1. 离上一次显示超过5分钟 // 2. 用户解锁星数大于20 // 3. 当前城市已解锁建筑数大于1 let unLockStatus1 = curTime - lastTime > 300 * 1000 let unLockStatus2 = GameModule.userInfo.stars > 20 let unLockStatus3 = (() => { let flag = false sortArray.forEach(n => { if (n.level != 0) { flag = true } }) return flag })() // showOffLineUI: 用户每次进入游戏离线收益只会显示1次 if (this.showOffLineUI && unLockStatus1 && unLockStatus2 && unLockStatus3) { this.showOffLineUI = false; AlertManager.showOfflineGrossIncome(offlineGrossIncome); } } }, randomResidentTip() { if (GameModule.userInfo.stars > 10) { if (this.buildings && this.buildings.length > 0) { let noArtistBuildings = []; for (let i = 0; i < this.buildings.length; i++) { let itemScript = this.buildings[i]; let buildingInfo = this.buildingInfos[i]; if (buildingInfo.level == 0) { itemScript.setResidentTip(false); continue; } if (buildingInfo.artists && buildingInfo.artists.length > 0) { itemScript.setResidentTip(false); } else { noArtistBuildings.push(itemScript); } } if (noArtistBuildings.length > 0) { let index = Math.floor(Math.random() * noArtistBuildings.length); for (let i = 0; i < noArtistBuildings.length; i++) { noArtistBuildings[i].setResidentTip(i == index); } clearInterval(this.residentTipHideTimer); clearInterval(this.residentTipShowTimer); this.residentTipHideTimer = setInterval(() => { noArtistBuildings[index].setResidentTip(false); }, 5000); this.residentTipShowTimer = setInterval(() => { this.randomResidentTip(false); }, 15000); } } } }, /** * 显示领取动画 */ showActGift(showFrame, showText) { GameModule.audioMng.playGift() this.mask.active = true; this.actGift.active = true; cc.find('/gift_sprite', this.actGift).getComponent('cc.Sprite').spriteFrame = showFrame; cc.find('/gift_label', this.actGift).getComponent('cc.Label').string = showText; }, /** * 关闭领取动画 */ hideActGift() { this.mask.active = false; this.actGift.active = false; }, onDestroy() { if (this.residentTipHideTimer) { clearInterval(this.residentTipHideTimer); clearInterval(this.residentTipShowTimer); } GameEvent.off(GameNotificationKey.showCatFlyAnimation, this); GameEvent.off(GameNotificationKey.ReloadLevelHomeData, this); GameEvent.off(GameNotificationKey.ResetLevelHomePaddingBottom, this); GameEvent.off(GameNotificationKey.LevelHomeItemUnlock, this); }, });