LevelHomeListAdapter.js 9.4 KB

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