1
0

UserPack.js 9.1 KB

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