const GameModule = require("./utils/GameModule"); const DWTool = require("./utils/DWTool"); const Api = require('./net/Api'); const NotiKey = require('./utils/GameEnum').GameNotificationKey; var cityList = require('./data/city'); // const BuildingModel = require('./utils/BuildingModel'); // const AlertManager = require('./utils/AlertManager'); // const ReportType = require("./utils/GameEnum").ReportType; cc.Class({ extends: cc.Component, properties: { _reportFailDuration: 0, grossIncomeLabel: cc.Label, grossCoin: sp.Skeleton, // rateLabel: cc.Label, headSprite: cc.Sprite, starsLabel: cc.Label, starsProgress: cc.ProgressBar, diamondLabel: cc.Label, ticketLabel: cc.Label, recordModify: [], recordUnlockModify: [], _stars: 0, stars: { get: function () { return this._stars; }, set: function (value) { this._stars = value; // 每5级跳动一次 if (value % 5 == 0) { this.updateStarAnimation(); } let cStar = Math.floor(this._stars / 5) this.starsLabel.string = cStar; // 把用户的星星数上报给微信 DWTool.submitWechatStars(cStar); // 更新星星数的进度条 this.starsProgress.progress = value % 5 == 0 ? 1 : value % 5 / 5 if (cStar >= 10) { GameModule.homeGuide.getComponent('HomeGuide').handleGuideState6(); // GameEvent.fire(NotiKey.ResetLevelHomePaddingBottom); } } }, _grossIncome: 0, grossIncome: { get: function () { return this._grossIncome; }, set: function (value) { this._grossIncome = value < 0 ? 0 : parseInt(value); this.grossIncomeLabel.string = DWTool.coinParse(this._grossIncome); GameModule.audioMng.playGetcoin(); } }, _diamond: 0, diamond: { get: function () { return this._diamond; }, set: function (value) { this._diamond = value < 0 ? 0 : parseInt(value); this.diamondLabel.string = this._diamond; } }, _ticket: 0, ticket: { get: function () { return this._ticket; }, set: function (value) { this._ticket = value < 0 ? 0 : parseInt(value); this.ticketLabel.string = this._ticket; } }, levelHomeItemFullCount: 0 }, onLoad() { GameModule.userInfo = this; this._rate = 0; this._isPlayAnimation = false; this.seq = 1; // 初始化用户信息 this.initUserInfo() // 监听用户头像点击事件 this.headSprite.node.on(cc.Node.EventType.TOUCH_END, _.debounce(() => { GameEvent.fire(NotiKey.ShowUserInfomation, this.userInfo.uid); }, 1000, true), this); // 监听用户收取金币事件 GameEvent.on(NotiKey.UserCollectCoin, this, (flag) => { let animKey = flag ? 'jinbi_huoqu2' : 'jinbi_huoqu' this.grossCoin.setAnimation(0, animKey, false) }); // 监听满级事件 GameEvent.on(NotiKey.LevelHomeItemBuildingFull, this, () => { this.levelHomeItemFullCount += 1; if (this.levelHomeItemFullCount === 5 && Global.devCityId < cityList.length) { GameEvent.fire(NotiKey.LevelHomeItemBuildingAllFull); GameEvent.fire(NotiKey.CurrentCompanyMax); // 满级立刻上报 this.doReport(); } }); // 轮询上报 this.reportFunc = () => { if (this._reportFailDuration > 0) { this._reportFailDuration -= 3; return; } this.doReport() } this.schedule(this.reportFunc, 3.0); }, /** * 上报用户数据 */ doReport() { DWTool.reportInfo(this.seq, this.grossIncome, this.stars, this.recordModify, this.recordUnlockModify) .then(() => { this._reportFailDuration = 0; this.recordModify = []; this.recordUnlockModify = []; this.seq += 1; }).catch(err => { if (err.code === -4) { this._reportFailDuration = 60; } console.log(err.msg); }) }, /** * 初始化用户数据 */ initUserInfo() { let userInfo = this.userInfo = Global.userData; Api.createImageFromUrl(userInfo.head, (spriteFrame) => { this.headSprite.spriteFrame = spriteFrame; }, null); this.updateUserRes({ grossIncome: userInfo.grossIncome, stars: userInfo.ogStars, diamond: userInfo.diamond, ticket: userInfo.ticket }) }, /** * 更新用户资源数据 * @param {object} data 更新资源对象 * @param {number} data.grossIncome 增加的金币 * @param {number} data.stars 星星 * @param {number} data.diamond 钻石 * @param {number} data.ticket 艺人券 */ updateUserRes(data) { console.log("Update userData: ", JSON.stringify(data)); if (data.grossIncome) { this.grossIncome += parseInt(data.grossIncome); } if (data.stars) { this.stars += parseInt(data.stars); } if (data.diamond) { this.diamond += parseInt(data.diamond); } if (data.ticket) { this.ticket += parseInt(data.ticket); } }, /** * 增加星星的动画 */ updateStarAnimation() { if (this._isPlayAnimation) { return; } this._isPlayAnimation = true; let jumpHeight = 30; let duration = 0.2; let animationArray = []; while (jumpHeight > 0.1) { jumpHeight = jumpHeight - (jumpHeight / 3); duration = duration - (duration / 3); let upAction = cc.moveBy(duration, 0, jumpHeight).easing(cc.easeCubicActionOut()); let downAction = cc.moveBy(duration, 0, -jumpHeight).easing(cc.easeCubicActionIn()); animationArray.push(upAction); animationArray.push(downAction); } let callback = cc.callFunc(() => { this._isPlayAnimation = false; }); animationArray.push(callback); this.starsLabel.node.runAction(cc.sequence(animationArray)); }, updateRecordModify(buildingInfo) { for (let i = 0; i < this.recordModify.length; i++) { let temp = this.recordModify[i]; if (buildingInfo.buildingId == temp.buildingId) { this.recordModify.splice(i, 1, buildingInfo) return; } } this.recordModify.push(buildingInfo); }, /** * 星星数到达10个,触发艺人入驻提示 */ stars10() { GameEvent.fire(NotiKey.LevelHomeItemUnlock); } });