UI_Item.ts 4.1 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. {
  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 (this._c <= 0) {
  65. if (this.type == 3) {
  66. let flag=false;
  67. for (let element of gui.get(UI_Main).getLayout<Layout_Main>().Container.node_isIdiom) {
  68. console.log(element);
  69. if (element == true) {
  70. flag=true;
  71. break;
  72. }
  73. }
  74. if(!flag)
  75. Toast.makeText(gui.getLayerNode(5),"槽中无可清除的方块").show();
  76. // UINotify.show('槽中无可清除的方块');
  77. return;
  78. }
  79. const ret = await chsdk.playRewardAd('获得道具' + this.type);
  80. if (ret) {
  81. // ch.audio.playOneShot('sounds/sfx_add_item');
  82. Hall.getInstance().player.add_item(this.type, 1);
  83. Hall.getInstance().player.set_watch_ad_num(1);
  84. ch.audio.resume();
  85. }
  86. // gui.show(UI_ad_get, this.type);
  87. } else {
  88. if (this.type == 3) {
  89. let flag=false;
  90. for (let element of gui.get(UI_Main).getLayout<Layout_Main>().Container.node_isIdiom) {
  91. console.log(element);
  92. if (element == true) {
  93. Hall.getInstance().player.use_item(this.type);
  94. flag=true;
  95. break;
  96. }
  97. }
  98. if(!flag)
  99. Toast.makeText(gui.getLayerNode(5),"槽中无可清除的方块").show();
  100. //UINotify.show('槽中无可清除的方块');
  101. }
  102. else {
  103. Hall.getInstance().player.use_item(this.type);
  104. }
  105. }
  106. }
  107. }