EquipPack.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import Main from "../../../main/Main";
  2. import GoodItem from "../../common/GoodItem";
  3. import PetIcon from "../../common/PetIcon";
  4. import FFCalAttr from "../../data/FFCalAttr";
  5. import RedPoint from "../../data/RedPoint";
  6. import { __EquipData } from "../../data/sdata/SManage";
  7. import { EquipAttr } from "../../data/udata/Player";
  8. import Equip from "./Equip";
  9. /**
  10. * 装备背包
  11. */
  12. const {ccclass, property} = cc._decorator;
  13. @ccclass
  14. export default class EquipPack extends cc.Component {
  15. @property(cc.Node)
  16. mPagContent: cc.Node = null;
  17. @property(cc.ScrollView)
  18. mPagScrollView: cc.ScrollView = null;
  19. @property(cc.Prefab)
  20. mEquipItem: cc.Prefab = null;
  21. @property([cc.Node])
  22. mButtonItem: Array<cc.Node> = [];
  23. @property(cc.SpriteFrame)
  24. mCheckIcon: cc.SpriteFrame = null;
  25. @property(cc.SpriteFrame)
  26. mNoCheckIcon: cc.SpriteFrame = null;
  27. @property
  28. mCheckX = -282
  29. @property
  30. mNoCheckX = -267
  31. public main:Main;
  32. public petIcon:PetIcon;
  33. /**
  34. * 装备类型
  35. */
  36. public equipType:number = 1;
  37. public equip:Equip;
  38. onLoad(){
  39. this.equip = this.node.getComponent(Equip)
  40. this.main = this.equip.main;
  41. }
  42. /**
  43. * @param pi 穿装备的伙伴
  44. * @param equipItem 选择的装备
  45. */
  46. public init(pi:PetIcon){
  47. this.petIcon = pi;
  48. for (let i = 0; i < this.mButtonItem.length; i++) {
  49. const element = this.mButtonItem[i];
  50. RedPoint.removeRed(element)
  51. }
  52. this.setEquipType(this.equipType)
  53. this.flushRedPoint()
  54. }
  55. //根据每个类型刷新按钮红点
  56. public flushRedPoint(){
  57. let equip = this.main.player.equip
  58. for (let i = 0; i < equip.length; i++) {
  59. const element = equip[i];
  60. if (RedPoint.equipRedPoint(this.main, element, this.petIcon.data)) {
  61. let _equipA: __EquipData = this.main.sManage.getEquipById(element.id);
  62. if(_equipA.type < this.mButtonItem.length ){
  63. let btNode = this.mButtonItem[_equipA.type-1]
  64. RedPoint.addRed(this.main,btNode,true,true)
  65. }
  66. }
  67. }
  68. }
  69. public setEquipType(equipType:number){
  70. this.mPagScrollView.stopAutoScroll()
  71. this.mPagScrollView.scrollToTop()
  72. this.equipType = equipType;
  73. this.equip.curPage = equipType;
  74. this.flushPag()
  75. for (let i = 0; i < this.mButtonItem.length; i++) {
  76. const element = this.mButtonItem[i];
  77. let sprite = element.getComponent(cc.Sprite)
  78. if(i == this.equipType - 1){
  79. sprite.spriteFrame = this.mCheckIcon
  80. element.x = this.mCheckX
  81. }else{
  82. sprite.spriteFrame = this.mNoCheckIcon
  83. element.x = this.mNoCheckX
  84. }
  85. }
  86. }
  87. /**
  88. * 刷新背包
  89. */
  90. public flushPag(){
  91. if(this.mPagContent.children.length <= 0){
  92. for (let i = 0; i < 30; i++) {
  93. let node = cc.instantiate(this.mEquipItem);
  94. node.parent = this.mPagContent;
  95. }
  96. }
  97. this.flushEquip();
  98. }
  99. /**
  100. * 刷新装备
  101. */
  102. public flushEquip(){
  103. //筛选职业和类型符合的装备
  104. let pet = this.main.sManage.getRoleById(this.petIcon.id);
  105. let equips = this.main.player.equip;
  106. let index = 0;
  107. let content = this.mPagContent;
  108. for (let i = 0; i < equips.length; i++) {
  109. const element = equips[i];
  110. let _equip = this.main.sManage.getEquipById(element.id);
  111. if(_equip.type == this.equipType
  112. && (_equip.occ == 0 || pet.post == _equip.occ)){
  113. if(index >= content.children.length){
  114. this.addItem(content);
  115. }
  116. let node = content.children[index];
  117. RedPoint.removeRed(node)
  118. let equipItem = node.getComponent(GoodItem);
  119. equipItem.initEquip(this.main,element);
  120. equipItem.setCallback((gi)=>{
  121. this.checkEquip(gi);
  122. });
  123. let equipAttr = FFCalAttr.getEquipAttr(this.main,equipItem.equip,equipItem.equipData)
  124. let zIndex = cc.macro.MAX_ZINDEX - equipAttr.zdl
  125. if(zIndex < cc.macro.MIN_ZINDEX){
  126. zIndex = cc.macro.MIN_ZINDEX
  127. }
  128. node.zIndex = zIndex
  129. index ++;
  130. }
  131. }
  132. for (let i = index; i < content.children.length; i++){
  133. let node = content.children[i];
  134. RedPoint.removeRed(node)
  135. let equipItem = node.getComponent(GoodItem);
  136. equipItem.setNull();
  137. node.zIndex = cc.macro.MAX_ZINDEX - 1
  138. }
  139. }
  140. private addItem(content:cc.Node){
  141. for (let i = 0; i < 5; i++) {
  142. let node = cc.instantiate(this.mEquipItem);
  143. node.parent = content;
  144. }
  145. }
  146. public checkEquip(equipItem:GoodItem){
  147. this.equip.openChangeEquip(equipItem);
  148. }
  149. /**
  150. * 点击切换装备栏
  151. */
  152. public onclickType(event,customEventData){
  153. let equipType = parseInt(customEventData)
  154. this.setEquipType(equipType)
  155. }
  156. }