LevelHome.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. },
  13. /**
  14. * home 初始化方法, 所有的初始化操作在这里操作, 必须在加入父节点之前调用
  15. * */
  16. init(uid, cityId) {
  17. this.uid = uid;
  18. this.cityId = cityId;
  19. this.buildingInfos = [];
  20. this.node.parent = cc.find("Canvas/game");
  21. this.refreshTheme();
  22. this.getNetworkData();
  23. },
  24. refreshTheme() {
  25. if (this.buildings) {
  26. for (let i = 0; i < this.buildings.length; i++) {
  27. let itemScript = this.buildings[i];
  28. itemScript.init(this.cityId, i + 1);
  29. }
  30. }
  31. if (this.bottomScript) {
  32. this.bottomScript.init(this.cityId);
  33. }
  34. },
  35. // LIFE-CYCLE CALLBACKS:
  36. onLoad() {
  37. this.buildings = [];
  38. this.matchScreenSize();
  39. let topNode = cc.instantiate(this.levelHomeTop);
  40. this.scrollView.content.addChild(topNode);
  41. for (let i = 0; i < 5; i++) {
  42. let item = cc.instantiate(this.levelHomeItem);
  43. let itemScript = item.getComponent('LevelHomeItem');
  44. itemScript.init(this.cityId, i + 1);
  45. this.scrollView.content.addChild(item);
  46. this.buildings.push(itemScript);
  47. }
  48. let bottomNode = cc.instantiate(this.levelHomeBottom);
  49. this.bottomScript = bottomNode.getComponent('LevelHomeBottom'); this.bottomScript.init(this.cityId);
  50. this.scrollView.content.addChild(bottomNode);
  51. this.scrollView.node.on("scrolling", (event) => {
  52. if (this.scrollView._isOutOfBoundary()) {
  53. if (this.scrollView._outOfBoundaryAmount.y > 0) { // 超出上面的界限
  54. this.scrollView._outOfBoundaryAmount.y = 0;
  55. this.scrollView._adjustContentOutOfBoundary();
  56. } else { // 超出下面的界限
  57. if (this.scrollView._outOfBoundaryAmount.y < this.minContentPosition) {
  58. if (this.recordScrollViewPosition) {
  59. this.scrollView.content.setPosition(this.recordScrollViewPosition);
  60. return;
  61. } else {
  62. this.recordScrollViewPosition = this.scrollView.getContentPosition();
  63. }
  64. } else {
  65. this.recordScrollViewPosition = null;
  66. }
  67. }
  68. }
  69. }, this);
  70. },
  71. /**
  72. * 适配不同高度的屏幕尺寸
  73. */
  74. matchScreenSize() {
  75. let initHeight = 1624;
  76. let vsize = cc.view.getVisibleSize()
  77. let paddingY = (initHeight - vsize.height) / 2;
  78. if (paddingY < 0) {
  79. paddingY = 0;
  80. }
  81. this.scrollView.content.getComponent(cc.Layout).paddingTop = paddingY;
  82. // 底部加多一块100px的内边距, 让底楼不要完全显示出来
  83. this.scrollView.content.getComponent(cc.Layout).paddingBottom = paddingY - 100;
  84. },
  85. // 用来访问自己家园时, 重置scrollView位置
  86. start() {
  87. this.scrollView.scrollToBottom(0.0);
  88. },
  89. // 用来访问好友家园时, 重置scrollView位置
  90. onEnable() {
  91. },
  92. getNetworkData(callback) {
  93. // 获取目标用户的建筑
  94. HomeApi.getUserBuildings(this.uid, this.cityId, (responseData) => {
  95. let reverseArray = responseData.buildings.reverse();
  96. reverseArray.map((value, index, array) => {
  97. let model = Global.BuildingManager.getBuildingInfo(this.cityId, value.buildingId, value.level)
  98. if (model.isFull() && this.cityId === Global.cityId) {
  99. try {
  100. GameEvent.fire(GameNotificationKey.LevelHomeItemBuildingFull);
  101. } catch (error) {
  102. console.log(error);
  103. }
  104. }
  105. model.coinCount = value.coinCount;
  106. model.artists = value.artists || [];
  107. this.buildingInfos.push(model);
  108. });
  109. GameModule.userInfo.setUserInfo(responseData.user);
  110. GameModule.userInfo.setGrossIncomeAndStars(responseData.grossIncome, responseData.stars);
  111. callback && callback();
  112. // 开始设置建筑
  113. this.configBuildings();
  114. }, (error) => {
  115. console.log("error: " + error);
  116. });
  117. },
  118. configBuildings() {
  119. for (let i = 0; i < this.buildings.length; i++) {
  120. let itemScript = this.buildings[i];
  121. itemScript.config(this.buildingInfos[i], this.uid);
  122. }
  123. },
  124. });