UI_Item.ts 3.8 KB

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