LevelHome.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. showOffLineUI: 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.scrollView.scrollToBottom();
  25. this.refreshTheme();
  26. this.getNetworkData();
  27. },
  28. refreshTheme() {
  29. if (this.topScript) {
  30. this.topScript.init(this.cityId);
  31. }
  32. if (this.buildings) {
  33. for (let i = 0; i < this.buildings.length; i++) {
  34. let itemScript = this.buildings[i];
  35. itemScript.init(this.cityId, i + 1);
  36. }
  37. }
  38. if (this.bottomScript) {
  39. this.bottomScript.init(this.cityId);
  40. }
  41. },
  42. // LIFE-CYCLE CALLBACKS:
  43. onLoad() {
  44. this.buildings = [];
  45. this.unlockBuilding = [];
  46. this.matchScreenSize();
  47. let topNode = cc.instantiate(this.levelHomeTop);
  48. this.topScript = topNode.getComponent('LevelHomeTop');
  49. this.topScript.init(this.cityId);
  50. this.scrollView.content.addChild(topNode);
  51. for (let i = 0; i < 5; i++) {
  52. let item = cc.instantiate(this.levelHomeItem);
  53. let itemScript = item.getComponent('LevelHomeItem');
  54. itemScript.init(this.cityId, i + 1);
  55. this.scrollView.content.addChild(item);
  56. this.buildings.push(itemScript);
  57. }
  58. let bottomNode = cc.instantiate(this.levelHomeBottom);
  59. this.bottomScript = bottomNode.getComponent('LevelHomeBottom'); this.bottomScript.init(this.cityId);
  60. this.scrollView.content.addChild(bottomNode);
  61. this.scrollView.node.on("scrolling", (event) => {
  62. if (this.scrollView._isOutOfBoundary()) {
  63. if (this.scrollView._outOfBoundaryAmount.y > 0) { // 超出上面的界限
  64. this.scrollView._outOfBoundaryAmount.y = 0;
  65. this.scrollView._adjustContentOutOfBoundary();
  66. } else { // 超出下面的界限
  67. if (this.scrollView._outOfBoundaryAmount.y < this.minContentPosition) {
  68. if (this.recordScrollViewPosition) {
  69. this.scrollView.content.setPosition(this.recordScrollViewPosition);
  70. return;
  71. } else {
  72. this.recordScrollViewPosition = this.scrollView.getContentPosition();
  73. }
  74. } else {
  75. this.recordScrollViewPosition = null;
  76. }
  77. }
  78. }
  79. }, this);
  80. GameEvent.on(GameNotificationKey.showCatFlyAnimation, this, () => {
  81. this.scrollView.scrollToTop(0.2);
  82. });
  83. GameEvent.on(GameNotificationKey.ReloadLevelHomeData, this, () => {
  84. this.refreshTheme();
  85. this.getNetworkData();
  86. });
  87. GameEvent.on(GameNotificationKey.ResetLevelHomePaddingBottom, this, () => {
  88. this.resetPaddingBottom();
  89. })
  90. // 监听小游戏隐藏到后台事件
  91. if (CC_WECHATGAME) {
  92. wx.onHide(() => {
  93. this.wxHide = true
  94. // 离线收益计算重置
  95. this.showOffLineUI = true
  96. let lastTime = new Date().getTime()
  97. cc.sys.localStorage.setItem('offlineLastTime', lastTime);
  98. // 退出前立刻调用一次上报
  99. GameModule.userInfo.doReport()
  100. })
  101. wx.onShow(() => {
  102. if (this.wxHide) {
  103. this.refreshTheme();
  104. this.getNetworkData();
  105. this.wxHide = false
  106. }
  107. })
  108. }
  109. },
  110. /**
  111. * 适配不同高度的屏幕尺寸
  112. */
  113. matchScreenSize() {
  114. let initHeight = 1624;
  115. let vsize = cc.view.getVisibleSize()
  116. let paddingY = (initHeight - vsize.height) / 2;
  117. if (paddingY < 0) {
  118. paddingY = 0;
  119. }
  120. this.scrollView.content.getComponent(cc.Layout).paddingTop = paddingY;
  121. this.scrollView.elastic = false;
  122. // 底部加多一块100px的内边距, 让底楼不要完全显示出来
  123. this.scrollView.content.getComponent(cc.Layout).paddingBottom = paddingY - 100;
  124. },
  125. resetPaddingBottom() {
  126. let initHeight = 1624;
  127. let vsize = cc.view.getVisibleSize()
  128. let paddingY = (initHeight - vsize.height) / 2;
  129. if (GameModule.userInfo.stars >= 20) {
  130. this.scrollView.elastic = true;
  131. // 底部加多一块100px的内边距, 让底楼不要完全显示出来
  132. this.scrollView.content.getComponent(cc.Layout).paddingBottom = paddingY - 100;
  133. } else {
  134. this.scrollView.elastic = false;
  135. // 底部加多一块100px的内边距, 让底楼不要完全显示出来
  136. this.scrollView.content.getComponent(cc.Layout).paddingBottom = paddingY - 275;
  137. }
  138. this.scrollView.scrollToBottom(0);
  139. },
  140. // 用来访问自己家园时, 重置scrollView位置
  141. start() {
  142. this.scrollView.scrollToBottom(0.0);
  143. },
  144. // 用来访问好友家园时, 重置scrollView位置
  145. onEnable() {
  146. },
  147. getNetworkData(callback) {
  148. // 获取目标用户的建筑
  149. HomeApi.getUserBuildings(this.uid, this.cityId, (responseData) => {
  150. // 清空数据
  151. this.buildingInfos = [];
  152. // 满级后去别的城市就置零
  153. GameModule.userInfo.levelHomeItemFullCount = 0;
  154. let sortArray = responseData.buildings.sort((a, b) => {
  155. return a.buildingId < b.buildingId;
  156. });
  157. // 离线收益金币数量
  158. let offlineGrossIncome = 0;
  159. sortArray.map((value, index, array) => {
  160. let model = Global.BuildingManager.getBuildingInfo(this.cityId, value.buildingId, value.level)
  161. this._unlockBuilding[index] = model.isUnlocked ? 1 : 0;
  162. if (model.isFull() && this.cityId === Global.devCityId) {
  163. try {
  164. GameEvent.fire(GameNotificationKey.LevelHomeItemBuildingFull);
  165. } catch (error) {
  166. console.log(error);
  167. }
  168. }
  169. model.coinCount = value.coinCount;
  170. model.artists = value.artists || [];
  171. if (model.artists.length > 0) { // 有艺人入驻的情况
  172. let addition = 0;
  173. for (let i = 0; i < model.artists.length; i++) {
  174. let artist = model.artists[i];
  175. addition += artist.stationJobLevel + 1;
  176. }
  177. offlineGrossIncome += (model.coinCount * (model.rate * addition));
  178. } else { // 无艺人入驻时
  179. offlineGrossIncome += (model.coinCount * model.rate);
  180. }
  181. this.buildingInfos.push(model);
  182. });
  183. this._unlockBuilding = this._unlockBuilding.reverse();
  184. // GameModule.userInfo.setUserInfo(responseData.user);
  185. // this.resetPaddingBottom();
  186. callback && callback();
  187. // 开始设置建筑
  188. this.configBuildings();
  189. // 离线收益处理
  190. this.configOffIncome(sortArray, offlineGrossIncome)
  191. }, (error) => {
  192. console.log("error: " + error);
  193. });
  194. },
  195. configBuildings() {
  196. for (let i = 0; i < this.buildings.length; i++) {
  197. let itemScript = this.buildings[i];
  198. itemScript.config(this.buildingInfos[i], this.uid, this._unlockBuilding, (newBuildingInfo) => {
  199. this.buildingInfos[i] = newBuildingInfo;
  200. });
  201. }
  202. this.randomResidentTip();
  203. },
  204. /**
  205. * 离线收益处理
  206. * @param {Array} sortArray 用户当前城市buildingInfos数组
  207. * @param {Number} offlineGrossIncome 离线收益金币数量
  208. */
  209. configOffIncome(sortArray, offlineGrossIncome) {
  210. let lastTime = cc.sys.localStorage.getItem('offlineLastTime');
  211. if (!lastTime) {
  212. lastTime = new Date().getTime()
  213. cc.sys.localStorage.setItem('offlineLastTime', lastTime);
  214. } else {
  215. let curTime = new Date().getTime()
  216. cc.sys.localStorage.setItem('offlineLastTime', curTime);
  217. // 显示离线收益的条件:
  218. // 1. 离上一次显示超过5分钟
  219. // 2. 用户解锁星数大于20
  220. // 3. 当前城市已解锁建筑数大于5
  221. let unLockStatus1 = curTime - lastTime > 300 * 1000
  222. let unLockStatus2 = GameModule.userInfo.stars > 20
  223. let unLockStatus3 = (() => {
  224. let flag = true
  225. sortArray.forEach(n => {
  226. if (n.level == 0) {
  227. flag = false
  228. }
  229. })
  230. return flag
  231. })()
  232. // showOffLineUI: 用户每次进入游戏离线收益只会显示1次
  233. if (this.showOffLineUI && unLockStatus1 && unLockStatus2 && unLockStatus3) {
  234. this.showOffLineUI = false;
  235. AlertManager.showOfflineGrossIncome(offlineGrossIncome);
  236. }
  237. }
  238. },
  239. randomResidentTip() {
  240. if (this.buildings && this.buildings.length > 0) {
  241. let noArtistBuildings = [];
  242. for (let i = 0; i < this.buildings.length; i++) {
  243. let itemScript = this.buildings[i];
  244. let buildingInfo = this.buildingInfos[i];
  245. if (buildingInfo.artists == undefined || buildingInfo.artists.length === 0) {
  246. noArtistBuildings.push(itemScript);
  247. } else {
  248. itemScript.setResidentTip(false);
  249. }
  250. }
  251. if (noArtistBuildings.length > 0) {
  252. let index = Math.floor(Math.random() * noArtistBuildings.length);
  253. for (let i = 0; i < noArtistBuildings.length; i++) {
  254. noArtistBuildings[i].setResidentTip(i == index);
  255. }
  256. clearInterval(this.residentTipHideTimer);
  257. clearInterval(this.residentTipShowTimer);
  258. this.residentTipHideTimer = setInterval(() => {
  259. noArtistBuildings[index].setResidentTip(false);
  260. }, 5000);
  261. this.residentTipShowTimer = setInterval(() => {
  262. this.randomResidentTip(false);
  263. }, 15000);
  264. }
  265. }
  266. },
  267. onDestroy() {
  268. if (this.residentTipHideTimer) {
  269. clearInterval(this.residentTipHideTimer);
  270. clearInterval(this.residentTipShowTimer);
  271. }
  272. GameEvent.off(GameNotificationKey.showCatFlyAnimation, this);
  273. },
  274. });