UpgradeItem.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { _decorator, Sprite, Label, ProgressBar, EventTouch,Node, utils } from 'cc';
  2. import ListItem from '../../third/ListItem';
  3. import { ResUtil } from '../../utils/ResUtil';
  4. import { userIns } from '../../data/UserData';
  5. const { ccclass, property } = _decorator;
  6. //枪的属性数据
  7. @ccclass('UpgradeItem')
  8. export class UpgradeItem extends ListItem {
  9. @property({ type: Sprite, tooltip: "枪的属性图片" })
  10. public attr_sp: Sprite;
  11. @property({ type: ProgressBar, tooltip: "属性值的最大进度" })
  12. public progressBar: ProgressBar;
  13. @property({ type: Label, tooltip: "属性值的数据文本" })
  14. public atts_label: Label;
  15. @property({ type: Label, tooltip: "枪的属性多语言名字" })
  16. public attribute_lang_lable: Label;
  17. @property({ type: Node, tooltip: "广告按钮" })
  18. public ad_free_btn: Node;
  19. @property({ type: Node, tooltip: "视频按钮" })
  20. public video_btn: Node;
  21. @property({ type: Node, tooltip: "金币和钻石按钮" })
  22. public prop_btn: Node;
  23. @property({ type: Node, tooltip: "最大等级按钮" })
  24. public max_btn: Node;
  25. public data: any = null;
  26. public cb: Function = null;
  27. public start() {
  28. this.ad_free_btn.active = false;
  29. this.video_btn.active = false;
  30. this.prop_btn.active = true;
  31. this.max_btn.active = false;
  32. }
  33. /**
  34. * 点击了按钮
  35. */
  36. public onBtnClicked(event:EventTouch, param:any){
  37. if(!this.data)return;
  38. let idx: number = Number(param);
  39. this.cb?.(this.data,idx,event.target);
  40. }
  41. /**
  42. * 数据填充
  43. * @param data 数据
  44. * @param cb 回调
  45. */
  46. public init(data: any,cb?: Function){
  47. if(!data)return;
  48. this.data = data;
  49. this.cb = cb;
  50. //设置属性图标
  51. ResUtil.setSpriteFrame(data.attr_icon, this.attr_sp);
  52. //设置进度条
  53. this.progressBar.progress = data.attr_value / data.totalValue;
  54. //设置属性数值文本
  55. this.atts_label.string = `${data.attr_value}/${data.totalValue}`;
  56. //设置多语言属性名称
  57. this.attribute_lang_lable.string = data.attribute_lang;
  58. //升级需要消耗的材料
  59. const parts:string[] = data.nextExpenditure.split("_");
  60. const stuff:any = userIns.itemTable.find(e=>e.id == parts[0]);
  61. if(stuff){
  62. //设置材料图标
  63. ResUtil.setSpriteFrame(stuff.icon, this.prop_btn.getChildByName("icon"));
  64. //设置材料数量文本
  65. this.prop_btn.getChildByName("num_lable").getComponent(Label).string = `x${parts[1]}`;
  66. }
  67. //判断是否可以升级
  68. this.prop_btn.active = !data.isMaxLevel;
  69. this.max_btn.active = data.isMaxLevel;
  70. }
  71. }