LevelFriendHome.js 4.8 KB

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