LevelHomeListAdapter.js 9.5 KB

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