123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- const HomeApi = require("../net/HomeApi");
- const GameModule = require("../utils/GameModule");
- const GameNotificationKey = require('../utils/GameEnum').GameNotificationKey;
- 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');
- },
- setEventListener() {
- // GameEvent.on(GameNotificationKey.ClickAddMoney, this, (position) => {
- // this.clickAddMoney(position);
- // });
- 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() {
- // 获取目标用户的建筑
- // HomeApi.getUserBuildings((responseData) => {
- // resolve(responseData);
- // }, (error) => {
- // reject(error);
- // });
- return new Promise((resolve, reject) => {
- if (GameGlobal.BuildingManager.networkRooms && GameGlobal.BuildingManager.networkRooms.length > 0) {
- resolve(GameGlobal.BuildingManager.networkRooms);
- } else {
- reject('error');
- }
- })
- // if (GameGlobal.BuildingManager.networkRooms && GameGlobal.BuildingManager.networkRooms.length > 0) {
- // success(GameGlobal.BuildingManager.networkRooms);
- // } else {
- // fail('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 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) {
- 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);
- },
- //获取房间里程碑
- getRoomAward(responseData) {
- this.buildingInfos.forEach((item, index) => {
- if (item.roomId == responseData.roomId) {
- item.awardCount = responseData.awardCount;
- item.roomMt = responseData.roomMt;
- }
- });
- this.refreshAllbuildingGoldRate();
- },
- // update (dt) {},
- });
|