CardItem.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import i18n from "../../../../i18n/i18n";
  2. import Main from "../../../../main/Main";
  3. import { __GodCard, __GodCardLevel } from "../../../data/sdata/SManage";
  4. import { GodCardAttr } from "../../../data/udata/Player";
  5. const {ccclass, property} = cc._decorator;
  6. @ccclass
  7. export default class CardItem extends cc.Component {
  8. @property(cc.Label)
  9. mName: cc.Label = null;
  10. @property(cc.Sprite)
  11. mIcon: cc.Sprite = null;
  12. @property(cc.Node)
  13. mStarNode: cc.Node = null;
  14. @property([cc.SpriteFrame])
  15. mStarFrame: Array<cc.SpriteFrame> = [];
  16. @property(cc.Node)
  17. mNoNode: cc.Node = null;
  18. @property(cc.Label)
  19. mCount: cc.Label = null;
  20. public main:Main
  21. public godCardAttr:GodCardAttr;
  22. public _godCard:__GodCard
  23. private callback:(cardItem:CardItem)=>void
  24. public init(main:Main,godCardAttr:GodCardAttr):number{
  25. if(this.mNoNode){
  26. this.mNoNode.active = false
  27. }
  28. this.node.zIndex = 1
  29. this.main = main
  30. this.godCardAttr = godCardAttr
  31. this._godCard = this.main.sManage.getGodCardById(godCardAttr.id)
  32. this.initIcon()
  33. return this.flushStar()
  34. }
  35. public initNo(main:Main,_godCard:__GodCard){
  36. this.main = main
  37. this._godCard = _godCard
  38. if(this.mNoNode){
  39. this.mNoNode.active = true
  40. }
  41. this.initIcon()
  42. let myCount = this.main.player.getGoodCount(this._godCard.goodId)
  43. this.mCount.string = myCount +'/'+this._godCard.goodCount
  44. if(myCount >= this._godCard.goodCount){
  45. this.node.zIndex = 0
  46. }else{
  47. this.node.zIndex = 2
  48. }
  49. }
  50. private initIcon(){
  51. if(this.mName){
  52. this.mName.string = i18n.t(this._godCard.name)
  53. }
  54. cc.resources.load('icon/card/'+this._godCard.id, cc.SpriteFrame, (err, spriteFrame:cc.SpriteFrame) =>{
  55. if(err){
  56. cc.error(err);
  57. }else{
  58. this.mIcon.spriteFrame = spriteFrame;
  59. }
  60. } );
  61. }
  62. public flushStar():number{
  63. let star = 5;
  64. if(this.godCardAttr.level < this._godCard.list.length){
  65. let _godCardLevel:__GodCardLevel = this._godCard.list[this.godCardAttr.level]
  66. star = _godCardLevel.star
  67. }
  68. let nodes = this.mStarNode.children
  69. for (let i = 0; i < nodes.length; i++) {
  70. const node = nodes[i];
  71. let sprite = node.getComponent(cc.Sprite)
  72. if(i < star){
  73. sprite.spriteFrame = this.mStarFrame[1]
  74. }else{
  75. sprite.spriteFrame = this.mStarFrame[0]
  76. }
  77. }
  78. return star
  79. }
  80. public setCallback(callback:(cardItem:CardItem)=>void){
  81. this.callback = callback
  82. }
  83. public onclick(){
  84. if(this.callback){
  85. this.callback(this)
  86. }
  87. }
  88. }