Pack.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import GoodItem from "../../common/GoodItem";
  2. import GoodAbout from "../../common/GoodAbout";
  3. import ViewObject from "../../../main/ViewObject";
  4. import { GameViewType } from "../../../main/ViewManage";
  5. import FoodView from "./FoodView";
  6. import { ReveData } from "../../../util/CHttp";
  7. /**
  8. * 背包
  9. */
  10. const { ccclass, property } = cc._decorator;
  11. @ccclass
  12. export default class Pack extends ViewObject {
  13. // /**
  14. // * 背包静态缓存
  15. // */
  16. // public static instance: Pack = null;
  17. /**
  18. * [0]:装备
  19. * [1]:道具
  20. * [2]:宝箱
  21. */
  22. @property([cc.Node])
  23. mContent: Array<cc.Node> = [];
  24. @property(cc.Prefab)
  25. mGoodItem: cc.Prefab = null;
  26. curInset: number = 0;
  27. public httpEvent(reveData: ReveData) {
  28. let data = reveData.data;
  29. if (data._equip != undefined) {
  30. this.flushEquip();
  31. }
  32. if (data._pack != undefined) {
  33. this.flushGood();
  34. }
  35. }
  36. public onLoad() {
  37. this.mContent[1].active = false;
  38. this.flush();
  39. }
  40. public flush() {
  41. this.flushGood();
  42. this.flushEquip();
  43. }
  44. /**
  45. * 刷新道具
  46. */
  47. public flushGood() {
  48. let pack = this.main.player.pack;
  49. let index = 0;
  50. let content = this.mContent[0];
  51. if (content.children.length) {
  52. content.removeAllChildren();
  53. }
  54. for (let id in pack) {
  55. let count = pack[id];
  56. if (count > 0) {
  57. let data = {
  58. id: id,
  59. count: count,
  60. };
  61. this.addGoodItem(content);
  62. let node = content.children[index];
  63. let goodItem = node.getComponent(GoodItem);
  64. goodItem.initGood(this.main, data);
  65. if (goodItem.good.type == 5 || goodItem.good.type == 3) {
  66. goodItem.setCallback((gi) => {
  67. this.openEatFood(gi);
  68. });
  69. } else {
  70. goodItem.setCallback((gi) => {
  71. this.openGoodAbout(gi);
  72. });
  73. }
  74. index++
  75. }
  76. }
  77. }
  78. /**
  79. * 刷新装备
  80. */
  81. public flushEquip() {
  82. // let equip = this.sortEquip();
  83. let equip = this.main.player.equip;
  84. let index = 0;
  85. let content = this.mContent[1];
  86. if (content.children.length) {
  87. content.removeAllChildren();
  88. }
  89. for (let i = 0; i < equip.length; i++) {
  90. const element = equip[i];
  91. this.addGoodItem(content);
  92. let node = content.children[index];
  93. let equipItem = node.getComponent(GoodItem);
  94. equipItem.initEquip(this.main, element);
  95. equipItem.node.zIndex = this.main.player.equip.length - equipItem.equip.id;
  96. equipItem.setCallback((gi) => {
  97. this.openGoodAbout(gi);
  98. });
  99. index++;
  100. }
  101. }
  102. sortEquip() {
  103. let equip = this.main.player.equip;
  104. equip.sort((a, b) => {
  105. return b.id - a.id
  106. })
  107. return equip
  108. }
  109. private addGoodItem(content: cc.Node) {
  110. let node = cc.instantiate(this.mGoodItem);
  111. node.parent = content;
  112. }
  113. private openGoodAbout(goodItem: GoodItem) {
  114. this.main.viewManage.loadFunc(GameViewType.goodAbout, (viewObject: ViewObject) => {
  115. let goodAbout = viewObject as GoodAbout;
  116. goodAbout.init(goodItem, false)
  117. viewObject.show();
  118. });
  119. }
  120. private openEatFood(goodItem: GoodItem) {
  121. this.main.viewManage.loadFunc(GameViewType.goodEat, (viewObject: ViewObject) => {
  122. let goodAbout = viewObject as FoodView;
  123. goodAbout.initGood(this, goodItem)
  124. viewObject.show();
  125. });
  126. }
  127. onClickInset(eventTouch: cc.Event.EventTouch, data: string) {
  128. this.curInset = Number(data);
  129. this.mContent.forEach((content, index) => {
  130. content.active = index == Number(data);
  131. })
  132. }
  133. }