LevelHome.js 7.9 KB

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