UserPack.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. var {GameNotificationKey} = require('../utils/GameEnum');
  2. const PackApi = require('../net/PackApi');
  3. const GameModule = require('../utils/GameModule');
  4. cc.Class({
  5. extends: cc.Component,
  6. properties: {
  7. packContentNode: cc.Node,
  8. packPagePrefab: cc.Prefab,
  9. pageView: cc.PageView,
  10. indicatorLayout: cc.Layout,
  11. indicatorPrefab: cc.Prefab,
  12. //装备卡片节点
  13. propDescNode: cc.Node,
  14. itemNameLabel: cc.Label,
  15. itemDescRichText: cc.RichText,
  16. equipButton: cc.Button,
  17. },
  18. // LIFE-CYCLE CALLBACKS:
  19. onLoad () {
  20. this.pageViewSize = this.pageView.node._contentSize;
  21. this.pageView.node.on('scrolling', this.pageScrolling, this);
  22. this.winSize = cc.view.getVisibleSize();
  23. if (this.winSize.height <= 1100) {
  24. this.packContentNode.getComponent(cc.Widget).top = 70;
  25. this.packContentNode.height = 984;
  26. }
  27. this.setEventListener();
  28. },
  29. setEventListener() {
  30. GameEvent.on(GameNotificationKey.ShowPropDesc, this, (position, cardInfo) => {
  31. this.showPropDesc(position, cardInfo);
  32. });
  33. GameEvent.on(GameNotificationKey.HidePropDesc, this, (position) => {
  34. this.hidePropDesc();
  35. });
  36. GameModule.homeGuide.on('Fire_state37', this.equipCardAction, this);
  37. },
  38. start () {
  39. },
  40. /*
  41. * 打开背包
  42. * @param justOpen [Bool] justOpen是否只打开背包
  43. * */
  44. init(justOpen,uid) {
  45. this.uid = uid;
  46. this.justOpen = justOpen;
  47. this.node.parent = cc.find("Canvas");
  48. this.node.setContentSize(cc.view.getVisibleSize());
  49. this.node.active = false;
  50. if (justOpen == true) {
  51. this.equipButton.node.active = false;
  52. } else {
  53. this.equipButton.node.active = true;
  54. }
  55. this.loadUserPackInfo();
  56. },
  57. onDisable() {
  58. this.destoryData();
  59. this.propDescNode.active = false;
  60. this.equipButton.enabled = true;
  61. },
  62. //读取背包信息
  63. loadUserPackInfo() {
  64. PackApi.getUserPackInfo((responseData) => {
  65. // console.log("responseData: " + JSON.stringify(responseData));
  66. this.loadPackData(responseData.list);
  67. }, (error) => {
  68. console.log('userinformation error' + error);
  69. this.setupEmptyPack();
  70. });
  71. },
  72. destoryData() {
  73. for (let child of this.pageView.content.children) {
  74. if (cc.isValid(child)) {
  75. child.destroy();
  76. }
  77. }
  78. for (let child of this.indicatorLayout.node.children) {
  79. if (cc.isValid(child)) {
  80. child.destroy();
  81. }
  82. }
  83. this.pageView.removeAllPages();
  84. },
  85. showPack() {
  86. this.node.zIndex += 1;
  87. this.node.active = true;
  88. },
  89. setupEmptyPack() {
  90. this.destoryData();
  91. for (var i = 0; i < 1; i++) {
  92. let item = cc.instantiate(this.packPagePrefab);
  93. item = item.getComponent('UserPackPage');
  94. item.init(i);
  95. item.node.setContentSize(this.pageViewSize);
  96. this.pageView.addPage(item.node);
  97. let indicatorItem = cc.instantiate(this.indicatorPrefab);
  98. this.indicatorLayout.node.addChild(indicatorItem);
  99. let indicatorItemMng = indicatorItem.getComponent('UserPackIndicator');
  100. indicatorItemMng.pageLabel.string = (i + 1).toString();
  101. indicatorItemMng.indicatorIndex = i;
  102. if (i === 0) {
  103. indicatorItemMng.isSelected = true;
  104. }
  105. }
  106. },
  107. loadPackData(list) {
  108. if (list && list.length > 0) {
  109. this.destoryData();
  110. this.list = list;
  111. let page = Math.ceil(this.list.length / 20.0);
  112. for (var i = 0; i < page; i++) {
  113. let array = this.list.slice(i,((i+1) * 20));
  114. let item = cc.instantiate(this.packPagePrefab);
  115. item = item.getComponent('UserPackPage');
  116. item.init(array,i);
  117. item.node.setContentSize(this.pageViewSize);
  118. this.pageView.addPage(item.node);
  119. let indicatorItem = cc.instantiate(this.indicatorPrefab);
  120. this.indicatorLayout.node.addChild(indicatorItem);
  121. let indicatorItemMng = indicatorItem.getComponent('UserPackIndicator');
  122. indicatorItemMng.pageLabel.string = (i + 1).toString();
  123. indicatorItemMng.indicatorIndex = i;
  124. if (i === 0) {
  125. indicatorItemMng.isSelected = true;
  126. }
  127. }
  128. if (!this.justOpen) {
  129. GameModule.homeGuide.getComponent('HomeGuide').handleGuideStateNext('state32', 'state33');
  130. }
  131. } else {
  132. this.setupEmptyPack();
  133. }
  134. },
  135. closeNodeAction() {
  136. this.node.active = false;
  137. },
  138. pageScrolling() {
  139. if (this.propDescNode.active == true) {
  140. this.propDescNode.active = false;
  141. GameEvent.fire(GameNotificationKey.RefreshPackItem);
  142. }
  143. },
  144. pageScrollEnd() {
  145. let selectedIndex = this.pageView.getCurrentPageIndex();
  146. for (let child of this.indicatorLayout.node.children) {
  147. if (child.getComponent('UserPackIndicator') != undefined) {
  148. let indicatorItemMng = child.getComponent('UserPackIndicator');
  149. if (selectedIndex == indicatorItemMng.indicatorIndex) {
  150. indicatorItemMng.isSelected = true;
  151. } else {
  152. indicatorItemMng.isSelected = false;
  153. }
  154. }
  155. }
  156. },
  157. showPropDesc(position,cardInfo) {
  158. this.equipButton.enabled = true;
  159. this.cardInfo = cardInfo;
  160. var positionY = (-position.y + 210);
  161. if (this.winSize.height <= 1100 && -position.y >= 200) {
  162. positionY = -position.y + 120;
  163. }
  164. let position2 = cc.p(-position.x, positionY);
  165. this.propDescNode.setPosition(position2);
  166. this.propDescNode.active = true;
  167. this.itemNameLabel.string = cardInfo.name;
  168. var validTime = '永久';
  169. if (cardInfo.validTime > 0) {
  170. validTime = DWTool.calculateTime(cardInfo.validTime / 1000);
  171. }
  172. this.itemDescRichText.string = cardInfo.funcInfo + '\n<color=#F01A26>' + validTime +'</color>';
  173. if (this.justOpen == false) {
  174. if (cardInfo.id > 0 && cardInfo.defend) {
  175. this.equipButton.node.active = true;
  176. } else {
  177. this.equipButton.node.active = false;
  178. }
  179. }
  180. },
  181. hidePropDesc() {
  182. this.propDescNode.active = false;
  183. },
  184. // 装备卡片
  185. equipCardAction() {
  186. this.equipButton.enabled = false;
  187. this.postInsertCard();
  188. },
  189. postInsertCard() {
  190. PackApi.postInsertCard(this.uid, this.cardInfo.id, (responseData) => {
  191. // console.log("insert card: " + JSON.stringify(responseData));
  192. this.insertCardSuccess();
  193. }, (error) => {
  194. console.log('insert card error' + error);
  195. this.equipButton.enabled = true;
  196. });
  197. },
  198. insertCardSuccess() {
  199. this.equipButton.enabled = true;
  200. this.closeNodeAction();
  201. this.propDescNode.active = false;
  202. GameEvent.fire(GameNotificationKey.InsertCardToUser,this.cardInfo);
  203. GameEvent.fire(GameNotificationKey.RefreshInsertCardsInfo,this.cardInfo,true);
  204. // GameEvent.fire(GameNotificationKey.RefreshPackInfo,this.cardInfo.id,false);
  205. GameModule.homeGuide.getComponent('HomeGuide').handleGuideStateNext('state37', 'state34');
  206. },
  207. //刷新背包内容
  208. refreshPackInfo(itemId,isAdd) {
  209. this.destoryData();
  210. for(var index in this.list) {
  211. let item = this.list[index];
  212. if (item.itemId == itemId) {
  213. if (isAdd) {
  214. if (item.count >= 999) {
  215. let newItem = new Object();
  216. newItem.itemId = itemId;
  217. newItem.count = 1;
  218. this.list.splice((index + 1), 0, newItem);
  219. } else {
  220. item.count += 1;
  221. }
  222. } else {
  223. if (item.count == 1) {
  224. this.list.splice(index,1);
  225. } else {
  226. item.count -= 1;
  227. }
  228. }
  229. break;
  230. }
  231. }
  232. let page = Math.ceil(this.list.length / 20.0);
  233. for (var i = 0; i < page; i++) {
  234. let array = this.list.slice(i,((i+1) * 20));
  235. let item = cc.instantiate(this.packPagePrefab);
  236. item = item.getComponent('UserPackPage');
  237. item.init(array,i);
  238. item.node.setContentSize(this.pageViewSize);
  239. this.pageView.addPage(item.node);
  240. let indicatorItem = cc.instantiate(this.indicatorPrefab);
  241. this.indicatorLayout.node.addChild(indicatorItem);
  242. let indicatorItemMng = indicatorItem.getComponent('UserPackIndicator');
  243. indicatorItemMng.pageLabel.string = (i + 1).toString();
  244. indicatorItemMng.indicatorIndex = i;
  245. if (i === 0) {
  246. indicatorItemMng.isSelected = true;
  247. }
  248. }
  249. },
  250. // update (dt) {},
  251. });