LevelFriendHome.js 5.3 KB

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