UI_Item.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { _decorator, Button, Component, instantiate, Label, Node, Sprite, SpriteFrame, tween, v3 } from 'cc';
  2. import { ch } from '../../../ch/ch';
  3. import { gui } from '../../../core/ui/ui';
  4. import { UI_Main } from '../UI_Main/UI_Main';
  5. import { Layout_Main } from '../UI_Main/Layout_Main';
  6. import { Hall } from '../../hall/Hall';
  7. import { Toast } from '../../../core/util_class/Toast';
  8. const { ccclass, property } = _decorator;
  9. @ccclass('UI_Item')
  10. export class UI_Item extends Component {
  11. @property
  12. type: number = 0;
  13. @property(Sprite)
  14. Ad: Sprite;
  15. @property(Label)
  16. count: Label;
  17. private _c: number = 0;
  18. protected start(): void {
  19. if (this.type == 0) return;
  20. this.show(this.type, Hall.getInstance().player.get_item(this.type));
  21. if (this.node.getComponent(Button)) {
  22. this.node.on(Node.EventType.TOUCH_END, this.onClick, this);
  23. }
  24. Hall.getInstance().player.event.on(Hall.getInstance().player.event.key.item_count, this.show, this);
  25. }
  26. protected onDestroy(): void {
  27. Hall.getInstance().player.event.off(Hall.getInstance().player.event.key.item_count, this.show, this);
  28. }
  29. show(type: number, count: number): void {
  30. if (type != this.type) return;
  31. if (this._c != count) {
  32. tween(this.node).to(0.05, { scale: v3(1.2, 1.2, 1) }).to(0.1, { scale: v3(1, 1, 1) }).start();
  33. if (count < this._c) {
  34. // if (type == 1) {
  35. // ch.audio.playOneShot('sounds/sfx_item1');
  36. // } else if (type == 2) {
  37. // ch.audio.playOneShot('sounds/sfx_item2');
  38. // } else {
  39. // ch.audio.playOneShot('sounds/sfx_item3');
  40. // }
  41. }
  42. }
  43. this._c = count;
  44. if (count != 0) {
  45. this.Ad.node.active = false;
  46. this.count.node.parent.active = true;
  47. this.count.string = count.toString();
  48. } else {
  49. this.Ad.node.active = true;
  50. this.count.node.parent.active = false;
  51. }
  52. if (type == 1) {
  53. // if (GameLink.getInst().lv.dir == LvDir.none) {
  54. // this.node.active = false;
  55. // } else {
  56. // this.node.active = true;
  57. // }
  58. }
  59. //this.ad.active = count <= 0;
  60. }
  61. //
  62. private async onClick(evt: any) {
  63. if (this._c <= 0) {
  64. if (this.type == 3) {
  65. let flag = false;
  66. for (let element of gui.get(UI_Main).getLayout<Layout_Main>().Container.node_isIdiom) {
  67. console.log(element);
  68. if (element == true) {
  69. flag = true;
  70. break;
  71. }
  72. }
  73. if (!flag) {
  74. Toast.makeText(gui.getLayerNode(5), "槽中无可清除的方块").show();
  75. return;
  76. }
  77. }
  78. const ret = await chsdk.playRewardAd('获得道具' + this.type);
  79. if (ret) {
  80. // ch.audio.playOneShot('sounds/sfx_add_item');
  81. Hall.getInstance().player.add_item(this.type, 1);
  82. Hall.getInstance().player.set_watch_ad_num(1);
  83. ch.audio.resume();
  84. }
  85. // gui.show(UI_ad_get, this.type);
  86. } else {
  87. if (this.type == 3) {
  88. let flag = false;
  89. for (let element of gui.get(UI_Main).getLayout<Layout_Main>().Container.node_isIdiom) {
  90. console.log(element);
  91. if (element == true) {
  92. Hall.getInstance().player.use_item(this.type);
  93. flag = true;
  94. break;
  95. }
  96. }
  97. if (!flag)
  98. Toast.makeText(gui.getLayerNode(5), "槽中无可清除的方块").show();
  99. //UINotify.show('槽中无可清除的方块');
  100. }
  101. else {
  102. Hall.getInstance().player.use_item(this.type);
  103. }
  104. }
  105. }
  106. }