GoodItem.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import Main from "../../main/Main";
  2. import FFCalAttr from "../data/FFCalAttr";
  3. import { __EquipData, __GoodData } from "../data/sdata/SManage";
  4. import { EquipAttr } from "../data/udata/Player";
  5. const {ccclass, property} = cc._decorator;
  6. /**
  7. * 道具/装备节点
  8. */
  9. @ccclass
  10. export default class GoodItem extends cc.Component {
  11. @property(cc.Sprite)
  12. mBG: cc.Sprite = null;
  13. @property(cc.Sprite)
  14. mIcon: cc.Sprite = null;
  15. @property(cc.SpriteFrame)
  16. mNullIcon: cc.SpriteFrame = null;
  17. @property([cc.SpriteFrame])
  18. mBGs: Array<cc.SpriteFrame> = [];
  19. @property(cc.Node)
  20. mNullNode: cc.Node = null;
  21. @property(cc.Node)
  22. mStarNode: cc.Node = null;
  23. @property(cc.SpriteFrame)
  24. mStar: cc.SpriteFrame = null;
  25. @property(cc.Label)
  26. mLevel: cc.Label = null;//强化等级/道具数量
  27. @property(cc.Label)
  28. mContrast: cc.Label = null;//数量对比
  29. public main:Main;
  30. public equip:EquipAttr;
  31. public equipData:__EquipData;
  32. /**
  33. * {
  34. * icon:"good/1001",
  35. * count:1
  36. * }
  37. */
  38. public data:any;
  39. public good:__GoodData;
  40. /**
  41. {
  42. type:3, ---0:金币,1:钻石,2:道具,3:装备
  43. icon:'icon/equip/'+equip.icon,
  44. count:element.count,
  45. id:element.id,
  46. pz:equip.pz,
  47. PI:element.PI,
  48. star:element.star,
  49. PR:1
  50. }
  51. * @param rewardData
  52. */
  53. public initReward(main:Main,rewardData:any){
  54. this.mNullNode.active = true;
  55. cc.resources.load(rewardData.icon, cc.SpriteFrame, (err, spriteFrame:cc.SpriteFrame) =>{
  56. if(err){
  57. cc.error(err);
  58. }else{
  59. this.mIcon.spriteFrame = spriteFrame;
  60. }
  61. } );
  62. this.mBG.spriteFrame = this.mBGs[rewardData.pz-1];
  63. if(rewardData.type < 3){
  64. this.mLevel.string = 'x'+rewardData.count;
  65. }else{
  66. let qhLevel = FFCalAttr.getEquipQHLevel(main,rewardData.PI,rewardData._data)
  67. if(qhLevel > 0){
  68. this.mLevel.string = 'Lv.'+qhLevel;
  69. }else{
  70. this.mLevel.string = ''
  71. }
  72. this.mStarNode.destroyAllChildren()
  73. for (let i = 0; i < rewardData.star; i++) {
  74. let node = new cc.Node()
  75. let sprite = node.addComponent(cc.Sprite)
  76. sprite.spriteFrame = this.mStar
  77. node.parent = this.mStarNode
  78. }
  79. }
  80. }
  81. public initRewardInfo(data:any){
  82. this.mNullNode.active = true;
  83. cc.resources.load(data.icon, cc.SpriteFrame, (err, spriteFrame:cc.SpriteFrame) =>{
  84. if(err){
  85. cc.error(err);
  86. }else{
  87. this.mIcon.spriteFrame = spriteFrame;
  88. }
  89. });
  90. this.mBG.spriteFrame = this.mBGs[data.pz-1];
  91. if(data.count){
  92. this.mLevel.string = 'x'+data.count;
  93. }else{
  94. this.mLevel.string = '';
  95. }
  96. }
  97. public initEquip(main:Main,equip:EquipAttr){
  98. this.mNullNode.active = true;
  99. this.main = main;
  100. this.equip = equip;
  101. let sManage = main.sManage;
  102. this.equipData = sManage.getEquipById(equip.id);
  103. this.flushEquip(main);
  104. }
  105. public initGood(main:Main,data:any){
  106. this.mNullNode.active = true;
  107. this.main = main;
  108. this.data = data;
  109. if(this.data.icon == undefined){
  110. let sManage = this.main.sManage;
  111. this.good = sManage.getGoodById(this.data.id);
  112. this.data.icon = 'good/'+this.good.icon;
  113. }
  114. this.flushGood();
  115. }
  116. public init(main:Main,data:any){
  117. this.mNullNode.active = true;
  118. this.main = main
  119. this.data = data
  120. this.flushGood()
  121. }
  122. public flushEquip(main:Main){
  123. let equipData = this.equipData
  124. let qhLevel = FFCalAttr.getEquipQH(main,this.equip,equipData)
  125. if(qhLevel > 0){
  126. this.mLevel.string = 'Lv.'+qhLevel;
  127. }else{
  128. this.mLevel.string = ''
  129. }
  130. this.mNullNode.active = true;
  131. this.mBG.spriteFrame = this.mBGs[this.equipData.pz-1];
  132. cc.resources.load('icon/equip/'+this.equipData.icon, cc.SpriteFrame, (err, spriteFrame:cc.SpriteFrame) =>{
  133. if(err){
  134. cc.error(err);
  135. }else{
  136. this.mIcon.spriteFrame = spriteFrame;
  137. }
  138. } );
  139. this.mStarNode.destroyAllChildren()
  140. for (let i = 0; i < this.equip.star; i++) {
  141. let node = new cc.Node()
  142. let sprite = node.addComponent(cc.Sprite)
  143. sprite.spriteFrame = this.mStar
  144. node.parent = this.mStarNode
  145. }
  146. }
  147. public flushGood(){
  148. cc.resources.load('icon/'+this.data.icon, cc.SpriteFrame, (err, spriteFrame:cc.SpriteFrame) =>{
  149. if(err){
  150. cc.error(err);
  151. }else{
  152. this.mIcon.spriteFrame = spriteFrame;
  153. }
  154. } );
  155. this.mLevel.string = 'x'+this.data.count;
  156. if(this.good){
  157. this.mBG.spriteFrame = this.mBGs[this.good.pz-1];
  158. }else if(this.data.pz){
  159. this.mBG.spriteFrame = this.mBGs[this.data.pz-1];
  160. }
  161. }
  162. public setNull(){
  163. this.equip = null;
  164. this.equipData = null;
  165. this.data = null;
  166. this.good = null;
  167. this.mBG.spriteFrame = this.mNullIcon;
  168. this.mNullNode.active = false;
  169. this.callback = null;
  170. }
  171. public callback:(goodItem:GoodItem)=>void;
  172. public setCallback(callback:(goodItem:GoodItem)=>void){
  173. this.callback = callback;
  174. }
  175. public inGuide = false;//是否进行引导中
  176. private guideCallback:()=>void;
  177. public setGuideCallback(guideCallback:()=>void){
  178. this.guideCallback = guideCallback
  179. }
  180. public onclick(){
  181. if(this.callback){
  182. this.callback(this);
  183. }
  184. if(this.guideCallback){
  185. this.guideCallback()
  186. this.guideCallback = null;
  187. }
  188. }
  189. /**
  190. * 刷新数量对比
  191. */
  192. public initContrast(main:Main,data:any){
  193. this.main = main;
  194. this.data = data;
  195. if(this.data.id){
  196. let sManage = this.main.sManage;
  197. this.good = sManage.getGoodById(this.data.id);
  198. this.data.icon = 'good/'+this.good.icon;
  199. }
  200. this.flushGood();
  201. this.mLevel.node.active = false
  202. this.mContrast.node.active = true
  203. let count = this.main.player.getGoodCount(this.data.id)
  204. this.mContrast.string = count+'/'+data.count
  205. if(count >= data.count){
  206. this.mContrast.node.color = cc.color(75,43,0)
  207. }else{
  208. this.mContrast.node.color = cc.color(255,0,0)
  209. }
  210. }
  211. }