LevelHome.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. const HomeApi = require("../net/HomeApi");
  2. const GameModule = require("../utils/GameModule");
  3. const { GameNotificationKey } = require("../utils/GameEnum")
  4. cc.Class({
  5. extends: cc.Component,
  6. properties: {
  7. scrollView: cc.ScrollView,
  8. levelHomeItem: cc.Prefab,
  9. levelHomeTop: cc.Prefab,
  10. levelHomeBottom: cc.Prefab,
  11. minContentPosition: -150,
  12. _unlockBuilding: []
  13. },
  14. /**
  15. * home 初始化方法, 所有的初始化操作在这里操作, 必须在加入父节点之前调用
  16. * */
  17. init(uid, cityId) {
  18. this.uid = uid;
  19. this.cityId = cityId;
  20. this.buildingInfos = [];
  21. this.node.parent = cc.find("Canvas/game");
  22. this.refreshTheme();
  23. this.getNetworkData();
  24. },
  25. refreshTheme() {
  26. if (this.topScript) {
  27. this.topScript.init(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. // LIFE-CYCLE CALLBACKS:
  40. onLoad() {
  41. this.buildings = [];
  42. this.unlockBuilding = [];
  43. this.matchScreenSize();
  44. let topNode = cc.instantiate(this.levelHomeTop);
  45. this.topScript = topNode.getComponent('LevelHomeTop');
  46. this.topScript.init(this.cityId);
  47. this.scrollView.content.addChild(topNode);
  48. for (let i = 0; i < 5; i++) {
  49. let item = cc.instantiate(this.levelHomeItem);
  50. let itemScript = item.getComponent('LevelHomeItem');
  51. itemScript.init(this.cityId, i + 1);
  52. this.scrollView.content.addChild(item);
  53. this.buildings.push(itemScript);
  54. }
  55. let bottomNode = cc.instantiate(this.levelHomeBottom);
  56. this.bottomScript = bottomNode.getComponent('LevelHomeBottom'); this.bottomScript.init(this.cityId);
  57. this.scrollView.content.addChild(bottomNode);
  58. this.scrollView.node.on("scrolling", (event) => {
  59. if (this.scrollView._isOutOfBoundary()) {
  60. if (this.scrollView._outOfBoundaryAmount.y > 0) { // 超出上面的界限
  61. this.scrollView._outOfBoundaryAmount.y = 0;
  62. this.scrollView._adjustContentOutOfBoundary();
  63. } else { // 超出下面的界限
  64. if (this.scrollView._outOfBoundaryAmount.y < this.minContentPosition) {
  65. if (this.recordScrollViewPosition) {
  66. this.scrollView.content.setPosition(this.recordScrollViewPosition);
  67. return;
  68. } else {
  69. this.recordScrollViewPosition = this.scrollView.getContentPosition();
  70. }
  71. } else {
  72. this.recordScrollViewPosition = null;
  73. }
  74. }
  75. }
  76. }, this);
  77. GameEvent.on(GameNotificationKey.showCatFlyAnimation, this, () => {
  78. this.scrollView.scrollToTop(0.2);
  79. });
  80. GameEvent.on(GameNotificationKey.ReloadLevelHomeData, this, () => {
  81. this.refreshTheme();
  82. this.getNetworkData();
  83. });
  84. GameEvent.on(GameNotificationKey.ResetLevelHomePaddingBottom, this, () => {
  85. this.resetPaddingBottom();
  86. })
  87. },
  88. /**
  89. * 适配不同高度的屏幕尺寸
  90. */
  91. matchScreenSize() {
  92. let initHeight = 1624;
  93. let vsize = cc.view.getVisibleSize()
  94. let paddingY = (initHeight - vsize.height) / 2;
  95. if (paddingY < 0) {
  96. paddingY = 0;
  97. }
  98. this.scrollView.content.getComponent(cc.Layout).paddingTop = paddingY;
  99. this.scrollView.elastic = false;
  100. // 底部加多一块100px的内边距, 让底楼不要完全显示出来
  101. this.scrollView.content.getComponent(cc.Layout).paddingBottom = paddingY - 100;
  102. },
  103. resetPaddingBottom() {
  104. let initHeight = 1624;
  105. let vsize = cc.view.getVisibleSize()
  106. let paddingY = (initHeight - vsize.height) / 2;
  107. if (GameModule.userInfo.stars >= 20) {
  108. this.scrollView.elastic = true;
  109. // 底部加多一块100px的内边距, 让底楼不要完全显示出来
  110. this.scrollView.content.getComponent(cc.Layout).paddingBottom = paddingY - 100;
  111. } else {
  112. this.scrollView.elastic = false;
  113. // 底部加多一块100px的内边距, 让底楼不要完全显示出来
  114. this.scrollView.content.getComponent(cc.Layout).paddingBottom = paddingY - 275;
  115. }
  116. this.scrollView.scrollToBottom(0);
  117. },
  118. // 用来访问自己家园时, 重置scrollView位置
  119. start() {
  120. this.scrollView.scrollToBottom(0.0);
  121. },
  122. // 用来访问好友家园时, 重置scrollView位置
  123. onEnable() {
  124. },
  125. getNetworkData(callback) {
  126. // 获取目标用户的建筑
  127. HomeApi.getUserBuildings(this.uid, this.cityId, (responseData) => {
  128. // 清空数据
  129. this.buildingInfos = [];
  130. // 满级后去别的城市就置零
  131. GameModule.userInfo.levelHomeItemFullCount = 0;
  132. let sortArray = responseData.buildings.sort((a, b) => {
  133. return a.buildingId < b.buildingId;
  134. });
  135. sortArray.map((value, index, array) => {
  136. let model = Global.BuildingManager.getBuildingInfo(this.cityId, value.buildingId, value.level)
  137. this._unlockBuilding[index] = model.isUnlocked ? 1 : 0;
  138. if (model.isFull() && this.cityId === Global.devCityId) {
  139. try {
  140. GameEvent.fire(GameNotificationKey.LevelHomeItemBuildingFull);
  141. } catch (error) {
  142. console.log(error);
  143. }
  144. }
  145. model.coinCount = value.coinCount;
  146. model.artists = value.artists || [];
  147. this.buildingInfos.push(model);
  148. });
  149. this._unlockBuilding = this._unlockBuilding.reverse();
  150. GameModule.userInfo.setUserInfo(responseData.user);
  151. GameModule.userInfo.setGrossIncomeAndStars(responseData.grossIncome, responseData.user.stars);
  152. // this.resetPaddingBottom();
  153. callback && callback();
  154. // 开始设置建筑
  155. this.configBuildings();
  156. }, (error) => {
  157. console.log("error: " + error);
  158. });
  159. },
  160. configBuildings() {
  161. for (let i = 0; i < this.buildings.length; i++) {
  162. let itemScript = this.buildings[i];
  163. itemScript.config(this.buildingInfos[i], this.uid, this._unlockBuilding, (newBuildingInfo) => {
  164. this.buildingInfos[i] = newBuildingInfo;
  165. });
  166. }
  167. },
  168. onDestroy() {
  169. GameEvent.off(GameNotificationKey.showCatFlyAnimation, this);
  170. },
  171. });