UI_Item.ts 4.3 KB

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