LevelHomeListAdapter.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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_CANCEL, () => {
  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. });
  55. this.scrollView.node.on(cc.Node.EventType.TOUCH_END, (event) => {
  56. /// 防止多点点击
  57. if (this._isTouch) {
  58. if (Global.isLongPressClick === true && this._addClick) {
  59. let clickCount = 1 / GameModule.userInfo.secondClick;
  60. clickCount -= 10;
  61. GameModule.userInfo.secondClick = 1 / clickCount;
  62. this._addClick = false;
  63. } else {
  64. this._addClick = true;
  65. }
  66. this._isTouch = false;
  67. this.unschedule(this.timeAction, this);
  68. }
  69. if (this.isMoving) {
  70. this.isMoving = false;
  71. return;
  72. }
  73. this.clickAddMoney();
  74. }, this);
  75. this.scrollView.node.on(cc.Node.EventType.TOUCH_MOVE, (event) => {
  76. let x = event.getLocationX();
  77. let y = event.getLocationY();
  78. let startLocation = event.getStartLocation();
  79. if (Math.abs(x - startLocation.x) > 5 || Math.abs(y - startLocation.y) > 5) {
  80. this.isMoving = true;
  81. }
  82. }, this);
  83. GameEvent.on(GameNotificationKey.StarEnterRoom, this, (starId, roomId) => {
  84. this.refreshStarEnterRoom(starId, roomId);
  85. });
  86. GameEvent.on(GameNotificationKey.StarLeaveRoom, this, (starId, roomId) => {
  87. this.refreshStarLeaveRoom(starId, roomId);
  88. });
  89. GameEvent.on(GameNotificationKey.AllStarLeaveRoom, this, this.refreshAllStarLeaveRoom);
  90. //领取完教程26奖励后可以滚动
  91. GameModule.homeGuide.on('Fire_state26', () => {
  92. this.canScroll = true;
  93. this.scrollView.vertical = true;
  94. }, this);
  95. this.canScroll = true;
  96. this.scrollView.node.on("scroll-began", (event) => {
  97. if (!this.canScroll) {
  98. this.scrollView.vertical = false;
  99. }
  100. }, this);
  101. },
  102. clickAddMoney() {
  103. GameEvent.fire(GameNotificationKey.ClickAddMoney);
  104. },
  105. timeAction() {
  106. this._timeCount += 1;
  107. if (this._timeCount == 1) {
  108. if (!this._addClick) {
  109. let clickCount = 0;
  110. if (GameModule.userInfo.secondClick == 0) {
  111. clickCount = 10;
  112. } else {
  113. clickCount = 1 / GameModule.userInfo.secondClick;
  114. clickCount += 10;
  115. }
  116. GameModule.userInfo.secondClick = 1 / clickCount;
  117. this._addClick = true;
  118. }
  119. } else {
  120. GameModule.userInfo.clickCount += 10;
  121. }
  122. },
  123. start () {
  124. },
  125. configBuildings(dataList, isUnlock = false) {
  126. this.dataList = dataList;
  127. if (isUnlock) {
  128. this.listAdapter.addItemsToDataList(this.dataList[0],true);
  129. //上面的方法会重新计算cotent总高度,所以需要再加回底部的高度
  130. this.bottomItemNode.setPosition(0, -(this.bottomItemHeight * 0.5 + this.content.height));
  131. this.content.height += this.bottomItemHeight;
  132. if (GameModule.homeGuide.getComponent('HomeGuide').curState == 'state21' || GameModule.homeGuide.getComponent('HomeGuide').curState == 'state24') {
  133. this.scrollView.vertical = true;
  134. this.scrollView.scrollToBottom(0.0);
  135. this.canScroll = false;
  136. } else {
  137. this.canScroll = true;
  138. this.scrollView.scrollToOffset(this.scrollPosition, 0.0);
  139. }
  140. } else {
  141. this.scrollView.content.removeAllChildren();
  142. let top = cc.instantiate(this.topItem);
  143. this.topItemHeight = top.height; // get total content height
  144. this.content.addChild(top);
  145. top.setPosition(0, -(this.topItemHeight * 0.5));
  146. let topItemScript = top.getComponent('LevelHomeTop');
  147. topItemScript.updateItem();
  148. this.listAdapter.updateItems(this.dataList, this.item, this.itemScriptName, this.topItemHeight);
  149. this.bottomItemNode = cc.instantiate(this.bottomItem);
  150. this.bottomItemHeight = this.bottomItemNode.height; // get total content height
  151. this.content.addChild(this.bottomItemNode);
  152. this.bottomItemNode.setPosition(0, -(this.bottomItemHeight * 0.5 + this.content.height));
  153. let itemScript = this.bottomItemNode.getComponent('LevelHomeBottom');
  154. itemScript.updateItem();
  155. //上面的方法会重新计算cotent总高度,所以需要再加回底部的高度
  156. this.bottomItemNode.setPosition(0, -(this.bottomItemHeight * 0.5 + this.content.height));
  157. this.content.height += this.bottomItemHeight;
  158. this.scrollView.scrollToBottom(0.1);
  159. this.scrollPosition = this.scrollView.getContentPosition();
  160. }
  161. },
  162. configGuide() {
  163. if (GameModule.homeGuide.getComponent('HomeGuide').curState == 'state20') {
  164. this.canScroll = false;
  165. return;
  166. }
  167. if (GameModule.homeGuide.getComponent('HomeGuide').curState == 'state24') {
  168. this.canScroll = false;
  169. return;
  170. }
  171. if (!GameModule.homeGuide.getComponent('HomeGuide').isPassGuideState('state24')) {
  172. this.canScroll = false;
  173. }
  174. },
  175. refreshRoomData(roomInfo) {
  176. this.dataList.forEach((item, index) => {
  177. if (item.roomId == roomInfo.roomId) {
  178. this.dataList[index] = roomInfo;
  179. }
  180. });
  181. },
  182. refreshStarEnterRoom(starId, roomId) {
  183. GameModule.userInfo.refreshSecondText();
  184. GameEvent.fire(GameNotificationKey.UpBuildingLevel);
  185. if (roomId <= 0) {
  186. return;
  187. }
  188. for (let i = 0; i < this.dataList.length; ++i) {
  189. let room = this.dataList[i];
  190. if (room.roomId == roomId) {
  191. if (room.roomStars == undefined) {
  192. room.roomStars = [];
  193. }
  194. room.roomStars.push(starId);
  195. }
  196. }
  197. this.listAdapter.updateDataList(this.dataList, roomId);
  198. },
  199. refreshStarLeaveRoom(starId, roomId) {
  200. // GameModule.userInfo.refreshSecondText();
  201. // GameEvent.fire(GameNotificationKey.UpBuildingLevel);
  202. if (roomId <= 0) {
  203. return;
  204. }
  205. for (let i = 0; i < this.dataList.length; ++i) {
  206. let room = this.dataList[i];
  207. if (room.roomId == roomId) {
  208. if (room.roomStars != undefined) {
  209. for (let j = 0; j < room.roomStars.length; ++j) {
  210. let roomStarId = room.roomStars[j];
  211. if (roomStarId == starId) {
  212. room.roomStars.splice(j, 1);
  213. }
  214. }
  215. }
  216. }
  217. }
  218. this.listAdapter.updateDataList(this.dataList, roomId);
  219. },
  220. refreshAllStarLeaveRoom() {
  221. for (let i = 0; i < this.dataList.length; ++i) {
  222. let room = this.dataList[i];
  223. if (room.roomStars != undefined && room.roomStars.length > 0) {
  224. room.roomStars = [];
  225. }
  226. }
  227. this.listAdapter.refreshItems(this.dataList);
  228. },
  229. // update (dt) {},
  230. onScrollEvent(sender, event) {
  231. this.listAdapter.update();
  232. },
  233. });