LevelFriendHome.js 5.3 KB

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