LevelFriendHome.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. const HomeApi = require("../net/HomeApi");
  2. const Api = require('../net/Api');
  3. const GameModule = require("../utils/GameModule");
  4. cc.Class({
  5. extends: cc.Component,
  6. properties: {
  7. scrollView: cc.ScrollView,
  8. levelFriendHomeItem: cc.Prefab,
  9. levelHomeTop: cc.Prefab,
  10. levelHomeBottom: cc.Prefab,
  11. minContentPosition: -150,
  12. },
  13. /**
  14. * 初始化好友家园
  15. * @param {Number} uid 用户id
  16. * @param {Function} callback 初始化完成之后的回调函数
  17. */
  18. initFriend(uid, callback) {
  19. this.uid = uid;
  20. this.buildingInfos = [];
  21. this.node.parent = cc.find("Canvas/game");
  22. this.scrollView.scrollToTop(0);
  23. this.getNetworkData(callback);
  24. },
  25. refreshTheme() {
  26. if (this.topScript) {
  27. this.topScript.initFriend(this.cityId);
  28. }
  29. if (this.buildings) {
  30. for (let i = 0; i < this.buildings.length; i++) {
  31. let itemScript = this.buildings[i];
  32. itemScript.init(this.cityId, i + 1);
  33. }
  34. }
  35. if (this.bottomScript) {
  36. this.bottomScript.init(this.cityId);
  37. }
  38. },
  39. reInitFriend(callback) {
  40. this.buildingInfos = [];
  41. this.getNetworkData(callback);
  42. },
  43. getNetworkData(callback) {
  44. // 获取目标用户的建筑
  45. HomeApi.getUserBuildings(this.uid, 0, (responseData) => {
  46. let sortArray = responseData.buildings.sort((a, b) => {
  47. return a.buildingId < b.buildingId;
  48. });
  49. this.cityId = sortArray[0].cityId;
  50. sortArray.map((value, index, array) => {
  51. let model = Global.BuildingManager.getBuildingInfo(this.cityId, value.buildingId, value.level)
  52. model.coinCount = value.coinCount;
  53. model.artists = value.artists || [];
  54. this.buildingInfos.push(model);
  55. });
  56. GameModule.friendInfo.userNick.string = responseData.user.nick;
  57. GameModule.friendInfo.userStars.string = responseData.stars;
  58. Api.createImageFromUrl(responseData.user.head, (spriteFrame) => {
  59. GameModule.friendInfo.userHead.spriteFrame = spriteFrame;
  60. }, null);
  61. callback && callback(responseData)
  62. this.scrollView.scrollToBottom(2.0);
  63. // 开始设置建筑
  64. this.configBuildings();
  65. this.refreshTheme();
  66. }, (error) => {
  67. console.log("error: " + error);
  68. });
  69. },
  70. // LIFE-CYCLE CALLBACKS:
  71. onLoad() {
  72. this.buildings = [];
  73. this.matchScreenSize();
  74. let topNode = cc.instantiate(this.levelHomeTop);
  75. this.topScript = topNode.getComponent('LevelHomeTop');
  76. this.topScript.initFriend(this.cityId);
  77. this.scrollView.content.addChild(topNode);
  78. for (let i = 0; i < 5; i++) {
  79. let item = cc.instantiate(this.levelFriendHomeItem);
  80. let itemScript = item.getComponent('LevelFriendHomeItem');
  81. itemScript.init(this.cityId, i + 1);
  82. this.scrollView.content.addChild(item);
  83. this.buildings.push(itemScript);
  84. }
  85. let bottomNode = cc.instantiate(this.levelHomeBottom);
  86. this.bottomScript = bottomNode.getComponent('LevelHomeBottom'); this.bottomScript.init(this.cityId);
  87. this.scrollView.content.addChild(bottomNode);
  88. this.scrollView.node.on("scrolling", (event) => {
  89. if (this.scrollView._isOutOfBoundary()) {
  90. if (this.scrollView._outOfBoundaryAmount.y > 0) { // 超出上面的界限
  91. this.scrollView._outOfBoundaryAmount.y = 0;
  92. this.scrollView._adjustContentOutOfBoundary();
  93. } else { // 超出下面的界限
  94. if (this.scrollView._outOfBoundaryAmount.y < this.minContentPosition) {
  95. if (this.recordScrollViewPosition) {
  96. this.scrollView.content.setPosition(this.recordScrollViewPosition);
  97. return;
  98. } else {
  99. this.recordScrollViewPosition = this.scrollView.getContentPosition();
  100. }
  101. } else {
  102. this.recordScrollViewPosition = null;
  103. }
  104. }
  105. }
  106. }, this);
  107. },
  108. /**
  109. * 适配不同高度的屏幕尺寸
  110. */
  111. matchScreenSize() {
  112. let initHeight = 1624;
  113. let vsize = cc.view.getVisibleSize()
  114. let paddingY = (initHeight - vsize.height) / 2;
  115. if (paddingY < 0) {
  116. paddingY = 0;
  117. }
  118. this.scrollView.content.getComponent(cc.Layout).paddingTop = paddingY;
  119. // 底部加多一块100px的内边距, 让底楼不要完全显示出来
  120. this.scrollView.content.getComponent(cc.Layout).paddingBottom = paddingY - 100;
  121. },
  122. configBuildings() {
  123. for (let i = 0; i < this.buildings.length; i++) {
  124. let itemScript = this.buildings[i];
  125. itemScript.config(this.buildingInfos[i], this.uid);
  126. }
  127. },
  128. });