123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- const HomeApi = require("../net/HomeApi");
- const Api = require('../net/Api');
- const GameModule = require("../utils/GameModule");
- cc.Class({
- extends: cc.Component,
- properties: {
- scrollView: cc.ScrollView,
- levelFriendHomeItem: cc.Prefab,
- levelHomeTop: cc.Prefab,
- levelHomeBottom: cc.Prefab,
- minContentPosition: -150,
- },
- /**
- * 初始化好友家园
- * @param {Number} uid 用户id
- * @param {Function} callback 初始化完成之后的回调函数
- */
- initFriend(uid, callback) {
- this.uid = uid;
- let target = Global.friendList.find(n => {
- return n.uid == uid
- })
-
- if(target) {
- this.nick = target.nick
-
- this.stars = target.stars
-
- this.head = target.head
- }
- this.buildingInfos = [];
- this.node.parent = cc.find("Canvas/game");
- this.scrollView.scrollToTop(0);
- this.getNetworkData(callback);
- },
- refreshTheme() {
-
- if (this.topScript) {
- this.topScript.initFriend(this.cityId);
- }
- if (this.buildings) {
- for (let i = 0; i < this.buildings.length; i++) {
- let itemScript = this.buildings[i];
- itemScript.init(this.cityId, i + 1);
- }
- }
- if (this.bottomScript) {
- this.bottomScript.init(this.cityId);
- }
- },
- reInitFriend(callback) {
- this.buildingInfos = [];
- this.getNetworkData(callback);
- },
- getNetworkData(callback) {
- // 获取目标用户的建筑
- HomeApi.getUserBuildings(this.uid, 0, (responseData) => {
- let sortArray = responseData.buildings.sort((a, b) => {
- return a.buildingId < b.buildingId;
- });
- this.cityId = sortArray[0].cityId;
- sortArray.map((value, index, array) => {
- let model = Global.BuildingManager.getBuildingInfo(this.cityId, value.buildingId, value.level)
- model.coinCount = value.coinCount;
- model.artists = value.artists || [];
- this.buildingInfos.push(model);
- });
- GameModule.friendInfo.userNick.string = this.nick;
- GameModule.friendInfo.userStars.string = this.stars;
- Api.createImageFromUrl(this.head, (spriteFrame) => {
- GameModule.friendInfo.userHead.spriteFrame = spriteFrame;
- }, null);
- callback && callback(responseData)
- this.scrollView.scrollToBottom(2.0);
- // 开始设置建筑
- this.configBuildings();
- this.refreshTheme();
- }, (error) => {
- console.log("error: " + error);
- });
- },
- // LIFE-CYCLE CALLBACKS:
- onLoad() {
- this.buildings = [];
- this.matchScreenSize();
- let topNode = cc.instantiate(this.levelHomeTop);
- this.topScript = topNode.getComponent('LevelHomeTop');
- this.topScript.initFriend(this.cityId);
- this.scrollView.content.addChild(topNode);
- for (let i = 0; i < 5; i++) {
- let item = cc.instantiate(this.levelFriendHomeItem);
- let itemScript = item.getComponent('LevelFriendHomeItem');
- itemScript.init(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.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);
- },
- /**
- * 适配不同高度的屏幕尺寸
- */
- 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;
- // 底部加多一块100px的内边距, 让底楼不要完全显示出来
- this.scrollView.content.getComponent(cc.Layout).paddingBottom = paddingY - 100;
- },
- configBuildings() {
- for (let i = 0; i < this.buildings.length; i++) {
- let itemScript = this.buildings[i];
- itemScript.config(this.buildingInfos[i], this.uid);
- }
- },
- });
|