LevelHomeListAdapter.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. const ListAdapter = require('../utils/ListViewAdapter');
  2. const GameNotificationKey = require('../utils/GameEnum').GameNotificationKey;
  3. const GameModule = require("../utils/GameModule");
  4. cc.Class({
  5. extends: cc.Component,
  6. properties: {
  7. scrollView: cc.ScrollView,
  8. content: cc.Node,
  9. item: cc.Prefab,
  10. itemScriptName: 'LevelHomeItem',
  11. //
  12. topItem: cc.Prefab,
  13. bottomItem: cc.Prefab,
  14. },
  15. // LIFE-CYCLE CALLBACKS:
  16. onLoad () {
  17. this.isMoving = false;
  18. this.listAdapter = new ListAdapter(this.scrollView);
  19. this.listAdapter.shouldRemove = false;
  20. this._isTouch = false;
  21. this.setupEvent();
  22. },
  23. setupEvent() {
  24. this.scrollView.node.on(cc.Node.EventType.TOUCH_START, (event) => {
  25. //// 如果有购买商品 一秒钟点击十次
  26. if (!this._isTouch) {
  27. if (Global.isLongPressClick === true) {
  28. /// 最起码要按住1秒
  29. this._isTouch = true;
  30. this._addClick = false;
  31. this._timeCount = 0;
  32. this.schedule(this.timeAction, 1);
  33. }
  34. }
  35. });
  36. this.scrollView.node.on(cc.Node.EventType.TOUCH_END, (event) => {
  37. /// 防止多点点击
  38. if (this._isTouch) {
  39. if (Global.isLongPressClick === true && this._addClick) {
  40. let clickCount = 1 / GameModule.userInfo.secondClick;
  41. clickCount -= 10;
  42. GameModule.userInfo.secondClick = 1 / clickCount;
  43. this._addClick = false;
  44. } else {
  45. this._addClick = true;
  46. }
  47. this._isTouch = false;
  48. this.unschedule(this.timeAction, this);
  49. }
  50. if (this.isMoving) {
  51. this.isMoving = false;
  52. return;
  53. }
  54. this.clickAddMoney();
  55. }, this);
  56. this.scrollView.node.on(cc.Node.EventType.TOUCH_MOVE, (event) => {
  57. let x = event.getLocationX();
  58. let y = event.getLocationY();
  59. let startLocation = event.getStartLocation();
  60. if (Math.abs(x - startLocation.x) > 5 || Math.abs(y - startLocation.y) > 5) {
  61. this.isMoving = true;
  62. }
  63. }, this);
  64. GameEvent.on(GameNotificationKey.StarEnterRoom, this, (starId, roomId) => {
  65. this.refreshStarEnterRoom(starId, roomId);
  66. });
  67. //领取完教程26奖励后可以滚动
  68. GameModule.homeGuide.on('Fire_state26', () => {
  69. this.canScroll = true;
  70. this.scrollView.vertical = true;
  71. }, this);
  72. this.canScroll = true;
  73. this.scrollView.node.on("scroll-began", (event) => {
  74. if (!this.canScroll) {
  75. this.scrollView.vertical = false;
  76. }
  77. }, this);
  78. },
  79. clickAddMoney() {
  80. GameEvent.fire(GameNotificationKey.ClickAddMoney);
  81. },
  82. timeAction() {
  83. this._timeCount += 1;
  84. if (this._timeCount == 1) {
  85. if (!this._addClick) {
  86. let clickCount = 0;
  87. if (GameModule.userInfo.secondClick == 0) {
  88. clickCount = 10;
  89. } else {
  90. clickCount = 1 / GameModule.userInfo.secondClick;
  91. clickCount += 10;
  92. }
  93. GameModule.userInfo.secondClick = 1 / clickCount;
  94. this._addClick = true;
  95. }
  96. } else {
  97. GameModule.userInfo.clickCount += 10;
  98. }
  99. },
  100. start () {
  101. },
  102. configBuildings(dataList, isUnlock = false) {
  103. this.dataList = dataList;
  104. if (isUnlock) {
  105. this.listAdapter.addItemsToDataList(this.dataList[0],true);
  106. //上面的方法会重新计算cotent总高度,所以需要再加回底部的高度
  107. this.bottomItemNode.setPosition(0, -(this.bottomItemHeight * 0.5 + this.content.height));
  108. this.content.height += this.bottomItemHeight;
  109. if (GameModule.homeGuide.getComponent('HomeGuide').curState == 'state21' || GameModule.homeGuide.getComponent('HomeGuide').curState == 'state24') {
  110. this.scrollView.vertical = true;
  111. this.scrollView.scrollToBottom(0.0);
  112. this.canScroll = false;
  113. } else {
  114. this.canScroll = true;
  115. this.scrollView.scrollToOffset(this.scrollPosition, 0.0);
  116. }
  117. } else {
  118. this.scrollView.content.removeAllChildren();
  119. let top = cc.instantiate(this.topItem);
  120. this.topItemHeight = top.height; // get total content height
  121. this.content.addChild(top);
  122. top.setPosition(0, -(this.topItemHeight * 0.5));
  123. let topItemScript = top.getComponent('LevelHomeTop');
  124. topItemScript.updateItem();
  125. this.listAdapter.updateItems(this.dataList, this.item, this.itemScriptName, this.topItemHeight);
  126. this.bottomItemNode = cc.instantiate(this.bottomItem);
  127. this.bottomItemHeight = this.bottomItemNode.height; // get total content height
  128. this.content.addChild(this.bottomItemNode);
  129. this.bottomItemNode.setPosition(0, -(this.bottomItemHeight * 0.5 + this.content.height));
  130. let itemScript = this.bottomItemNode.getComponent('LevelHomeBottom');
  131. itemScript.updateItem();
  132. //上面的方法会重新计算cotent总高度,所以需要再加回底部的高度
  133. this.bottomItemNode.setPosition(0, -(this.bottomItemHeight * 0.5 + this.content.height));
  134. this.content.height += this.bottomItemHeight;
  135. this.scrollView.scrollToBottom(0.1);
  136. this.scrollPosition = this.scrollView.getContentPosition();
  137. }
  138. },
  139. configGuide() {
  140. if (GameModule.homeGuide.getComponent('HomeGuide').curState == 'state20') {
  141. this.canScroll = false;
  142. return;
  143. }
  144. if (GameModule.homeGuide.getComponent('HomeGuide').curState == 'state24') {
  145. this.canScroll = false;
  146. return;
  147. }
  148. if (!GameModule.homeGuide.getComponent('HomeGuide').isPassGuideState('state24')) {
  149. this.canScroll = false;
  150. }
  151. },
  152. refreshRoomData(roomInfo) {
  153. this.dataList.forEach((item, index) => {
  154. if (item.roomId == roomInfo.roomId) {
  155. this.dataList[index] = roomInfo;
  156. }
  157. });
  158. },
  159. refreshStarEnterRoom(starId, roomId) {
  160. GameModule.userInfo.refreshSecondText();
  161. GameEvent.fire(GameNotificationKey.UpBuildingLevel);
  162. if (roomId <= 0) {
  163. return;
  164. }
  165. for (let i = 0; i < this.dataList.length; ++i) {
  166. let room = this.dataList[i];
  167. if (room.roomId == roomId) {
  168. if (room.roomStars == undefined) {
  169. room.roomStars = [];
  170. }
  171. room.roomStars.push(starId);
  172. }
  173. }
  174. this.listAdapter.updateDataList(this.dataList, roomId);
  175. },
  176. // update (dt) {},
  177. onScrollEvent(sender, event) {
  178. this.listAdapter.update();
  179. },
  180. });